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
🌐
GitHub
github.com › pytorch › pytorch › issues › 7993
np.repeat vs torch.repeat · Issue #7993 · pytorch/pytorch
May 31, 2018 - This was unexpected to me. This may be unexpected to other people. For me, this failed silently in my model, welp. numpy.repeat [1, 2, 3] → [1, 1, 2, 2, 3, 3] torch.repeat [1, 2, 3] → [1, 2, 3, 1, 2, 3]
Author: pytorch
🌐
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]]])
🌐
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]]])
🌐
PyTorch Forums
discuss.pytorch.org › t › how-to-do-repeat-operation-like-numpy-repeat-in-pytorch-on-variable-like-tensor › 1005
How to do repeat operation like numpy.repeat in pytorch on Variable like tensor? - PyTorch Forums
March 11, 2017 - Assume that I have got a tensor like below: var = Variable(torch.randn(1,7,7)) I would like to know, how can I copy this variable and make a new variable with size 512*7*7. Actually the dimension 0 is the number of c…
🌐
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]]) …
Find elsewhere
🌐
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
🌐
GeeksforGeeks
geeksforgeeks.org › deep learning › repeating-tensors-in-a-specific-new-dimension-in-pytorch
Repeating Tensors in a Specific New Dimension in PyTorch - GeeksforGeeks
July 23, 2025 - Comparison Between torch. repeat() and torch. expand() Tensor repetition involves duplicating the elements of a tensor along one or more dimensions. This operation is analogous to the concept of tiling in NumPy and can be used to expand the ...
🌐
EDUCBA
educba.com › home › software development › software development tutorials › pytorch tutorial › pytorch repeat
PyTorch repeat | How to repeat new dimension in PyTorch?
April 7, 2023 - In the above syntax, we use repeat function as shown, here specified torch means the actual variable name of tensor and specified dimension means the required dimension that we need to change.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
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...