I figured it out:
h, w = 4, 10
repeats = 2
my_tensor = torch.randint(0, 10, [h, w])
print("my_tensor:", my_tensor)
repeated_1 = my_tensor.repeat_interleave(repeats , -2)
print("repeated_1:", repeated_1)
repeated_2 = my_tensor.view(h, 1, w).expand(h, repeats, w).contiguous().view(-1, w)
print("re… Answer from ZimoNitrome on discuss.pytorch.org
PyTorch Forums
discuss.pytorch.org › t › repeat-interleave-without-cloning › 136691
Repeat_interleave without cloning - PyTorch Forums
November 12, 2021 - Is there an equivalent method to torch.repeat_interleave() that does not copy the tensor? Or operations that will achieve the same result?
PyTorch Forums
discuss.pytorch.org › autograd
Torch.repeat and torch.expand which to use? - autograd - PyTorch Forums
October 24, 2018 - I have one vector and one 2-D vector. Say embed1= [1, 2, 3] shape is (3) and embed2=[[1, 2, 3], [4,5,6], [7,8,9]] shape is (3, 3). Now I want to expand embed1 to [[1, 2, 3], [1, 2, 3], [1, 2, 3]] so that I can do torch.c…
Call: +917738666252
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
PyTorch Forums
discuss.pytorch.org › uncategorized
Repeat examples along batch dimension - Uncategorized - PyTorch Forums
February 3, 2019 - Hi, I’m trying to repeat tensors along the batch dimension. Ex) We have a batch (8 x 3 x 224 x 224) where its size is 8 and let’s say it is called as [a, b, c, d, e, f, g, h], and each alphabet denotes an example in the batch. Then, I want to repeat each of them three times, resulting in EXACTLY FOLLOWING ORDER [a, a, a, b, b, b, c, c, c, d, d, d, …, h, h, h]. Same elements should come in succession.
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 - It is more memory-intensive but necessary when you need actual repeated data. torch.expand(): Does not duplicate data but creates a view with the expanded shape. This is more efficient in terms of memory usage but cannot be used if you need actual duplicates of the tensor's values. ... You need real data duplication for subsequent operations. The expanded dimensions require independent values in memory. ... You need to broadcast a tensor without memory overhead.
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 - You might be wondering, “Why would I need torch.expand when I already have repeat?" Here’s the deal: expand is memory-efficient for certain operations, especially when you don’t need the tensor physically duplicated but only need it to behave as if it were. ... In many scenarios, expand just modifies the view of the tensor, allowing it to virtually extend along a dimension without additional memory allocation.
Top answer 1 of 5
93
tensor.repeat should suit your needs but you need to insert a unitary dimension first. For this we could use either tensor.unsqueeze or tensor.reshape. Since unsqueeze is specifically defined to insert a unitary dimension we will use that.
B = A.unsqueeze(1).repeat(1, K, 1)
Code Description A.unsqueeze(1) turns A from an [M, N] to [M, 1, N] and .repeat(1, K, 1) repeats the tensor K times along the second dimension.
2 of 5
12
Einops provides repeat function
import einops
einops.repeat(x, 'm n -> m k n', k=K)
repeat can add arbitrary number of axes in any order and reshuffle existing axes at the same time.
DEV Community
dev.to › hyperkai › repeat-in-pytorch-1ggk
repeat in PyTorch - DEV Community
November 5, 2024 - repeats= mustn't be used for the one or more dimensions without a tuple, list or size(). import torch my_tensor = torch.tensor([7, 4, 2]) my_tensor.repeat(repeats=(0,)) my_tensor.repeat(0) my_tensor.repeat(repeats=torch.tensor([]).size()) # tensor([], dtype=torch.int64) my_tensor.repeat(repeats=(1,)) my_tensor.repeat(1) my_tensor.repeat(repeats=torch.tensor([5]).size()) # tensor([7, 4, 2]) my_tensor.repeat(repeats=(2,)) my_tensor.repeat(2) my_tensor.repeat(repeats=torch.tensor([5, 8]).size()) # tensor([7, 4, 2, 7, 4, 2]) my_tensor.repeat(repeats=(3,)) my_tensor.repeat(3) my_tensor.repeat(repeats=torch.tensor([5, 8, 1]).size()) # tensor([7, 4, 2, 7, 4, 2, 7, 4, 2]) etc.
PyTorch
docs.pytorch.org › docs › stable › generated › torch.Tensor.repeat.html
torch.Tensor.repeat — PyTorch 2.14 documentation
Continue to ../../2.14/generated/torch.Tensor.repeat.html
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]]) …
PyTorch Forums
discuss.pytorch.org › memory format
Avoiding data copy when using array indexing - Memory Format - PyTorch Forums
June 23, 2023 - In the following toy example: import torch data = torch.randn(10, 1024) indices = torch.tensor([0, 1, 7, 0]) selected_data = data[indices] Is there a way to avoid a copy by having selected_data be a view of data and share the same underlying storage? In my real case, as in this toy example, the indices in indices may have repeating and non following elements.
Pytorch
docs.pytorch.wiki › en › generated › torch.repeat_interleave.html
torch.repeat_interleave — PyTorch 1.11.0 documentation
Repeated tensor which has the same shape as input, except along the given axis. ... >>> x = torch.tensor([1, 2, 3]) >>> x.repeat_interleave(2) tensor([1, 1, 2, 2, 3, 3]) >>> y = torch.tensor([[1, 2], [3, 4]]) >>> torch.repeat_interleave(y, 2) tensor([1, 1, 2, 2, 3, 3, 4, 4]) >>> torch.repeat_interleave(y, 3, dim=1) tensor([[1, 1, 1, 2, 2, 2], [3, 3, 3, 4, 4, 4]]) >>> torch.repeat_interleave(y, torch.tensor([1, 2]), dim=0) tensor([[1, 2], [3, 4], [3, 4]]) >>> torch.repeat_interleave(y, torch.tensor([1, 2]), dim=0, output_size=3) tensor([[1, 2], [3, 4], [3, 4]])
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.
PyTorch Forums
discuss.pytorch.org › uncategorized
How to tile a tensor? - Uncategorized - PyTorch Forums
February 20, 2018 - If I have a tensor like: z = torch.FloatTensor([[1,2,3],[4,5,6]]) 1 2 3 4 5 6 How might I turn it into a tensor like: 1 2 3 1 2 3 1 2 3 1 2 3 4 5 6 4 5 6 4 5 6 4 5 6 I imagine that torch.repeat() is somehow in play here. The only solution I have come up with is to do: z.repeat(1,4).view(-1, ...
PyTorch
docs.pytorch.org › docs › stable › generated › torch.repeat_interleave.html
torch.repeat_interleave — PyTorch 2.14 documentation
Continue to ../../2.14/generated/torch.repeat_interleave.html
MindSpore
mindspore.cn › function differences with torch.tensor.repeat
Function Differences with torch.Tensor.repeat | MindSpore 1.8 documentation | MindSpore
For more information, see torch.Tensor.repeat. mindspore.numpy.tile(a, reps) For more information, see mindspore.numpy.tile. MindSpore: Constructs an array by repeating a the number of times given by reps. If reps has length d, a has dimensions a.dim, the rules for repeat operation is: If a.ndim = d: copy a for reps times in the corresponding axis ; If a.ndim < d: a is promoted to be d-dimensional by prepending new axis, and then copied; If a.ndim > d: The reps will be promoted to a.ndim by adding 1 in the front, and then copied.