当前位置:网站首页>torch.nn.functional.pad(input, pad, mode=‘constant‘, value=None)记录

torch.nn.functional.pad(input, pad, mode=‘constant‘, value=None)记录

2022-07-07 17:44:00 ODIMAYA

torch.nn.functional.pad该函数用来填充tensor

其中参数pad定义了四个参数,表示对输入矩阵的后两个维度(w,h–与正常的h,w相反)进行扩充:
(左边填充数, 右边填充数, 上边填充数, 下边填充数)
如果仅写两个参数,则填充的是w:
(左边填充数, 右边填充数)
如果写六个参数,则填充的是(w,h,c)三个维度:
(左边填充数, 右边填充数, 上边填充数, 下边填充数,通道填充数1,通道填充数2)

t4d = torch.empty(3, 3, 4, 2)
p1d = (1, 1) # pad last dim by 1 on each side
out = F.pad(t4d, p1d, "constant", 0)  # effectively zero padding
print(out.size())
p2d = (1, 1, 2, 2) # pad last dim by (1, 1) and 2nd to last by (2, 2)
out = F.pad(t4d, p2d, "constant", 0)
print(out.size())
t4d = torch.empty(3, 3, 4, 2)
p3d = (0, 1, 2, 1, 3, 3) # pad by (0, 1), (2, 1), and (3, 3)
out = F.pad(t4d, p3d, "constant", 0)
print(out.size())

注意:
上述经常使用填充数是正数,但实际应用中也可使用负数,来缩小tensor的size,比如:

x = torch.rand((8,3,57,57))

up = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)

xx = up(x)

xx.shape
Out[8]: torch.Size([8, 3, 114, 114])

import torch.nn.functional as F

xxx = F.pad(xx, [0, -1, 0, -1])              

xxx.shape
Out[18]: torch.Size([8, 3, 113, 113])

xxxx = F.pad(xxx,[-2,-2,-3,-3,-1,-1])

xxxx.shape
Out[20]: torch.Size([8, 1, 107, 109])
原网站

版权声明
本文为[ODIMAYA]所创,转载请带上原文链接,感谢
https://blog.csdn.net/ODIMAYA/article/details/125635040