主要参数:
1.in_channels (python:int) – Number of channels in the input imag
2.out_channels (python:int) – Number of channels produced by the convolution
3.kernel_size (python:int or tuple) – Size of the convolving kernel
4.stride (python:int or tuple, optional) – Stride of the convolution. Default: 1
5.padding (python:int or tuple, optional) – Zero-padding added to both sides of the input. Default: 0
6.bias (bool, optional) – If True, adds a learnable bias to the output. Default: True
X = torch.rand(4, 2, 3, 5)
print(X.shape)
conv2d = nn.Conv2d(in_channels=2, out_channels=3, kernel_size=(3, 5), stride=1, padding=(1, 2))
Y = conv2d(X)
print('Y.shape: ', Y.shape)
print('weight.shape: ', conv2d.weight.shape)
print('bias.shape: ', conv2d.bias.shape)
池化层的实现
主要参数:
1.kernel_size – the size of the window to take a max over
2.stride – the stride of the window. Default value is kernel_size
3.padding – implicit zero padding to be added on both sides
X = torch.arange(32, dtype=torch.float32).view(1, 2, 4, 4)
pool2d = nn.MaxPool2d(kernel_size=3, padding=1, stride=(2, 1))
Y = pool2d(X)
print(X)
print(Y)