tensorflow2.0 基础知识点3 (维度变换)

<1> tf.reshape(tensor,shape)  改变张量的形状
 

a = tf.range(12)

tf.reshape(a,[2,6])


<2> tf.expand_dims(input,axis) 增加维度,在指定维度上增加维度

t = tf.constant([1,2])
print(t.shape)
t1 = tf.expand_dims(t,axis=1)
print(t1.shape)
print(t1)

结果:
(2,)
(2, 1)
tf.Tensor(
[[1]
 [2]], shape=(2, 1), dtype=int32)

<3> tf.squeeze(input,axis) 删除维度,注意只能删除维度为1

t = tf.constant([[1,2]])
print(t.shape)
t1 = tf.squeeze(t)
print(t1.shape)
print(t1)

结果:
(1, 2)
(2,)
tf.Tensor([1 2], shape=(2,), dtype=int32)

<3>tf.transpose(input,perm) perm可以调换各轴顺序

a = tf.constant([[1,2,3],[4,5,6]])
tf.transpose(a)

结果:

<4>tf.concat(tensors,axis)  将张量按照指定轴进行拼接

a = tf.constant([[1,2],[3,4]])
b = tf.constant([[5,6],[7,8]])
print(tf.concat([a,b],axis = 1))
print(tf.concat([a,b],axis = 0))

结果:
tf.Tensor(
[[1 2 5 6]
 [3 4 7 8]], shape=(2, 4), dtype=int32)
tf.Tensor(
[[1 2]
 [3 4]
 [5 6]
 [7 8]], shape=(4, 2), dtype=int32)

<5>tf.split(value,num_or_size_splits,axis)  num_or_size_splits 分割方案

x = tf.range(24)
x = tf.reshape(x,[4,6])
print(x)
tf.split(x,2,0) 

结果:
tf.Tensor(
[[ 0  1  2  3  4  5]
 [ 6  7  8  9 10 11]
 [12 13 14 15 16 17]
 [18 19 20 21 22 23]], shape=(4, 6), dtype=int32)
[,
 ]

<6>

你可能感兴趣的:(笔记,python,tensorflow,深度学习)