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

Parameter containing:

tensor([[[[ 0.0009, -0.1240, -0.0231],

[-0.2259, -0.2288, -0.1945],

[-0.1141, -0.2631, 0.2795]]],

[[[-0.0662, 0.2868, 0.1039],

[-0.2823, 0.2307, -0.0917],

[-0.1278, -0.2767, -0.3314]]]], requires_grad=True)

See? There are two filters represented by three-by-three matrices of weights

(your values are going to be different than mine).

Even if you have only one channel as input, you can have many

channels as output.

Spoiler alert: The filters learned by the network are going to

show edges, patterns, and even more complex shapes (sometimes

resembling faces, for instance). We’ll get back to visualizing

those filters later in this chapter.

We can also force a convolutional module to use a particular filter by setting its

weights:

with torch.no_grad():

conv.weight[0] = kernel_identity 1

conv.bias[0] = 0

1

1 weight[0] and bias[0] are indexing the first (and only) output channel in this

convolutional layer.

IMPORTANT: Setting the weights is a strictly no-gradient

operation, so you should always use the no_grad() context

manager.

In the code snippet above, we are forcing the module to use the (boring) identity

kernel we have been using so far. As expected, if we convolve our input image we’ll

Convolutions | 355

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

Saved successfully!

Ooh no, something went wrong!