[英]Pad multiple torch tensor over the last dim
I have multiple torch tensors with the following shapes我有多个具有以下形状的火炬张量
x1 = torch.Size([1, 512, 177])
x2 = torch.Size([1, 512, 250])
x3 = torch.Size([1, 512, 313])
How I can pad all these tensors by 0 over the last dimension, to have a unique shape like ([1, 512, 350]).我如何在最后一个维度上将所有这些张量填充 0,以具有像 ([1, 512, 350]) 这样的独特形状。
What I tried to do is to convert them into NumPy arrays and use these two lines of code:我试图做的是将它们转换为 NumPy 数组并使用这两行代码:
if len(x1) < 350:
ff = np.pad(f, [(0, self.max_len - f.shape[0]), ], mode='constant')
f = ff
But unfortunately, it doesn't affect the last dim and still, the shapes are not equal.但不幸的是,它不会影响最后的暗淡,仍然,形状不相等。 Any help will be appreciated Thanks任何帮助将不胜感激谢谢
You can simply do:你可以简单地做:
import torch.nn.functional as F
x = F.pad(x, (0, self.max_len - x.size(2)), "constant", 0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.