TensorFlow介绍-TensorFlow2.0基础操作

数据类型:
常见的数据类型载体
• List:list设计非常的灵活,可以随意的插入添加和编辑,内存的管理不是很连续。对于高维度数据的读取和写入效率也会很低。
• np.array专门用来解决同类型数据运算的一个载体,可以很方便的将图片数据进行吞吐和转置等计算操作。弊端:在深度学习之前就已设计和广泛应用的科学计算库,没有很好的GPU计算支持,也不能支持自动求导。因此tensorflow 就应运而生。
• tf.Tensor: TensorFlow 和 Numpy的地位在某种层面上相似,例如一些拼接,random的操作等等。而且为了方便使用Numpy的开发者,能够更便利的转移到TensorFlow,在一些API的命名上也很相似。只不过功能上更加偏重于神经网络的计算。
什么叫tensor
• Scalar(标量):1.1,2…,实质是指一个准确的数据类型。如果是标量1.1:维度为0。
• Vector(向量): [1.1], [1.1, 2.2, 3.3…]。如果是向量[1.1]:维度是1。
• Matrix(矩阵): [[1.1,2.2],[3.3,4.4],[5.5,6.6]]。维度通常描述为m*n。
• tensor(张量): 数学上用到的定义:一般是指维度大于2的都称为tensor。在TensorFlow中习惯上将标量和一维向量也称为tensor,因此所有的数据都可以称为tensor。
Flow
TensorFlow介绍-TensorFlow2.0基础操作
• 很多的数据经过很多的操作从输入到输出完成一个flow的过程,就像水流一样连接每一个管道完成每一个运算。
V2.0中常用数据类型
• TensorFlow中常用的数据类型有:
• int, float, double:
• tf.constant(1) : constant沿用1.0版本,可以理解成是一个普通的tensor
<tf.Tensor: id=4, shape=(), dtype=int32, numpy=1>
• tf.constant(1.)
<tf.Tensor: id=4, shape=(), dtype=float32, numpy=1.0>
• tf.cosntant(2.2, dtype=tf.int32),如果输入2.2 但是指定类型为int类型,则会报错
• tf.constant(2., dtype=tf.double)
<tf.Tensor: id=7, shape=(), dtype=float64, numpy=2.0>,指定双精度类型 , double型实质是一个别名,对应的是 float64。
• bool: 布尔类型
• tf.constant([True, False])
<tf.Tensor: id=9, shape=(2,), dtype=bool, numpy=array([True, False])
• string:
• tf.constant(‘hello,world’)
<tf.Tensor: id=14, shape(), dtpype=string, numpy=b’hello,world’>
V2.0常用的属性(device)
• device:
TensorFlow介绍-TensorFlow2.0基础操作
• 如果需要将tensor在CPU和GPU之间相互转移,操作如下:
TensorFlow介绍-TensorFlow2.0基础操作
• 一个CPU上的tensor和一个GPU上的tensor的区别在于:在GPU上的tensor只能进行GPU上的操作,同理对于一个CPU上的tensor也只能进行CPU上的操作。但往往我们的操作是自动判定的,操作符是根据传入参数的tensor来自动选择,如果该tensor在GPU上就选在在GPU上来完成操作,反之亦然。
• 例如: a+b 必须两个tensor同时在CPU上或者GPU上, 不支持一个在CPU一个在GPU上, 否则会报错。
V2.0常用的属性(numpy,ndim)
• numpy:支持tensor类型直接转换成np.array。
TensorFlow介绍-TensorFlow2.0基础操作
• 查看维度的属性有:
TensorFlow介绍-TensorFlow2.0基础操作
V2.0常用的属性(判断tensor)
• 判断是否是tensor
TensorFlow介绍-TensorFlow2.0基础操作
• 查看数据类型
TensorFlow介绍-TensorFlow2.0基础操作
V2.0常用的属性(数据转换)
• Convert
TensorFlow介绍-TensorFlow2.0基础操作
• cast:同样可以实现数据转换,且更简洁,只需要指定dtype。
TensorFlow介绍-TensorFlow2.0基础操作
• 很明显看出实际上b还是个tensor,但是使用isinstance方法查询结果返回的是False。 因此我们更推荐使用tf.is_tensor和tf.dtype 来查询数据的类型。

创建Tensor:
from numpy/list
• from numpy
TensorFlow介绍-TensorFlow2.0基础操作
• from list
TensorFlow介绍-TensorFlow2.0基础操作
zeros/ones
• tf.zeros: 初始化全为0
TensorFlow介绍-TensorFlow2.0基础操作
• tf.ones: 类似于tf.zeros,在这里是初始化全为1。
TensorFlow介绍-TensorFlow2.0基础操作
Uniform/shuffle
• tf.random.uniform(): 随机均匀分布初始化
TensorFlow介绍-TensorFlow2.0基础操作
• tf.random.shuffle(): 将数据顺序打乱
TensorFlow介绍-TensorFlow2.0基础操作

