1、reshape的用法,将数组转换成指定的形状
import numpy as np
x = np.array([[1,2,3],[11,12,13,],[22,23,24],[31,32,33]])
>>>x
array([[ 1, 2, 3],
[11, 12, 13],
[22, 23, 24],
[31, 32, 33]])
>>>x.reshape(2,6)
array([[ 1, 2, 3, 11, 12, 13],
[22, 23, 24, 31, 32, 33]])
2、-1参数的意思
2.1 reshape(N, -1)指定N行,列数自动确定
x.reshape(-1)自动生成一列
>>>x.reshape(6,-1)
array([[ 1, 2],
[ 3, 11],
[12, 13],
[22, 23],
[24, 31],
[32, 33]])
>>>x.reshape(-1)
array([ 1, 2, 3, 11, 12, 13, 22, 23, 24, 31, 32, 33])
2.2 reshape(-1, N)生成N列,行数自动确定
>>>x.reshape(-1, 4)
array([[ 1, 2, 3, 11],
[12, 13, 22, 23],
[24, 31, 32, 33]])
2.3 应用
在深度学习中,四维数据(N, C, H, W)分别表示批数、通道数、高度和宽度,如out是二维数据
out = out.reshape(N, H, W, -1)
则其中的C通道数会自动确定