Tensorflow2.0——基础
Tensorflow基础
Tensorflow数据类型
- scalar:标量1.1
- vector:向量[1.1],[1.1,2.2]
- matrix:矩阵[[1.1,2.2],[3.3,4.4]]
- tensor:所有数据类型
- int、float、double
- bool、string
【基本操作】
【数据类型转换】
创建tensor
- from numpy、list;
- zeros、ones;
- fill;
- random;
- constant;
- Application
1.from numpy、list
2.tf.zeros、tf.ones
3.normal(正态分布)
4.Uniform(均匀分布)
Typical Dim Data
1.Scalar:[]
例:Loss
2.Vector:[1,2,3,4…]
3.Matrix
input x:[b, vec_dim]
weight:[input_dim,output_dim]
4.Dim=3 Tensor
词向量:x:[b, seq_len, word_dim]
b:语句个数;seq_len:语句所含词语数;word_dim:每个词向量的长度
5.Dim=4 Tensor
Image:[b, h, w, 3]
feature maps:[b, h, w, c]
b:图片个数;h:高度;w:宽度;c:通道数
6.Dim=5 Tensor
Single task:[b, h, w, 3]
meta_task:[task_b, b, h, w, 3]
Tensorflow基本操作
索引和切片
1.索引
start: end: step
倒序采样:
省略号:…
2.tf.gather
data:[classes, students, subjects]
[4, 35, 8]
(1)tf.gather
(2)tf.gather_nd
(3)tf.boolean_mask
维度变化
1.reshape
reshape有时会导致潜在的问题,容易造成维度混乱,一定小心使用。
2.tf.tranpose 指定维度转换
3.Expand Dim
4.Squeeze Dim
Broadcasting
注意:维度匹配,从最后一个维度(最小的)开始。
如果维度为1就变成相同的;如果没有维度就增加一个维度变成相同的;否则,如果不同就不可转变。
1.Broadcasting
2.tf.broadcast_to
3.Broadcast VS Tile
数学运算
- ±*/, //, %
- **, pow, square, sqrt
- exp, log
- @, matmul
- linear layer
1.tf.math.log 、tf.exp
【log2或log10,底数放在后面】
2.pow、sqrt
3.@、mutmul