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
🌐
GitHub
github.com › pytorch › pytorch › issues › 43192
backward of torch.repeat slower than for torch.repeat_interleave · Issue #43192 · pytorch/pytorch
August 18, 2020 - 🐛 Bug I noticed a high performance difference when repeating a variable that requires grad. If I repeat the variable with torch.repeat, the backward pass will take 10x more time than when using tor...
Author: pytorch
🌐
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…
🌐
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
🌐
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.
Find elsewhere
🌐
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 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.
Top answer
1 of 1
1

Assuming that the first dimension of a is 1 as in your example, you could do the following:

a = torch.Tensor([[1, 2, 3, 4]])
b_abbreviated = torch.Tensor([[1, 2, 3], [4, 5, 6]])
torch.mm(a.reshape(-1, 2), b_abbreviated).sum(axis=0, keepdim=True)

Here, instead of repeating the rows, you multiply a in chunks, then add them up column-wise to get the same result.


If the first dimension of a is not necessarily 1, you could try the following:

torch.cat(torch.split(torch.mm(a.reshape(-1,2),b_abbreviated), a.shape[0]), dim=1).sum(
dim=0, keepdim=True).reshape(a.shape[0], -1)

Here, you do the following:

  • With torch.mm(a.reshape(-1,2),b_abbreviated, you again split each row of a into chunks of size 2 and stack them one over the other, and then stack each row over the other.
  • With torch.split(torch.mm(a.reshape(-1,2),b_abbreviated), a.shape[0]), these stacks are then separated row-wise, so that each resultant component of the split corresponds to chunks of a single row.
  • With torch.cat(torch.split(torch.mm(a.reshape(-1,2),b_abbreviated), a.shape[0]), dim=1) these stacks are then concatenated column-wise.
  • With .sum(dim=0, keepdim=True), results corresponding to separate chunks of individual rows in a are added up.
  • With .reshape(a.shape[0], -1), rows of a that were concatenated column-wise are again stacked row-wise.

It seems quite slow compared to direct matrix multiplication, which is not surprising, but I have not yet checked in comparison to explicit iteration. There are likely better ways of doing this, will edit if I think of any.

🌐
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]])
🌐
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, ...
🌐
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.