tensorflow的reshape操作tf.reshape()
在處理影象資料的時候總會遇到輸入影象的維數不符合的情況,此時tensorflow中reshape()就很好的解決了這個問題。
更為詳細的可以參考官方文件說明:
numpy.reshape
reshape()的括號中所包含的引數有哪些呢?常見的寫法有tf.reshape((28,28)):
tf.reshape(tensor,shape,name=None)
函式的作用是將tensor變換為引數shape形式,其中的shape為一個列表形式,特殊的是列表可以實現逆序的遍歷,即list(-1).-1所代表的含義是我們不用親自去指定這一維的大小,函式會自動進行計算,但是列表中只能存在一個-1。(如果存在多個-1,就是一個存在多解的方程)
下面就說一下reshape是如何進行矩陣的變換的,其簡單的流程就是:
將矩陣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