1 创建
1.1 constant()方法
Int [1]: import tensorflow as tf
Int [2]: tf.constant(1) # 创建一个整型张量
Out [2]: <tf.Tensor: id=0, shape=(), dtype=int32, numpy=1>
Int [3]: tf.constant(1.) # 创建一个浮点型张量
Out [3]: <tf.Tensor: id=2, shape=(), dtype=float32, numpy=1.0>
Int [4]: tf.constant(2., dtype=tf.double) # 创建的同时指定数据类型
Out [4]: <tf.Tensor: id=4, shape=(), dtype=float64, numpy=2.0>
Int [5]: tf.constant([[1.,2.,3.],[4.,5.,6.]]) # 通过传入一个list参数创建
Out [5]: <tf.Tensor: id=6, shape=(2, 3), dtype=float32, numpy=
array([[1., 2., 3.],
[4., 5., 6.]], dtype=float32)>
如果输入的数据与指定的数据类型不相符,会产生以下异常:
TypeError: Cannot convert provided value to EagerTensor. Provided value: 2.1 Requested dtype: int32
1.2 convert_to_tensor()方法
Int[9]: import numpy as np
Int[10]: tf.convert_to_tensor(np.ones([2, 3]))
Out[10]: <tf.Tensor: id=9, shape=(2, 3), dtype=float64, numpy=
array([[1., 1., 1.],
[1., 1., 1.]])>
Int[11]: tf.convert_to_tensor(np.ones([2, 3]))
Out[11]: <tf.Tensor: id=11, shape=(2, 3), dtype=float64, numpy=
array([[1., 1., 1.],
[1., 1., 1.]])>
Int[12]: tf.convert_to_tensor([[2.,3.],[3., 4.]])
Out[12]: <tf.Tensor: id=13, shape=(2, 2), dtype=float32, numpy=
array([[2., 3.],
[3., 4.]], dtype=float32)>
1.3 创建元素为指定值的tensor
如果你熟悉numpy创建数组的方法,你一定见过zeros()、ones()等方法,TensorFlow中也有这些方法。
(1)zeros()与ones()
In [3]: a = tf.zeros([2, 3, 3]) # 创建一个元素全为0,形状为[2, 3, 3]的tensor
In [4]: a
Out[4]:
<tf.Tensor: id=4, shape=(2, 3, 3), dtype=float32, numpy=
array([[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]],
[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]]], dtype=float32)>
In [5]: b = tf.ones([2, 3]) # 创建一个元素全为1,形状为[2, 3]的tensor
In [6]: b
Out[6]:
<tf.Tensor: id=8, shape=(2, 3), dtype=float32, numpy=
array([[1., 1., 1.],
[1., 1., 1.]], dtype=float32)>
(2)zeros_like()与ones_like
In [7]: tf.zeros_like(b) # 仿照b的shape创建一个全为0的tensor
Out[7]:
<tf.Tensor: id=10, shape=(2, 3), dtype=float32, numpy=
array([[0., 0., 0.],
[0., 0., 0.]], dtype=float32)>
In [8]: tf.ones_like(a) # 仿照b的shape创建一个全为1的tensor
Out[8]:
<tf.Tensor: id=14, shape=(2, 3, 3), dtype=float32, numpy=
array([[[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]],
[[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]]], dtype=float32)>
(3)fill()
In [9]: tf.fill([2,3],5) # 创建元素全为5,形状为[2,3]的tensor
Out[9]:
<tf.Tensor: id=18, shape=(2, 3), dtype=int32, numpy=
array([[5, 5, 5],
[5, 5, 5]])>
1.2 随机初始化
在实际应用中,经常需要随机初始化元素服从某种分布的tensor,TensorFlow中也提供了这种功能。
(1)从指定正态分布中随机取值:tf.random.normal()。例如,随机初始化一个元素服从均值为1,方差为1的正态分布且形状为[2, 3]的tensor:
In [10]: tf.random.normal([2, 3], mean=1, stddev=1)
Out[10]:
<tf.Tensor: id=25, shape=(2, 3), dtype=float32, numpy=
array([[2.769353 , 0.29556453, 3.1812348 ],
[0.98696893, 1.5953724 , 1.280001 ]], dtype=float32)>
(2)从指定的截断正态分布中随机取值:truncated_normal()。意思是从指定的正太分布中取值,但是取值范围在两个标准差范围内,也就是:[ mean - 2 stddev, mean + 2 stddev ]
In [11]: tf.random.truncated_normal([2, 3], mean=1, stddev=1)
Out[11]:
<tf.Tensor: id=32, shape=(2, 3), dtype=float32, numpy=
array([[ 0.8288505 , 2.4870796 , 0.09972537],
[-0.2808863 , 0.8300834 , 0.8910987 ]], dtype=float32)>
(3)从指定均匀分布中随机取值:tf.random.uniform()。
In [12]: tf.random.uniform([2, 3], minval=1, maxval=2) # 在1~2之间均匀分布
Out[12]:
<tf.Tensor: id=40, shape=(2, 3), dtype=float32, numpy=
array([[1.857396 , 1.6122543, 1.354155 ],
[1.6087122, 1.7220445, 1.7369657]], dtype=float32)>
2 索引
In [14]: import numpy as np
In [15]: import tensorflow as tf
In [16]: a = tf.convert_to_tensor(np.arange(80).reshape(2,2,4,5))
In [17]: a
Out[17]:
<tf.Tensor: id=42, shape=(2, 2, 4, 5), dtype=int32, numpy=
array([[[[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]],
[[20, 21, 22, 23, 24],
[25, 26, 27, 28, 29],
[30, 31, 32, 33, 34],
[35, 36, 37, 38, 39]]],
[[[40, 41, 42, 43, 44],
[45, 46, 47, 48, 49],
[50, 51, 52, 53, 54],
[55, 56, 57, 58, 59]],
[[60, 61, 62, 63, 64],
[65, 66, 67, 68, 69],
[70, 71, 72, 73, 74],
[75, 76, 77, 78, 79]]]])>
2.1 基础索引
TensorFlow支持Python原生的基础索引方式,即多个方括号逐步索引取值:[idx][idx][idx],每个方括号对应一个维度。
In [18]: a[0] # 取第一个维度
Out[18]:
<tf.Tensor: id=47, shape=(2, 4, 5), dtype=int32, numpy=
array([[[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]],
[[20, 21, 22, 23, 24],
[25, 26, 27, 28, 29],
[30, 31, 32, 33, 34],
[35, 36, 37, 38, 39]]])>
In [19]: a[0][1] # 同时筛选两个维度
Out[19]:
<tf.Tensor: id=56, shape=(4, 5), dtype=int32, numpy=
array([[20, 21, 22, 23, 24],
[25, 26, 27, 28, 29],
[30, 31, 32, 33, 34],
[35, 36, 37, 38, 39]])>
In [20]: a[0][1][3][3] # 同时对4个维度进行筛选
Out[20]: <tf.Tensor: id=73, shape=(), dtype=int32, numpy=38>
这种索引数据的方法简单,易于理解,但是可读性差,只能按维度依次索引数据,也不能索引列。
2.2 numpy索引
TensorFlow也继承了numpy中的部分索引方式
(1)[idx1, idx2, idx3]
这种索引方式是在一个方括号内写下所有的索引,每个索引序号之间用逗号隔开。
In [21]: a[1] # 筛选第一维度,这跟基础索引一样
Out[21]:
<tf.Tensor: id=78, shape=(2, 4, 5), dtype=int32, numpy=
array([[[40, 41, 42, 43, 44],
[45, 46, 47, 48, 49],
[50, 51, 52, 53, 54],
[55, 56, 57, 58, 59]],
[[60, 61, 62, 63, 64],
[65, 66, 67, 68, 69],
[70, 71, 72, 73, 74],
[75, 76, 77, 78, 79]]])>
In [22]: a[1,1, 3] # 同时帅选3个维度
Out[22]: <tf.Tensor: id=83, shape=(5,), dtype=int32, numpy=array([75, 76, 77, 78, 79])>
(2)冒号切片与步长:[start?step]
这种索引方式在Python原生的list类型中也是常见的,而且使用方法也是一样的。
In [23]: a[1,:,0:2] # 对第1维度选第二块数据,对第二维度选所有数据,对第三维度选前两行
Out[23]:
<tf.Tensor: id=88, shape=(2, 2, 5), dtype=int32, numpy=
array([[[40, 41, 42, 43, 44],
[45, 46, 47, 48, 49]],
[[60, 61, 62, 63, 64],
[65, 66, 67, 68, 69]]])>
In [24]: a[1,:,0:2,0:4] # 继续上面的例子,对第4维度筛选去前4列
Out[24]:
<tf.Tensor: id=93, shape=(2, 2, 4), dtype=int32, numpy=
array([[[40, 41, 42, 43],
[45, 46, 47, 48]],
[[60, 61, 62, 63],
[65, 66, 67, 68]]])>
In [25]: a[1,:,0:2,0:4:2] # 对第4维度加上步长,每隔一个数据取一次
Out[25]:
<tf.Tensor: id=98, shape=(2, 2, 2), dtype=int32, numpy=
array([[[40, 42],
[45, 47]],
[[60, 62],
[65, 67]]])>
也可以使用负值步长表示逆序索引,但要注意,负数步长时,原本的[start : end : step]也要跟着编程[end : start : step]:
In [26]: a[1,:,0:2,4:0:-1]
Out[26]:
<tf.Tensor: id=103, shape=(2, 2, 4), dtype=int32, numpy=
array([[[44, 43, 42, 41],
[49, 48, 47, 46]],
[[64, 63, 62, 61],
[69, 68, 67, 66]]])>
In [27]: a[1,:,0:2,4:0:-2]
Out[27]:
<tf.Tensor: id=108, shape=(2, 2, 2), dtype=int32, numpy=
array([[[44, 42],
[49, 47]],
[[64, 62],
[69, 67]]])>
在numpy和TensorFlow中还有“…"(三个英文句号)的使用,“…"用于表示连续多个维度全选:
In [28]: a[1,...,0:4] # 等同于a[1, : , : ,0:4]
Out[28]:
<tf.Tensor: id=113, shape=(2, 4, 4), dtype=int32, numpy=
array([[[40, 41, 42, 43],
[45, 46, 47, 48],
[50, 51, 52, 53],
[55, 56, 57, 58]],
[[60, 61, 62, 63],
[65, 66, 67, 68],
[70, 71, 72, 73],
[75, 76, 77, 78]]])>
In [29]: a[0,0,...] # 等同于a[0,0,:,:]
Out[29]:
<tf.Tensor: id=118, shape=(4, 5), dtype=int32, numpy=
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]])>
2.3 gather与gather_nd
gather与gather_nd是指TensorFlow通过gather()方法和gather_nd()方法提供的两种索引方式。在numpy中,可以通过嵌套list的方式来指定无规则的索引:
In [30]: b = np.arange(20).reshape(4,5)
In [31]: b[1, [0,3,4]] # 选取第2行的第1列、第4列、第5列
Out[31]: array([5, 8, 9])
但是在TensorFlow中,这种索引方式并没有从numpy中继承下来,所以如果在Tensor中使用这种方式,会抛出以下异常:
TypeError: Only integers, slices ( : ), ellipsis (…), tf.newaxis (None) and scalar tf.int32/tf.int64 tensors are valid indices, got [0, 3, 4]
还好的是,在TensorFlow中通过gather()方法和gather_nd()方法提供了这种索引方法。
(1)gather()方法
In [32]: tf.gather(b, axis=0, indices=[0, 2, 3]) # 选取第1行,第3行,第4行
Out[32]:
<tf.Tensor: id=123, shape=(3, 5), dtype=int32, numpy=
array([[ 0, 1, 2, 3, 4],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]])>
In [33]: tf.gather(b, axis=1, indices=[0, 2, 3]) # 选取第1列,第3列,第4列
Out[33]:
<tf.Tensor: id=128, shape=(4, 3), dtype=int32, numpy=
array([[ 0, 2, 3],
[ 5, 7, 8],
[10, 12, 13],
[15, 17, 18]])>
仔细观察上面gather()方法例子,可以发现,第一个参数时数据源,还有两个参数中,axis指的是将要的维度,indices指的是需要选取的序号。
(2)gather_nd()
gather()方法一次只能对一个维度进行索引,gather_nd()方法可以同时对多个维度进行索引。
In [34]: tf.gather_nd(b, [[0, 2],[3, 3]]) # 选取第1行第3列的那个数据,和第4行第4列的数据
Out[34]: <tf.Tensor: id=132, shape=(2,), dtype=int32, numpy=array([ 2, 18])>
2.5 条件索引
可以结合一些简单的逻辑运算符进行索引取值:
In [1]: import tensorflow as tf
In [2]: a = tf.random.uniform([3,3],minval=-10,maxval=10,dtype=tf.int32)^M
...: a
2019-10-23 11:03:13.189763: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
Out[2]:
<tf.Tensor: id=3, shape=(3, 3), dtype=int32, numpy=
array([[ 5, 3, -5],
[ 0, -5, 8],
[ 7, -10, -8]])>
In [3]: mask = a < 0^M
...: mask
Out[3]:
<tf.Tensor: id=6, shape=(3, 3), dtype=bool, numpy=
array([[False, False, True],
[False, True, False],
[False, True, True]])>
可以看到,返回的是一个shape与a相同的tensor,在a小于零的位置是True,大于零的位置为False。进一步地,我们可以用boolwan_mask()方法直接取出符合条件的元素:
In [4]: tf.boolean_mask(a,mask)
Out[4]: <tf.Tensor: id=34, shape=(4,), dtype=int32, numpy=array([ -5, -5, -10, -8])>
可以结合where()方法取出符合条件元素的索引:
In [5]: m_index = tf.where(mask)^M
...: m_index
Out[5]:
<tf.Tensor: id=36, shape=(4, 2), dtype=int64, numpy=
array([[0, 2],
[1, 1],
[2, 1],
[2, 2]], dtype=int64)>
再使用之前说过的gather_nd()方法取值:
In [6]: tf.gather_nd(a,m_index)
Out[6]: <tf.Tensor: id=38, shape=(4,), dtype=int32, numpy=array([ -5, -5, -10, -8])>
where()方法还有第二种用法——从两个tensor中取出符合条件的值,这时候where()方法必须接受3个参数:
In [7]: condition = tf.random.uniform([3,3],minval=0,maxval=2,dtype=tf.int32)^M
...: condition = tf.cast(condition, tf.bool)^M
...: condition
Out[7]:
<tf.Tensor: id=44, shape=(3, 3), dtype=bool, numpy=
array([[ True, True, True],
[False, False, False],
[False, True, False]])>
In [8]: a = tf.range(1,10)^M
...: a = tf.reshape(a,[3,3])^M
...: a
Out[8]:
<tf.Tensor: id=51, shape=(3, 3), dtype=int32, numpy=
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])>
In [9]: b = tf.range(-9,0)^M
...: b = tf.reshape(b,[3,3])^M
...: b
Out[9]:
<tf.Tensor: id=58, shape=(3, 3), dtype=int32, numpy=
array([[-9, -8, -7],
[-6, -5, -4],
[-3, -2, -1]])>
In [10]: tf.where(condition, a, b)
Out[10]:
<tf.Tensor: id=60, shape=(3, 3), dtype=int32, numpy=
array([[ 1, 2, 3],
[-6, -5, -4],
[-3, 8, -1]])>
上面where()方法返回的结果在True的位置取值是a中对应位置元素的值,在False位置是b中对应元素的值。