V2.0索引和切片:
索引
• 基础索引方式:给定每一个维度的索引([index][index])
TensorFlow介绍-TensorFlow2.0基础操作
• 类似于Numpy的索引方式
TensorFlow介绍-TensorFlow2.0基础操作
切片
• [start:end]:
TensorFlow介绍-TensorFlow2.0基础操作
• [start????stop]/[::step]
TensorFlow介绍-TensorFlow2.0基础操作
• b[::-1]: 倒序
• ‘…’: 默认为任意长的冒号
TensorFlow介绍-TensorFlow2.0基础操作
• tf.gather: 可以获取指定维度所指定索引的数据。并且可以用来进行抽取数据顺序的设定。
TensorFlow介绍-TensorFlow2.0基础操作

V2.0维度变换
• shape, ndim
• reshape
• expand_dims/squeeze
• transpose
• broadcast_to
View
• view的概念(content):
• [b, 28, 28] → 标准的content模式
• [b, 2828]
• [b, 2, 14
28]
• [b, 28, 28, 1] → [batch, height(row), width(columa), channel]
Reshape
• reshape的使用方法:
TensorFlow介绍-TensorFlow2.0基础操作
reshape的灵活性:
TensorFlow介绍-TensorFlow2.0基础操作
• Reshape虽然灵活性很好,原则上可以有很多种组合方式,但是同时要考虑到,所设定的view是否有相应的物理意义
转置
• tf.transpose:[w,h] →[h, w]
TensorFlow介绍-TensorFlow2.0基础操作
• 指定转置的维度
TensorFlow介绍-TensorFlow2.0基础操作
增加维度
• 维度扩张:expand dim
• a: [classes, students, classes] → [4, 35, 8]: 可以描述为4个班级,每个班级有35个学生,每个学生有8门课程。
• 增加学校的维度: [1, 4, 35, 8]+[1, 4, 35, 8] → [2, 4, 35, 8]
TensorFlow介绍-TensorFlow2.0基础操作
减少维度
• 维度压缩:
• squeeze dim
TensorFlow介绍-TensorFlow2.0基础操作
• 通过axis参数来指定要压缩的维度:
TensorFlow介绍-TensorFlow2.0基础操作
broadcasting(广播)
• Broadcasting:本质是张量维度扩张的一个手段,指对某一个维度上重复n次但是确没有真正的复制一个数据。
• 扩张
• 没有复制数据
• 通过tf.broadcast_to来实现
• 广播的方法:
• 在需要的时候添加维度,且添加维度的初始size为1。
• 扩张初始化为1的维度大小与需要进行计算的维度大小相同。
• 例: [4, 32, 32, 3]+[3], 对[3]进行broadcasting,方法如下:
[3]→[1, 1, 1, 3] →[4, 32, 32, 3]
广播的优势
• 为什么使用broadcasting:
• 编辑代码更加简洁:
[classes, students, scores] : +5 score
[4, 32, 8] + [4, 32, 8],通过expand的方式需要先把维度扩张到相同size, 再进行计算。
[4, 32, 8] + [5.0] (此处的5.0为数值,shape为8),广播的方式则是在后台自动进行扩张计算。
• 节省内存:不需要复制数据,不占内存。
[4, 32, 8] → 10244
[8] → 8
4
广播的实现
• 广播的实现:不需要输入tf.broadcast_to的命令,而是自动判断该操作符是否支持broadcastng, 如果支持会自动进行广播并完成计算。
TensorFlow介绍-TensorFlow2.0基础操作
• 若无法复制成相同维度大小,则无法进行广播运算。
TensorFlow介绍-TensorFlow2.0基础操作

V2.0数学运算:
运算符的类型
• 对应元素的操作:
• ,+, -, *, /
• //, %,
• exp, log
• pow, sqrt
• 矩阵的操作:
• @, matmul
• 维度的操作:对某一个维度的操作
• Reduce_mean/max/min/sum
对应元素的操作符
• +, -, *, /
TensorFlow介绍-TensorFlow2.0基础操作
• //, %
TensorFlow介绍-TensorFlow2.0基础操作
• Exp
TensorFlow介绍-TensorFlow2.0基础操作
• log: V2.0不支持直接调用log的API,并且log是默认以e为底。
TensorFlow介绍-TensorFlow2.0基础操作
• Pow
TensorFlow介绍-TensorFlow2.0基础操作
• sqrt
TensorFlow介绍-TensorFlow2.0基础操作
• @
TensorFlow介绍-TensorFlow2.0基础操作
• tf.matmul(a, b)
TensorFlow介绍-TensorFlow2.0基础操作