TensorFlow2中的Tensor的常用操作速查速记

数据类型

  • 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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值