当前位置:网站首页>torch. nn. functional. pad

torch. nn. functional. pad

2022-06-11 08:11:00 Interval


effect

For a tensor Fill in . The most typical is the picture , Turned out to be 2*2 Of , Now I want to be 3*3 Of , Then you need to fill , There are many choices at this time , For example, whether to fill in the original top right or bottom left ? Or the top left ? wait .

This function can be used to implement these functions .

torch.nn.functional.pad(input, pad, mode='constant', value=0.0)

We only care about the second parameter pad, Don't worry about anything else .pad It's a tuple , The format is as follows :

(1,1) In the last dimension , Fill in the front 1 individual 0, Back filling 1 individual 0
(1,0) In the last dimension , Fill in the front 1 individual 0, No fill after .
(1,1,1,1) Indicates that the last two dimensions should be filled , And fill up, down, left and right 1 individual 0.
.....

below , According to the above 3 Cases for actual combat :


actual combat

import torch
import torch.nn.functional as tnf
x = torch.tensor([[1, 2],[ 3,4]])
print(x)
print(tnf.pad(x,(1,1)))# Case study 1
print(tnf.pad(x,(1,0)))# Case study 2
print(tnf.pad(x,(1,1,1,1)))# Case study 3

 Insert picture description here
That started 2*2 How to fill in 3*3 The picture of ? Suppose we fill in the right side and the bottom side 1 individual 0, So that's it :

print(x)
print(tnf.pad(x,(0,1,0,1)))

 Insert picture description here

原网站

版权声明
本文为[Interval]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020514399394.html