1 回答

紫衣仙女
TA贡献1839条经验 获得超15个赞
这样做的一种方法是
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
添加回答
举报
0/150
提交
取消