TensoFlow的tf.reshape()

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

  函数的作用是将tensor变换为参数shape形式,其中的shape为一个列表形式,特殊的是列表可以实现逆序的遍历,即list(-1):-1所代表的含义是我们不用亲自去指定这一维的大小,函数会自动进行计算,但是列表中只能存在一个-1。(如果存在多个-1,就是一个存在多解的方程)

>>>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环境下测试运行的

原文地址:https://www.cnblogs.com/chamie/p/8763393.html