Try torch.repeat_interleave() method: https://pytorch.org/docs/stable/torch.html#torch.repeat_interleave
>>> x = torch.tensor([1, 2, 3])
>>> x.repeat_interleave(2)
tensor([1, 1, 2, 2, 3, 3])
Answer from GreMal on Stack Overflow Top answer 1 of 6
22
Try torch.repeat_interleave() method: https://pytorch.org/docs/stable/torch.html#torch.repeat_interleave
>>> x = torch.tensor([1, 2, 3])
>>> x.repeat_interleave(2)
tensor([1, 1, 2, 2, 3, 3])
2 of 6
8
Quoting https://discuss.pytorch.org/t/how-to-tile-a-tensor/13853 -
z = torch.FloatTensor([[1,2,3],[4,5,6],[7,8,9]]) 1 2 3 4 5 6 7 8 9 z.transpose(0,1).repeat(1,3).view(-1, 3).transpose(0,1) 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9
This will give you a intuitive feel of how it works.
MindSpore
mindspore.cn › function differences with torch.tensor.repeat
Function Differences with torch.Tensor.repeat | MindSpore 1.8 documentation | 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]]] ... 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]]])
PyTorch
docs.pytorch.org › docs › stable › generated › torch.Tensor.repeat.html
torch.Tensor.repeat
Continue to ../../2.14/generated/torch.Tensor.repeat.html
MindSpore
mindspore.cn › docs › migration_guide › en › r1.5 › api_mapping › pytorch_diff › npTile.html
Function Differences with torch.Tensor.repeat | MindSpore 1.5 documentation | 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]]] ... 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]]])
GitHub
github.com › pytorch › pytorch › issues › 50013
torch.Tensor.repeat is divergent from np.repeat · Issue #50013 · pytorch/pytorch
January 3, 2021 - high prioritymodule: deprecationmodule: numpyRelated to numpy support, and also numpy compatibility of our operatorsRelated to numpy support, and also numpy compatibility of our operatorstriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate moduleThis issue has been looked at a team member, and triaged and prioritized into an appropriate module ... torch.Tensor.repeat, as its documentation notes, is divergent from np.repeat but similar to np.tile.
GitHub
github.com › wkentaro › pytorch-for-numpy-users
GitHub - wkentaro/pytorch-for-numpy-users: PyTorch for Numpy users. https://pytorch-for-numpy-users.wkentaro.com · GitHub
Numpy · PyTorch · np.put · x.put · x.put_ x = np.array([1, 2, 3]) x.repeat(2) # [1, 1, 2, 2, 3, 3] x = torch.tensor([1, 2, 3]) x.repeat_interleave(2) # [1, 1, 2, 2, 3, 3] x.repeat(2) # [1, 2, 3, 1, 2, 3] x.repeat(2).reshape(2, -1).transpose(1, 0).reshape(-1) # [1, 1, 2, 2, 3, 3] np.tile(x, (3, 2)) x.repeat(3, 2) x = np.array([[0, 1], [2, 3], [4, 5]]) idxs = np.array([0, 2]) np.choose(idxs, x) # [0, 5] x = torch.tensor([[0, 1], [2, 3], [4, 5]]) idxs = torch.tensor([0, 2]) x[idxs, torch.arange(x.shape[1])] # [0, 5] torch.gather(x, 0, idxs[None, :])[0] # [0, 5] np.sort ·
Starred by 705 users
Forked by 87 users
Languages: HTML 81.3% | Python 18.7%
PyTorch Forums
discuss.pytorch.org › t › torch-repeat-different-per-row-like-np-repeat › 41323
Torch.repeat different per row, like np.repeat - PyTorch Forums
March 31, 2019 - Hi everybody, Is there a current API that’s behave similarly to numpy, where we can repeat each row individually ? x = np.array([[1,2],[3,4]]) np.repeat(x, [1, 2], axis=0) array([[1, 2], [3, 4], [3, 4]]) …
Medium
medium.com › biased-algorithms › how-to-repeat-a-tensor-in-a-specific-new-dimension-in-pytorch-7519f4172065
How to Repeat a Tensor in a Specific New Dimension in PyTorch | by Amit Yadav | Biased-Algorithms | Medium
January 19, 2025 - In some cases, torch.tile() offers a cleaner, often more efficient alternative, especially when readability or large repetitions are involved. ... While both functions create repeated tensors, tile differs subtly. It aligns well with NumPy-like syntax and is sometimes more intuitive for extending arrays over larger dimensions.
PyTorch
docs.pytorch.org › reference api › torch.tensor › torch.tensor.repeat
torch.Tensor.repeat — PyTorch 2.12 documentation
January 1, 2023 - 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: repeat (torch.Size, int..., tuple of int or list of int) – The number of times to repeat this tensor along each dimension ·
GitHub
github.com › pytorch › pytorch › pull › 18395 › files
Add numpy like repeat as torch.repeat_interleave by zasdfgbnm · Pull Request #18395 · pytorch/pytorch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters ... Repeat elements of a tensor. ... This is different from :func:`torch.repeat` but similar to `numpy.repeat`.
Author: pytorch
PyTorch
docs.pytorch.org › docs › stable › generated › torch.repeat_interleave.html
torch.repeat_interleave
Continue to ../../2.14/generated/torch.repeat_interleave.html
Call: +917738666252
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
Rdrr.io
rdrr.io › cran › torch › man › torch_repeat_interleave.html
torch_repeat_interleave: Repeat_interleave in torch: Tensors and Neural Networks with 'GPU' Acceleration
November 5, 2025 - This is different from `torch_Tensor.repeat` but similar to `numpy.repeat`.
Programmersought
programmersought.com › article › 33611152400
The difference between numpy repeat and torch repeat - Programmer Sought
>>> w=np.random.randn(1,2) >>> w=torch.tensor(w) >>> w tensor([[ 0.8654, -2.3015]], dtype=torch.float64) >>> w.repeat(3,1) tensor([[ 0.8654, -2.3015], [ 0.8654, -2.3015], [ 0.8654, -2.3015]], dtype=torch.float64) ... tile tile(array, number of repetitions) tile(array, (number of repetitions of the first dimension, number of repetitions of the second dimension, ..., number of repetitions of the nth dimension)) exam... repeat Create Observables that can emit specific items multiple times 1 repeat Set the number of repetitions Repeat 5 times Repeat output 1 time in 5 seconds, 3 repeatUntil set repeat, until the...