用法:
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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。