How to bring different tensors to the same size?

Hi,

I’m trying to prepare the input_data for my project but my tensors are of different sizes, for example: [1, 1256], [1, 6497], [1, 58672], etc. I want to have them all of the size [1, 58672]. Can you please tell me how can I do it?

Thanks in advance :slight_smile:

Depending how you would like to increase this dimension, you could use

  • F.pad and pad the dimension to the desired shape
  • create another tensor in the “missing” shape and use torch.cat((x, other), dim=1) to concatenate them
  • concatenate the tensor to itself and pad the rest

Let me know, if that would work or if you would like to apply another approach. :slight_smile:

Hi ptrblck, thank you for your answer. I’m actually trying to create a Dataset for my audio project but unfortunately I’m still a beginner and I have to learn a lot. I have tried this site: https://pytorch.org/tutorials/beginner/data_loading_tutorial.html but it’s still not working. Can you please suggest me a website where I can learn something more, for example F.pad and torch.cat?

The docs are generally a good starting point.
This code should work for the padding approach:

target_len = 58672
x = torch.randn(1, 1256)
x = F.pad(x, (target_len - x.size(1), 0))
print(x.shape)
>  torch.Size([1, 58672])
1 Like

Thank you very much, it really helped me :slight_smile: