How do I pad a tensor of shape [71 32 1]
with zero vectors to make it [100 32 1]
?
RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 2. Got 32 and 71 in dimension 0 at /pytorch/aten/src/THC/generic/THCTensorMath.cu:87
I tried by concatenating a padding vector of zeros of shape [29 32 1]
. I get the error above. I try with a padding vector of zeros of shape [29 32 1]
, I still get an error.
How to append to a torch tensor? This is achieved by using the expand function which will return a new view of the tensor with its dimensions expanded to larger size. It is important to do because at some time if we have two tensors one is of smaller dimension and another is of larger one.
torch. cat(tensors, dim=0, *, out=None) → Tensor. Concatenates the given sequence of seq tensors in the given dimension. All tensors must either have the same shape (except in the concatenating dimension) or be empty. torch.cat() can be seen as an inverse operation for torch.
PyTorch torch. stack() method joins (concatenates) a sequence of tensors (two or more tensors) along a new dimension. It inserts new dimension and concatenates the tensors along that dimension. This method joins the tensors with the same dimensions and shape.
In order to help you better, you need to post the code that caused the error, without it we are just guessing here...
Guessing from the error message you got:
Sizes of tensors must match except in dimension 2
pytorch tries to concat along the 2nd dimension, whereas you try to concat along the first.
Got 32 and 71 in dimension 0
It seems like the dimensions of the tensor you want to concat are not as you expect, you have one with size (72, ...)
while the other is (32, ...)
.
You need to check this as well.
Here's an example of concat
import torch
x = torch.rand((71, 32, 1))
# x.shape = torch.Size([71, 32, 1])
px = torch.cat((torch.zeros(29, 32, 1, dtype=x.dtype, device=x.device), x), dim=0)
# px.shape = torch.Size([100, 32, 1])
Alternatively, you can use functional.pad
:
from torch.nn import functional as F
px = F.pad(x, (0, 0, 0, 0, 29, 0))
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