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.
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.
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.
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.
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.
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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With