TensorFlow三大操作之维度变换

2020-01-31  本文已影响0人  酵母小木

【时间:2020年1月31日】希望疫情赶紧过去,惟愿祖国繁荣昌盛,人家平平安安。也不知道能为别人做些啥?好好待在家里,增强自己的抵抗力不给家人和国家添麻烦,是我最大的功劳。

今天观看了电影《死亡诗社》,超级喜欢这种学院风格的电影,之前就像看了,但是一直没那种心境,类似的有《心灵捕手》《美丽心灵》《历史系男生》等。我从中得到的东西,再一次强调了“What is free”、“Do not be silly”,要想成为一个真正的人,必须做到辨识种种迷雾和真相,明白什么是媚俗、蛊惑、煽动、真理、追求,做到冷静和自律。这部电影配合《斯通纳》食用更佳!

“人类一思考,上帝就发笑”——米兰·昆德拉

1.Reshape:改变视图(View)

tf.reshape(
    tensor,
    shape,  //重组shape
    name=None
)

练习代码如下

In [3]: a = tf.random.normal([4, 28, 28,3])

In [4]: a.shape, a.ndim
Out[4]: (TensorShape([4, 28, 28, 3]), 4)
//舍弃行列信息
In [5]: tf.reshape(a, [4, 784, 3]).shape
Out[5]: TensorShape([4, 784, 3])
//用【-1】代替实现自动推导
In [6]: tf.reshape(a, [4, -1, 3]).shape
Out[6]: TensorShape([4, 784, 3])
//舍弃行列信息和通道信息
In [7]: tf.reshape(a, [4, 784*3]).shape
Out[7]: TensorShape([4, 2352])
//用【-1】代替实现自动推导
In [8]: tf.reshape(a, [4, -1]).shape
Out[8]: TensorShape([4, 2352])

2.Transpose:改变内容(Content)

//轴交换:装置
tf.transpose(
    a,
    perm=None,
    conjugate=False,
    name='transpose'
)

练习代码如下

//转置的含义
In [14]: b = tf.random.normal([2,3])

In [15]: b
Out[15]: <tf.Tensor: id=40, shape=(2, 3), dtype=float32, numpy=
array([[-0.31084606, -0.01956377,  1.1082025 ],
       [ 0.94886476,  0.08457222, -0.208771  ]], dtype=float32)>

In [16]: tf.transpose(b)
Out[16]: <tf.Tensor: id=42, shape=(3, 2), dtype=float32, numpy=
array([[-0.31084606,  0.94886476],
       [-0.01956377,  0.08457222],
       [ 1.1082025 , -0.208771  ]], dtype=float32)>

In [17]: tf.transpose(b).shape
Out[17]: TensorShape([3, 2])

//对应轴变换
In [18]: a = tf.random.normal((4, 3, 2, 1))

In [19]: a.shape
Out[19]: TensorShape([4, 3, 2, 1])
//若未指定转置顺序,则完全转置
In [20]: tf.transpose(a).shape
Out[20]: TensorShape([1, 2, 3, 4])

In [21]: tf.transpose(a, perm=[0, 1, 3, 2]).shape
Out[21]: TensorShape([4, 3, 1, 2])

//示例:data=(b, h, w, c)
In [22]: a = tf.random.normal([4, 28, 28, 3])
data=(b, w, h, c)
In [23]: tf.transpose(a, [0, 2, 1, 3]).shape
Out[23]: TensorShape([4, 28, 28, 3])
data=(b, c, w, h)
In [24]: tf.transpose(a, [0, 3, 2, 1]).shape
Out[24]: TensorShape([4, 3, 28, 28])
data=(b, c, h, w)
In [25]: tf.transpose(a, [0, 3, 1, 2]).shape
Out[25]: TensorShape([4, 3, 28, 28])

3.Expand_dims:增加维度

tf.expand_dims(
    input,
    axis,      //插入维度所在序列号,对应维度往后移动,正序则往后移,负序则往前移
    name=None
)

代码练习

//data:[classes, students, classes]
//[4, 35, 8]
// How to add school dims
In [26]: a = tf.random.normal([4, 35, 8])

In [27]: tf.expand_dims(a, axis=0).shape
Out[27]: TensorShape([1, 4, 35, 8])

In [28]: tf.expand_dims(a, axis=3).shape
Out[28]: TensorShape([4, 35, 8, 1])

In [29]: a = tf.random.normal([4, 35, 8])
//正序,【序号】所在元素的前方插入
In [30]: tf.expand_dims(a, axis=0).shape
Out[30]: TensorShape([1, 4, 35, 8])

In [31]: tf.expand_dims(a, axis=3).shape
Out[31]: TensorShape([4, 35, 8, 1])
//负序,【序号】所在元素的后方插入
In [32]: tf.expand_dims(a, axis=-1).shape
Out[32]: TensorShape([4, 35, 8, 1])

In [33]: tf.expand_dims(a, axis=-4).shape
Out[33]: TensorShape([1, 4, 35, 8])

4.Squeeze:减少维度

//去掉对应的维度。但是只能去掉单元素的数据项
tf.squeeze(
    input,
    axis=None,
    name=None
)

代码练习

In [35]: a = tf.zeros([1, 2, 1, 1, 3])

In [36]: a.shape
Out[36]: TensorShape([1, 2, 1, 1, 3])
//默认参数则自动缩减
In [37]: tf.squeeze(a).shape
Out[37]: TensorShape([2, 3])
//无论正序还是负序,都是一样删除对应的单元素数据项
In [48]: a = tf.zeros([1, 2, 1, 3])

In [49]: tf.squeeze(a, axis=0).shape
Out[49]: TensorShape([2, 1, 3])

In [50]: tf.squeeze(a, axis=2).shape
Out[50]: TensorShape([1, 2, 3])

In [51]: tf.squeeze(a, axis=-2).shape
Out[51]: TensorShape([1, 2, 3])

In [52]: tf.squeeze(a, axis=-4).shape
Out[52]: TensorShape([2, 1, 3])

参考资料

上一篇下一篇

猜你喜欢

热点阅读