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.

dummy_list

Output

[(Linear(in_features=1, out_features=1, bias=True),

(tensor([0.3000]),),

tensor([-0.7514], grad_fn=<AddBackward0>))]

Now we’re talking! Here is the tuple we were expecting! If you call the model once

again, it will append yet another tuple to the list, and so on and so forth. This hook

is going to be hooked to our model until it is explicitly removed (hence the need to

keep the handles). To remove a hook, you can simply call its remove() method:

dummy_handle.remove()

And the hook goes bye-bye! But we did not lose the collected information, since

our variable, dummy_list, was defined outside the hook function.

Look at the first element of the tuple: It is an instance of a model (or layer). Even if

we use a Sequential model and name the layers, the names won’t make it to the

hook function. So we need to be clever here and make the association ourselves.

Let’s get back to our real model now. We can get a list of all its named modules by

using the appropriate method: named_modules() (what else could it be?!).

modules = list(sbs_cnn1.model.named_modules())

modules

Visualizing Filters and More! | 397

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

Saved successfully!

Ooh no, something went wrong!