在 Pytorch 中填充

在 PyTorch 张量中,我想从输入中获取输出如下:

http://img.mukewang.com/61011dcc00015eab12650940.jpg

如何在 Pytroch 中实现这种填充?


12345678_0001
浏览 179回答 1
1回答

紫衣仙女

这样做的一种方法是def my_odd_padding(list_of_2d_tensors, pad_value):  # get the sizes of the matrices  hs = [t_.shape[0] for t_ in list_of_2d_tensors]  ws = [t_.shape[1] for t_ in list_of_2d_tensors]  # allocate space for output  result = torch.zeros(sum(hs), sum(ws))  result.add_(pad_value)  fh = 0  fw = 0  for i, t_ in enumerate(list_of_2d_tensors):    result[fh:fh+hs[i], fw:fw+ws[i]] = t_    fh += hs[i]    fw += ws[i]  return result 假设所有张量list_of_2d_tensors都相同dtype并且相同,device您可以result在使用分配时显式设置此 dtype 和设备torch.zeros
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python