mindspore.mint.reshape
- mindspore.mint.reshape(input, shape)[源代码]
基于给定的 shape ,对输入Tensor进行重新排列。
shape 最多只能有一个-1,在这种情况下,它可以从剩余的维度和输入的元素个数中推断出来。
- 参数:
input (Tensor) - Tensor的 shape 为
。shape (Union[tuple[int], list[int], Tensor[int]]) - 如果 shape 是list或者tuple,其元素需为整数, 并且只支持常量值。如
。如果 shape 是张量,则其数据类型为int32或者int64,并且只支持一维张量。 兼容支持list和Tensor类型输入,但是不推荐使用。
- 返回:
Tensor,若给定的 shape 中不包含-1, 则输出 shape 为
。若给定的 shape 中第 k 个位置为-1,则输出 shape 为 。- 异常:
ValueError - 给定的 shape 中包含一个以上的-1。
ValueError - 给定的 shape 中包含小于-1的元素。
ValueError - 给定的 shape 中不包含-1的场景,各元素的乘积不等于输入Tensor的 shape 的乘积,
,(即与输入的数组大小不匹配)。 或者给定的 shape 中包含-1的场景,除-1外元素的乘积无法整除输入Tensor的 shape 的乘积 。
- 支持平台:
Ascend
样例:
>>> import mindspore >>> import numpy as np >>> from mindspore import Tensor, mint >>> input = Tensor(np.array([[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]]), mindspore.float32) >>> output = mint.reshape(input, (3, 2)) >>> print(output) [[-0.1 0.3] [ 3.6 0.4] [ 0.5 -3.2]]