torch.Tensor.repeat
torch.Tensor.repeat(*sizes) → Tensor
Repeats this tensor along the specified dimensions.
- Unlike expand(), this function copies the tensor’s data.
- repeat() behaves differently from numpy.repeat, but is more similar to numpy.tile.
- For the operator similar to numpy.repeat, see torch.repeat_interleave().
Parameters
- sizes (torch.Size or int...) – The number of times to repeat this tensor along each dimension
>>> x = torch.tensor([1, 2, 3])
>>> x.repeat(4, 2)
tensor([[ 1, 2, 3, 1, 2, 3],
[ 1, 2, 3, 1, 2, 3],
[ 1, 2, 3, 1, 2, 3],
[ 1, 2, 3, 1, 2, 3]])
>>> x.repeat(4, 2, 1).size()
torch.Size([4, 2, 3])
个人(仅仅是猜测,还没有完全验证):具体如何复制,可以根据参数sizes,从后面的维度向前面的维度,将x进行复制,也就是首先从后往前,从参数sizes得到x的维度个维度,然后根据得到的这个元组的各个分量复制x,整个过程完成后,再每次从后向前参数sizes,每次增加一个维度,复制x,
x = torch.tensor([[1,2,3,4],[99,88,77,66]])

y = x.repeat((5,1,1))

浙公网安备 33010602011771号