Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to repeat tensor in a specific new dimension in PyTorch

Tags:

repeat

pytorch

If I have a tensor A which has shape [M, N], I want to repeat the tensor K times so that the result B has shape [M, K, N] and each slice B[:, k, :] should has the same data as A. Which is the best practice without a for loop. K might be in other dimension.

torch.repeat_interleave() and tensor.repeat() does not seem to work. Or I am using it in a wrong way.

like image 428
River Avatar asked Sep 11 '19 20:09

River


People also ask

How do I add another dimension to tensor PyTorch?

All you have to do is rearrange the colons and the None (s). Notice that we have to specify the first two dimensions with colons and then add a None to the end. When you append dimensions to the end, the colons are required. And naturally, this trick works regardless of where you want to insert the dimension.

What is squeeze and Unsqueeze in PyTorch?

squeeze(input). It squeezes (removes) the size 1 and returns a tensor with all other dimensions of the input tensor. Compute torch. unsqueeze(input, dim). It inserts a new dimension of size 1 at the given dim and returns the tensor.

What is Item () PyTorch?

item () → number. Returns the value of this tensor as a standard Python number. This only works for tensors with one element. For other cases, see tolist() . This operation is not differentiable.


Video Answer


3 Answers

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.

like image 100
jodag Avatar answered Oct 29 '22 00:10

jodag


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.

like image 32
Alleo Avatar answered Oct 29 '22 00:10

Alleo


Adding to the answer provided by @Alleo. You can use following Einops function.

einops.repeat(example_tensor, 'b h w -> (repeat b) h w', repeat=b)

Where b is the number of times you want your tensor to be repeated and h, w the additional dimensions to the tensor.

Example -

example_tensor.shape -> torch.Size([1, 40, 50]) 
repeated_tensor = einops.repeat(example_tensor, 'b h w -> (repeat b) h w', repeat=8)
repeated_tensor.shape -> torch.Size([8, 40, 50]) 

More examples here - https://einops.rocks/api/repeat/

like image 28
Ashish Alex Avatar answered Oct 29 '22 00:10

Ashish Alex