记录一下,tensorflow的复习和tensorflow2.0的学习,首先是看看我们最为常用的一些tensor的操作方式,先是总结成为了思维导图,然后根据思维导图,强调一些常用的api。
1、常规的张量基础操作
下面就是一些实际使用的例子
1、创建Tensor
采用list或者numpy array的方式生成tensor
In [1]: import tensorflow as tf
In [2]: import numpy as np
In [3]: tf.convert_to_tensor([[1,1],[1,1]])
Out[3]:
In [4]: tf.convert_to_tensor(np.ones([2,2]))
Out[4]:
采用tf.ones、tf.zeros的方式生成,注意传入的shape。
In [5]: tf.ones([])
Out[5]:
In [6]: tf.ones([1])
Out[6]:
In [7]: tf.ones([2,2,2])
Out[7]:
采用tf.ones_like、tf.zeros_like的方式创建tensor:
In [8]: t = tf.ones([2,2])
In [9]: tf.zeros_like(t)
Out[9]:
采用tf.fill的方式创建:
In [10]: tf.fill([2,2],9)
Out[10]:
随机的创建tensor,使用random下面的方法来完成我们的目的:
In [11]: tf.random.normal([3,3], mean=10, stddev=1)
Out[11]:
In [12]: tf.random.truncated_normal([3,3],mean=0,stddev=1)
Out[12]:
In [13]: tf.random.uniform([3,3],minval=0,maxval=10)
Out[13]:
2、索引
data = [classes, students, subjects]
基础的索引就不记录,主要是说明一下选取式的索引方式:
In [14]: a = tf.random.normal([4,20,6])
In [15]: a.shape
Out[15]: TensorShape([4, 20, 6])
# 选取第一个和第二个学校
In [16]: tf.gather(a,axis=0,indices=[0,1]).shape
Out[16]: TensorShape([2, 20, 6])
# 选取第一个学生和第二个学生
In [17]: tf.gather(a,axis=1,indices=[0,1]).shape
Out[17]: TensorShape([4, 2, 6])
# 选取第一个科目和第二个科目
In [18]: tf.gather(a,axis=2,indices=[0,1]).shape
Out[18]: TensorShape([4, 20, 2])
# 选取第一个学校第一个学生和第二个学校及其第二个学生
In [19]: tf.gather_nd(a,[[0,0],[1,1]]).shape
Out[19]: TensorShape([2, 6])
# 同上面的道理
In [20]: tf.gather_nd(a,[[0,0,0],[1,1,1]]).shape
Out[20]: TensorShape([2])
In [21]: tf.boolean_mask(a, mask=[True, True, False, False]).shape
Out[21]: TensorShape([2, 20, 6])
In [22]: tf.boolean_mask(a, mask=[True, True, False, False,True, False],axis=2).shape
Out[22]: TensorShape([4, 20, 3])
3、Broadcasting
In [11]: b.shape
Out[11]: TensorShape([4, 20, 6])
In [12]: (b + tf.random.normal([6])).shape
Out[12]: TensorShape([4, 20, 6])
In [13]: (b + tf.random.normal([4,1,6])).shape
Out[13]: TensorShape([4, 20, 6])
In [14]: (b + tf.random.normal([1,1,6])).shape
Out[14]: TensorShape([4, 20, 6])
4、维度变换
In [24]: a.ndim
Out[24]: 3
In [25]: tf.reshape(a,[4,-1]).shape
Out[25]: TensorShape([4, 120])
In [26]: tf.transpose(a)
Out[26]: TensorShape([6, 20, 4])
通过tf.expand_dims给tensor增加一个维度
In [27]: a.shape
Out[27]: TensorShape([4, 20, 6])
In [28]: tf.expand_dims(a,axis=0).shape
Out[28]: TensorShape([1, 4, 20, 6])
In [29]: tf.expand_dims(a,axis=-1).shape
Out[29]: TensorShape([4, 20, 6, 1])
In [30]: tf.expand_dims(a,axis=2).shape
Out[30]: TensorShape([4, 20, 1, 6])
In [31]: tf.expand_dims(a,axis=-2).shape
Out[31]: TensorShape([4, 20, 1, 6])
通过tf.squeeze来挤压到维度只为1的那个维度
In [32]: b = tf.random.normal([4,28,28,1])
In [33]: b.shape
Out[33]: TensorShape([4, 28, 28, 1])
In [34]: tf.squeeze(b).shape
Out[34]: TensorShape([4, 28, 28])
5、数学运算
In [35]: a = tf.fill([2,2], 1.)
In [36]: b = tf.reshape(tf.range(4,dtype=tf.float32),[2,2])
In [37]: a+b, a-b, a*b, b/a, b//a, b%a
Out[37]:
(,
,
,
,
,
)
In [38]: tf.exp(a)
Out[38]:
In [39]: tf.pow(b,2)
Out[39]:
In [40]: tf.sqrt(b)
Out[40]:
矩阵相乘:
In [41]: A = tf.random.normal([2,3])
In [42]: B = tf.random.normal([3,4])
In [43]: A@B
Out[43]:
In [44]: tf.matmul(A,B)
Out[44]:
参考资料:
1、https://study.163.com/course/courseMain.htm?courseId=1209092816&share=1&shareId=1355397
2、https://github.com/dragen1860/TensorFlow-2.x-Tutorials