22.02.2024 Views

Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Output

tensor([[[[9., 5., 0., 7.],

[0., 2., 4., 6.],

[7., 6., 6., 8.],

[3., 8., 5., 1.]]]])

As expected, we got the same result shown in the previous section. No surprises

there.

Now, let’s turn our attention to PyTorch’s convolution module, nn.Conv2d. It has

many arguments; let’s focus on the first four of them:

• in_channels: number of channels of the input image

• out_channels: number of channels produced by the convolution

• kernel_size: size of the (square) convolution filter / kernel

• stride: the size of the movement of the selected region

There are a couple of things to notice here. First, there is no argument for the

kernel / filter itself, there is only a kernel_size argument.

The actual filter, that is, the square matrix used to perform

element-wise multiplication, is learned by the module.

Second, it is possible to produce multiple channels as output. It simply means the

module is going to learn multiple filters. Each filter is going to produce a different

result, which is being called a channel here.

So far, we’ve been using a single-channel image as input, and applying one filter

(size three by three) to it, moving one pixel at a time, resulting in one output per

channel. Let’s do it in code:

conv = nn.Conv2d(

in_channels=1, out_channels=1, kernel_size=3, stride=1

)

conv(image)

Convolutions | 353

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!