tf.transpose/expand_dims/squeeze

Reason is the light and the light of life.

Jerry Su Nov 23, 2020 1 mins
  • shape, ndim

  • reshape

  • expand_dims/squeeze

  • transpose

  • broadcast_to

import tensorflow as tf

view1-4不同的数据理解方式,不改变原有数据的content,并且数据全部使用。

  • view1: [b, 28, 28]

    图片包含行列信息

  • view2: [b, 28*28]

    图片的数据不变,理解方式改变, 没有了行列信息,常用于全连接层的输入

  • view3: [b, 2, 14*28]

    图片的上下两部分

  • view4: [b, 28, 28, 1]

    增加一个通道,channel概念,仅仅增加view1的理解方式,并未修改content

1. reshape

a = tf.random.normal([4, 28, 28, 3]) # [batch, row, col, channel] 行列理解方式
a.shape, a.ndim
(TensorShape([4, 28, 28, 3]), 4)
tf.reshape(a, [4, 28*28, 3]).shape   # [batch, pixel, channel]  像素理解方式(抹掉行列信息)
TensorShape([4, 784, 3])
tf.reshape(a, [4, -1, 3]).shape
TensorShape([4, 784, 3])
tf.reshape(a, [4, 28*28*3]).shape    # [batch, data_point]   数据点方式(抹掉行列和通道信息)
TensorShape([4, 2352])
tf.reshape(a, [4, -1]).shape
TensorShape([4, 2352])

reshape is flexible

2. tf.transpose

改变content

b = tf.random.normal([4, 28, 28, 3])
b.shape
TensorShape([4, 28, 28, 3])
tf.transpose(b).shape                      # [b, h, w, c]
TensorShape([3, 28, 28, 4])
tf.transpose(b, perm=[0, 2, 1, 3]).shape   # [b, w, h, c]
TensorShape([4, 3, 1, 2])
tf.transpose(b, perm=[0, 3, 2, 1]).shape   # [b, c, w, h]
TensorShape([4, 3, 28, 28])

3. tf.expand_dims

e.g.

  • a: [班级,学生, 成绩]

    [4, 35, 8] 4个班级,35个学生,8门课成绩

  • 增加学校维度dim=axis

    [1 ,4, 35, 8] + [1, 4, 35, 8]

axis增加方式:正数在轴前,负数在轴后

c = tf.random.normal([4, 35, 8])
tf.expand_dims(c, axis=0).shape
TensorShape([1, 4, 35, 8])
tf.expand_dims(c, axis=3).shape
TensorShape([4, 35, 8, 1])
tf.expand_dims(c, axis=-3).shape    # axis=0 vs -3
TensorShape([4, 1, 35, 8])

4. tf.squeeze

只能squeeze shape=1的维度

tf.squeeze(tf.zeros([1, 2, 1, 1, 5])).shape
TensorShape([2, 5])
d = tf.zeros([1, 2, 1, 3])
tf.squeeze(d, axis=0).shape, tf.squeeze(d, axis=2).shape, tf.squeeze(d, axis=-4).shape, tf.squeeze(d, axis=-2).shape
(TensorShape([2, 1, 3]),
 TensorShape([1, 2, 3]),
 TensorShape([2, 1, 3]),
 TensorShape([1, 2, 3]))


Read more:

Related posts: