在PyTorch张量中,我想从输入中获得如下输出:
我怎样才能在Pytroch实现这个填充呢?
发布于 2018-10-24 11:51:56
这样做的一个方法是
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
,那么当使用torch.zeros
分配它时,您可以显式地将这个dtype和设备设置为result
。
https://stackoverflow.com/questions/52964807
复制相似问题