The nn.conv2d method is generally used to construct the convolution layer in pytorch. In some cases, we need to customize the weight of the convolution kernel, but the convolution parameters in nn.conv2d are not allowed to be customized. In this case, torch.nn.functional.conv2d, f.conv2d for short, can be used
torch.nn.functional.conv2d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1)
F. Conv2d can input by itself and must also require itself to input convolution weight and bias bias. Therefore, construct the convolution kernel parameters you want, and then enter f.conv2d.
The following is an example of building a convolution layer with f.conv2d
Here, a class is written for the network model:
It is worth noting that the data type of weights to be trained for each layer in pytorch is set to nn.parameter instead of tensor or variable. Parameter requires_ Grad is set to true by default, while variable is set to false by default.
Supplement: understanding of convolution parameters in pytorch
kernel_ Size stands for convolution kernel, such as kernel_ Size = 3 or kernel_ size=（3，7）；
stride: it shows the step size of the convolution kernel walking on the pixel level image, as shown in Fig. 2, the step size is 1;
padding: refers to the size of the top, bottom, left and right filling, for example, padding = 0 / 1 / (1, 1) / (1, 3),
Padding = 0, not filled;
Padding = 1 / (1,1) fill 1 grid up, down, left and right respectively;
Padding = (1, 3) fill 2 grids in height (up and down) and 6 grids in width (left and right);
Specifies the upsampling of the output shape
def upsample_add(self,x,y): _,_,H,W = y.size() return F.interpolate(x, size=(H,W), mode="bilinear", align_corners=False) + y
self.upscore2 = nn.ConvTranspose2d( 512, 1, kernel_size=3, stride=2,padding=0, bias=False)
The above is my personal experience. I hope I can give you a reference, and I hope you can support developepper.