5 - 维度变换
1个月前 • 151次点击 • 来自 TensorFlow
收录专题: TensorFlow入门笔记
维度变换的方式:
方式1:tf.reshape(可通过破坏维度之间的关系改变tensor的维度,但不会改变原有数据的存储顺序)
a = tf.random.normal([4,28,28,3])
print(a.shape)
print(tf.reshape(a,[4,784,3]).shape)
print(tf.reshape(a,[4,-1,3]).shape)
print(tf.reshape(a,[4,784*3]).shape)
print(tf.reshape(a,[4,-1]).shape)
但是reshape在恢复已经reshape的数据时会出现问题,比如[ 4,28,28,3 ]的数据reshape成[ 4,784,3 ]的数据要想再恢复成以前的样子,就需要记录下以前的content(内容)信息,如果记录过程出现错误(如width和height维度记反或者数值记错),就会导致恢复不成想要的样子。
方式2:tf.transpose (content的变换)
a = tf.random.normal([4,3,2,1])
print(a.shape)
print(tf.transpose(a).shape)
print(tf.transpose(a,perm=[0,1,3,2]).shape)
通过这种变换方式会彻底改变原来图片数据的维度关系,在经过transpose之后,再用reshape变换得到的数据是基于新的content(transpose之后)进行的变换,所以reshape时要记录新的content信息,不然会导致数据混乱甚至程序异常。
方式3:tf.expand_dims、tf.squeeze (增加和减少维度)
a = tf.random.normal([4,35,8])
# tf.expand_dims增加维度
# 若给定axis>0,则在给定轴前增加维度,若给定axis<0,则在给定轴后增加维度
print(tf.expand_dims(a,axis=0).shape)
print(tf.expand_dims(a,axis=3).shape)
print(tf.expand_dims(a,axis=-1).shape)
print(tf.expand_dims(a,axis=-4).shape)
# tf.squeeze用于减少维度
print(tf.squeeze(tf.zeros([1,2,1,1,3])).shape)
a = tf.zeros([1,2,1,3])
print(tf.squeeze(a,axis=0).shape)
print(tf.squeeze(a,axis=2).shape)
print(tf.squeeze(a,axis=-2).shape)
print(tf.squeeze(a,axis=-4).shape)
需要注意的是,squeeze只能减少维度值为1的维度,且axis必须为已存在的轴索引
标签