TensoFlow的tf.reshape()

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

  函數的做用是將tensor變換爲參數shape形式,其中的shape爲一個列表形式,特殊的是列表能夠實現逆序的遍歷,即list(-1):-1所表明的含義是咱們不用親自去指定這一維的大小,函數會自動進行計算,可是列表中只能存在一個-1。(若是存在多個-1,就是一個存在多解的方程)python

>>>import numpy as np
>>>a = np.array([[1,2,3],[4,5,6]])
>>>np.reshape(a,(3,-1))   #總共有6個元素,shape是(3,-1),行是3,列是自動計算的,即6/3=2,所以是3行2列
array([[1, 2],
       [3, 4],
       [5, 6]])
>>> np.reshape(a,(1,-1))   ##總共有6個元素,shape是(1,-1),行是1,列是自動計算的,即6/1=6,所以是1行6列
array([[1, 2, 3, 4, 5, 6]])
>>> np.reshape(a,(6,-1))   ##總共有6個元素,shape是(6,-1),行是6,列是自動計算的,即6/6=1,所以是6行1列
array([[1], 
       [2], 
       [3], 
       [4], 
       [5],
       [6]])
>>> np.reshape(a,(-1,6))   ##總共有6個元素,shape是(-1,6),列是6,行是自動計算的,即6/6=1,所以是1行6列
array([[1],[2],[3],[4],[5],[6]])

備註:TensorFlow環境下測試運行的函數

相關文章
相關標籤/搜索