数据类型
- Int, Float, Double
- Boolean
- String
其实Boolean和String类型一般是没有什么用的,Boolean在一定程度上可能还怎加了程序编写的复杂性。但也可以让思路更加清楚。
Tensor
tf.constant()
tf.device()
tf.rank()
tf.is_tensor()
Tensor.device
Tensor.cpu()
; Tensor.gpu()
Tensor.ndim
Tensor.dtype
Variable
tf.Variable()
Variable是对Tensor的再次封装,让该Tensor可以求导,Variable依旧是Tensor。
创建Tensor
从numpy的ndarray或者是list转化
tf.convert_to_tensor
固定生成
tf.zeros()
; tf.ones()
tf.zeros_like()
; tf.ones_like()
tf.fill()
tf.range()
随机生成
tf.random.normal()
tf.random.truncated_normal()
tf.random.uniform()
tf.random.shuffle()
索引和切片
索引
:
::
...
基本上和numpy一模一样,没啥好记录的
切片
tf.gather()
指定维度(axis),然后在指定索引(index)
tf.gather_nd()
多维度上采样,无需指定维度,根据索引自动推断维度
tf.boolean_mask()
注意mask的含义,可以指定维度,也可对维度进行自行的推断
维度变换
注意理解View的概念
tf.reshape()
tf.transpose()
tf.expand_dims()
tf.squeeze()
Broadcasting
合并与分割
合并
tf.concat()
在某一个维度上堆叠,要求除了堆叠的维度都相等
tf.stack()
会创建新的维度,要求所有维度都相等
拆分
tf.unstack()
指定一个维度,拆分这个维度并消除这个维度
tf.split()
在指定的维度上任意拆分
数学运算
tf.math.log()
tf.math.exp()
tf.math.pow()
tf.math.sqrt()
tf.math.top_k()
数据统计
tf.norm()
tf.reduce_min()
; tf.reduce_max()
; tf.reduce_mean()
之所以加reduce是因为改操作会减少Tensor的维度
tf.argmax()
;tf.argmin()
tf.equal()
tf.unique()
张量排序
tf.sort()
tf.argsort()
注意Top-K Accuracy的实战练习
Tensor的高级操作
填充与复制
tf.pad()
tf.tile()
张量限幅
tf.clip_by_value()
tf.nn.relu()
tf.clip_by_norm()
tf.clip_by_global_norm()
高阶操作
tf.where()
tf.scatter_nd()
tf.meshgrid()