Function Differences with torch.Tensor.repeat
torch.Tensor.repeat
torch.Tensor.repeat(*sizes)
For more information, see torch.Tensor.repeat.
mindspore.numpy.tile
mindspore.numpy.tile(a, reps)
For more information, see mindspore.numpy.tile.
Differences
MindSpore: Constructs an array by repeating
athe number of times given byreps. Ifrepshas lengthd,ahas dimensionsa.dim, the rules for repeat operation is:If
a.ndim=d: copyaforrepstimes in the corresponding axis ;If
a.ndim<d:ais promoted to be d-dimensional by prepending new axis, and then copied;If
a.ndim>d: Therepswill be promoted toa.ndimby adding 1 in the front, and then copied.PyTorch: The length of input args
sizemust be greater than or equal to the dimension of the self tensor, that is, the above third case is not supported.
Code Example
MindSpore:
import mindspore.numpy as np
a = np.array([[0, 2, 1], [3, 4, 5]])
b = np.tile(a, 2)
print(b)
# out:
# [[0 2 1 0 2 1]
# [3 4 5 3 4 5]]
c = np.tile(a, (2, 1))
print(c)
# out:
# [[0 2 1]
# [3 4 5]
# [0 2 1]
# [3 4 5]]
d = np.tile(a, (2, 1, 2))
print(d)
# out
# [[[0 2 1 0 2 1]
# [3 4 5 3 4 5]]
# [[0 2 1 0 2 1]
# [3 4 5 3 4 5]]]
PyTorch:
import torch
a = torch.tensor([[0, 2, 1], [3, 4, 5]])
b = a.repeat(2)
# error:
# RuntimeError: Number of dimensions of repeat dims can not be smaller than number of dimensions of tensor
c = a.repeat(2, 1)
print(c)
# out:
#tensor([[0, 2, 1],
# [3, 4, 5],
# [0, 2, 1],
# [3, 4, 5]])
d = a.repeat(2, 1, 2)
print(d)
# out:
#tensor([[[0, 2, 1, 0, 2, 1],
# [3, 4, 5, 3, 4, 5]],
#
# [[0, 2, 1, 0, 2, 1],
# [3, 4, 5, 3, 4, 5]]])
