tensorflow的reshape操作tf.reshape()

匿名 (未验证) 提交于 2019-12-03 00:33:02



numpy.reshape


reshape()的括号中所包含的参数有哪些呢?常见的写法有tf.reshape((28,28)):

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



将矩阵t变换为一维矩阵,然后再对矩阵的形式进行更改就好了,具体的流程如下:

reshape(t,shape) =>reshape(t,[-1]) =>reshape(t,shape)

实际操作中,有如下效果:我创建了一个一维的数组

>>>import numpy as np >>>a= np.array([1,2,3,4,5,6,7,8]) >>>a array([1,2,3,4,5,6,7,8]) >>>

使用reshape()方法来更改数组的形状,使得数组成为一个二维的数组:(数组中元素的个数是2×4=8)

>>>d = a.reshape((2,4)) >>>d array([[1, 2, 3, 4],        [5, 6, 7, 8]])

进一步提升,可以得到一个三维的数组f:(注意数组中元素的个数时2×2×2=8)

>>>f = a.reshape((2,2,2)) >>>f array([[[1, 2],         [3, 4]],         [[5, 6],         [7, 8]]])


(元素的个数是2×2=4,所以会报错)

>>> e = a.shape((2,2)) Traceback (most recent call last):   File "<stdin>", line 1, in <module> TypeError: 'tuple' object is not callable

-1 的应用:-1 表示不知道该填什么数字合适的情况下,可以选择,由python通过a和其他的值3推测出来,比如,这里的a 是二维的数组,数组中共有6个元素,当使用reshape()时,6/3=2,所以形成的是3行2列的二维数组,可以看出,利用reshape进行数组形状的转换时,一定要满足(x,y)中x×y=数组的个数。

>>>a = np.array([[1,2,3],[4,5,6]]) >>>np.reshape(a,(3,-1))  array([[1, 2],        [3, 4],        [5, 6]]) >>> np.reshape(a,(1,-1)) array([[1, 2, 3, 4, 5, 6]]) >>> np.reshape(a,(6,-1)) array([[1],        [2],        [3],        [4],        [5],        [6]]) >>> np.reshape(a,(-1,1)) array([[1],        [2],        [3],        [4],        [5],        [6]])

下面是两张2×3大小的图片(不知道有几张图片可以用-1代替),如何把所有二维照片给转换成一维的,请看以下三维的数组:

>>>image = np.array([[[1,2,3], [4,5,6]], [[1,1,1], [1,1,1]]]) >>>image.shape (2,2,3) >>>image.reshape((-1,6)) array([[1, 2, 3, 4, 5, 6],        [1, 1, 1, 1, 1, 1]]) >>> a = image.reshape((-1,6)) >>> a.reshape((-1,12)) array([[1, 2, 3, 4, 5, 6, 1, 1, 1, 1, 1, 1]]) a.reshape((12,-1)) array([[1],        [2],        [3],        [4],        [5],        [6],        [1],        [1],        [1],        [1],        [1],        [1]]) >>> a.reshape([-1]) array([1, 2, 3, 4, 5, 6, 1, 1, 1, 1, 1, 1])

通过reshape生成的新的形状的数组和原始数组共用一个内存,所以一旦更改一个数组的元素,另一个数组也将会发生改变。

>>>a[1] = 100 >>>a array([  1, 100,   3,   4,   5,   6,   7,   8]) >>> d array([[  1, 100,   3,   4],        [  5,   6,   7,   8]])

最后再给大家呈现一下官方给出的例子:

# 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 scalar reshape(t, []) ==> 7
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!