当前位置:网站首页>torch. nn. functional. Pad (input, pad, mode= 'constant', value=none) record

torch. nn. functional. Pad (input, pad, mode= 'constant', value=none) record

2022-07-07 19:57:00 ODIMAYA

torch.nn.functional.pad This function is used to fill tensor

The parameter pad Four parameters are defined , Represents the last two dimensions of the input matrix (w,h– And normal h,w contrary ) To expand :
( Left padding number , Number of padding on the right , Number of top fills , The number of bottom fills )
If you only write two parameters , Then fill in w:
( Left padding number , Number of padding on the right )
If you write six parameters , Then fill in (w,h,c) Three dimensions :
( Left padding number , Number of padding on the right , Number of top fills , The number of bottom fills , Number of channel fills 1, Number of channel fills 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())

Be careful :
The above often used padding number is a positive number , However, negative numbers can also be used in practical applications , To shrink tensor Of size, such as :

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://yzsam.com/2022/188/202207071744385074.html