标量:单个实数,shape=[]
向量:1维实数集合,shape=[n]
矩阵:n行m列集合,shape=[n,m]
张量:维度dim>2的数组
Tensorflow一般把以上统称为张量
a = tf.constant([0,1,2,3,4],dtype=tf.int32)
Out: <tf.Tensor: shape=(4,), dtype=int32, numpy=array([1, 2, 3, 4])>
此处shape=[4,]表示创建的是向量
a.numpy()
Out: array([1, 2, 3, 4])
a = tf.constant(True)
out: <tf.Tensor: shape=(), dtype=bool, numpy=True>
一般使用tf.int32、tf.int64、tf.float32、tf.float64
在创建张量时定义
a.dtype#读取精度
tf.cast(a,tf.int64)#精度转换
精度转换时注意发生数据溢出(高精度转低精度时)
a = tf.constant(123456789,dtype = tf.int32)
Out[10]: <tf.Tensor: shape=(), dtype=int32, numpy=123456789>
tf.cast(a,tf.int16)
Out[12]: <tf.Tensor: shape=(), dtype=int16, numpy=-13035>
布尔值与整数之间的转换也是合法的
In [16]: a = tf.constant(123456789,dtype = tf.int32)
In [17]: tf.cast(a,tf.bool)
Out[17]: <tf.Tensor: shape=(), dtype=bool, numpy=True>
0为False,非0数字为True
例如参数w1 b1 w2 b2…需要计算计算梯度信息的张量,叫做待优化张量
a = tf.Variable([1,2,3])#也可以将普通张量转化为待优化张量
将Numpy数组或python列表转化为Tensor张量
tf.convert_to_tensor([1,2.])
tf.convert_to_tensor(np.array([[1,2],[3,4]]))
tf.zeros([2,2])#shape=[2,2]
tf.ones([3,3,3])#shape=[3,3,3]
创建形如a的全0\1的张量
tf.ones_like(a)
tf.fill([2,2],99)
Out[20]:
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[99, 99],
[99, 99]])>
tf.random.normal([2,2],meam=0,stddev=1)
#创建正态分布的张量,shape=[2,2],均值为0,标准差为1
tf.random.uniform([3,3],minval=0,maxval=1)
#创建均匀分布张量,区间为[0,1]----如果没有规定范围默认0-1
tf.range()#python的range用法一致
需要理解存储、视图者之间的关系:同一存储,从不同角度观察数据能得到不同的视图。
改变视图的前提是存储不需要改变,否则改变视图操作是不合法的。
如果定义不合法的视图,例如将[b,h,w,c]转换为[b,c,h,w]呢?这个时候存储就进行了改变。
需要交换维度!
a = tf.random.normal([2,32,32,6])
b=tf.transpose(a,perm=[3,1,2,0])
In [28]: b.shape
Out[28]: TensorShape([6, 32, 32, 2])
增加维度
In [29]: a = tf.random.normal([2,32,32,6])
In [30]: b = tf.expand_dims(a,axis=0)
In [31]: b.shape
Out[31]: TensorShape([1, 2, 32, 32, 6])
删除维度(只能删除维度为1的)
In [29]: b = tf.random.normal([1,2,32,32,6])
In [37]: c = tf.squeeze(b,axis=2)
#error
out: Can not squeeze dim[2], expected a dimension of 1
In [38]: c = tf.squeeze(b,axis=0)
In [40]: c.shape
Out[40]: TensorShape([2, 32, 32, 6])
In [44]: a = tf.constant([[1,2,4],[3,4,6]])
In [47]: a.numpy()
Out[47]:
array([[1, 2, 4],
[3, 4, 6]])
In [48]: tf.tile(a,multiples=[2,1])#即在axit=0位置复制1次,在axis=1位置不复制
Out[48]:
<tf.Tensor: shape=(4, 3), dtype=int32, numpy=
array([[1, 2, 4],
[3, 4, 6],
[1, 2, 4],
[3, 4, 6]])>
tip:x@w+b中,b的加法运算会自动进行插入和复制数据的操作,不需要手动执行。(广播机制)
In [54]: a
Out[54]:
<tf.Tensor: shape=(2, 3), dtype=int32, numpy=
array([[1, 2, 4],
[3, 4, 6]])>
In [52]: c
Out[52]: <tf.Tensor: shape=(), dtype=int32, numpy=1>
In [55]: a+c
Out[55]:
<tf.Tensor: shape=(2, 3), dtype=int32, numpy=
array([[2, 3, 5],
[4, 5, 7]])>
可以使用tf.pow(x,n),也可以使用x**n的方法。
In [20]: a
Out[20]:
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[7, 7],
[3, 9],
[8, 5]])>
In [21]: a = tf.pow(a,2)
Out[21]:
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[49, 49],
[ 9, 81],
[64, 25]])>
特别的,平方运算tf.square(x),平方根运算tf.sqrt(x)。
tf.exp(1.)#自然指数
tf.matb,log(x)#对数运算
矩阵乘法的规则与线性代数一致,这里要求:A的第一维度长度和B的倒数第二个维度长度相等。
a = tf.random.uniform ([4,3,28,32])
b = tf.random.uniform ([4,3,32,2])
c=a@b
c.shape
out: TensorShape([4, 3, 28, 2])
矩阵乘法支持自动扩展。