用法:
reshape(*shape, **kwargs)
- shape:(
tuple of int
, or
n ints
) -新形狀不應更改數組大小,即np.prod(new_shape)
應等於np.prod(self.shape)
。形狀的某些維度可以從集合 {0, -1, -2, -3, -4} 中獲取特殊值。每一個的意義解釋如下:0
將此維度從輸入複製到輸出形狀。 例子:- input shape = (2,3,4), shape = (4,0,2), output shape = (4,3,2) - input shape = (2,3,4), shape = (2,0,0), output shape = (2,3,4)
-1
通過使用輸入維度的剩餘部分來推斷輸出形狀的維度,保持新數組的大小與輸入數組的大小相同。最多一維形狀可以是-1。 例子:- input shape = (2,3,4), shape = (6,1,-1), output shape = (6,1,4) - input shape = (2,3,4), shape = (3,-1,8), output shape = (3,1,8) - input shape = (2,3,4), shape=(-1,), output shape = (24,)
-2
將輸入尺寸的所有/剩餘部分複製到輸出形狀。 例子:- input shape = (2,3,4), shape = (-2,), output shape = (2,3,4) - input shape = (2,3,4), shape = (2,-2), output shape = (2,3,4) - input shape = (2,3,4), shape = (-2,1,1), output shape = (2,3,4,1,1)
-3
使用輸入形狀的兩個連續維度的乘積作為輸出維度。 例子:- input shape = (2,3,4), shape = (-3,4), output shape = (6,4) - input shape = (2,3,4,5), shape = (-3,-3), output shape = (6,20) - input shape = (2,3,4), shape = (0,-3), output shape = (2,12) - input shape = (2,3,4), shape = (-3,-2), output shape = (6,4)
-4
將輸入的一維拆分為在形狀 -4 之後傳遞的二維(可以包含 -1)。 例子:- input shape = (2,3,4), shape = (-4,1,2,-2), output shape =(1,2,3,4) - input shape = (2,3,4), shape = (2,-4,-1,3,-2), output shape = (2,1,3,4)
- 如果參數
reverse
設置為 1,則從右到左推斷特殊值。 例子:- without reverse=1, for input shape = (10,5,4), shape = (-1,0), output shape would be (40,5). - with reverse=1, output shape will be (50,4).
- reverse:(
bool
,
default False
) - 如果為 true,則從右到左推斷特殊值。僅支持作為關鍵字參數。
- shape:(
具有所需形狀的數組,與該數組共享數據。
參數:
返回:
返回類型:
返回一個看法在不改變任何數據的情況下,使用新形狀的這個數組。
例子:
>>> x = mx.nd.arange(0,6).reshape(2,3) >>> x.asnumpy() array([[ 0., 1., 2.], [ 3., 4., 5.]], dtype=float32) >>> y = x.reshape(3,2) >>> y.asnumpy() array([[ 0., 1.], [ 2., 3.], [ 4., 5.]], dtype=float32) >>> y = x.reshape(3,-1) >>> y.asnumpy() array([[ 0., 1.], [ 2., 3.], [ 4., 5.]], dtype=float32) >>> y = x.reshape(3,2) >>> y.asnumpy() array([[ 0., 1.], [ 2., 3.], [ 4., 5.]], dtype=float32) >>> y = x.reshape(-3) >>> y.asnumpy() array([ 0. 1. 2. 3. 4. 5.], dtype=float32) >>> y[:] = -1 >>> x.asnumpy() array([[-1., -1., -1.], [-1., -1., -1.]], dtype=float32)
相關用法
- Python mxnet.ndarray.NDArray.ndim用法及代碼示例
- Python mxnet.ndarray.NDArray.ctx用法及代碼示例
- Python mxnet.ndarray.NDArray.size用法及代碼示例
- Python mxnet.ndarray.NDArray.copy用法及代碼示例
- Python mxnet.ndarray.NDArray.to_dlpack_for_write用法及代碼示例
- Python mxnet.ndarray.NDArray.shape用法及代碼示例
- Python mxnet.ndarray.NDArray.flatten用法及代碼示例
- Python mxnet.ndarray.NDArray.to_dlpack_for_read用法及代碼示例
- Python mxnet.ndarray.NDArray.slice_assign_scalar用法及代碼示例
- Python mxnet.ndarray.NDArray.slice_assign用法及代碼示例
- Python mxnet.ndarray.NDArray.broadcast_to用法及代碼示例
- Python mxnet.ndarray.NDArray.astype用法及代碼示例
- Python mxnet.ndarray.NDArray.expand_dims用法及代碼示例
- Python mxnet.ndarray.NDArray.broadcast_like用法及代碼示例
- Python mxnet.ndarray.NDArray.copyto用法及代碼示例
- Python mxnet.ndarray.NDArray.dtype用法及代碼示例
- Python mxnet.ndarray.NDArray.as_in_context用法及代碼示例
- Python mxnet.ndarray.NDArray.asscalar用法及代碼示例
- Python mxnet.ndarray.NDArray.T用法及代碼示例
- Python mxnet.ndarray.NDArray.asnumpy用法及代碼示例
注:本文由純淨天空篩選整理自apache.org大神的英文原創作品 mxnet.ndarray.NDArray.reshape。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。