Tensorflow系列:reshape

来源:互联网 发布:装修得花多少钱 知乎 编辑:程序博客网 时间:2024/06/08 09:04

矩阵变形是常用的操作,在Tensorflow中调用方式有多种,例如:

1. tf.reshape

 tf.reshape(L3, [-1, W4.get_shape().as_list()[0]])

2. object.reshape

mnist.test.images.reshape(-1, 28, 28, 1)


所有reshape函数中,关键的是shape这个参数

下以tf.reshape为例介绍shape参数对结果的影响

tf.reshape(tensor, shape, name=None)

# tensor 't' is [1, 2, 3, 4, 5, 6, 7, 8, 9]# tensor 't' has shape [9]reshape(t, [3, 3]) ==> [[1, 2, 3],                        [4, 5, 6],                        [7, 8, 9]]# tensor 't' is [[[1, 1], [2, 2]],#                [[3, 3], [4, 4]]]# tensor 't' has shape [2, 2, 2]reshape(t, [2, 4]) ==> [[1, 1, 2, 2],                        [3, 3, 4, 4]]# tensor 't' is [[[1, 1, 1],#                 [2, 2, 2]],#                [[3, 3, 3],#                 [4, 4, 4]],#                [[5, 5, 5],#                 [6, 6, 6]]]# tensor 't' has shape [3, 2, 3]# pass '[-1]' to flatten 't'reshape(t, [-1]) ==> [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6]# -1 can also be used to infer the shape# -1 is inferred to be 9:reshape(t, [2, -1]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3],                         [4, 4, 4, 5, 5, 5, 6, 6, 6]]# -1 is inferred to be 2:reshape(t, [-1, 9]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3],                         [4, 4, 4, 5, 5, 5, 6, 6, 6]]# -1 is inferred to be 3:reshape(t, [ 2, -1, 3]) ==> [[[1, 1, 1],                              [2, 2, 2],                              [3, 3, 3]],                             [[4, 4, 4],                              [5, 5, 5],                              [6, 6, 6]]]# tensor 't' is [7]# shape `[]` reshapes to a scalarreshape(t, []) ==> 7

0 0