<jupyter_start><jupyter_text>Loading the model.Here we will focus only on `nn.Sequential` model types as they are easier to deal with. Generalizing the methods described here to `nn.Module` will require more work.<jupyter_code>class Flatten(nn.Module):
    def forward(self, x):
        return x.view(x.size(0), -1)
    
    def __str__(self):
        return 'Flatten()'

model = nn.Sequential(OrderedDict([
    ('conv2d_1', nn.Conv2d(in_channels=1, out_channels=32, kernel_size=3)),
    ('relu_1', nn.ReLU()),
    ('max_pooling2d_1', nn.MaxPool2d(kernel_size=2)),
    ('conv2d_2', nn.Conv2d(in_channels=32, out_channels=32, kernel_size=3)),
    ('relu_2', nn.ReLU()),
    ('dropout_1', nn.Dropout(p=0.25)),
    ('flatten_1', Flatten()),
    ('dense_1', nn.Linear(3872, 64)),
    ('relu_3', nn.ReLU()),
    ('dropout_2', nn.Dropout(p=0.5)),
    ('dense_2', nn.Linear(64, 10)),
    ('readout', nn.LogSoftmax())
]))

model.load_state_dict(torch.load('example_torch_mnist_model.pth'))<jupyter_output><empty_output><jupyter_text>Accessing the layersA `torch.nn.Sequential` module serves itself as an iterable and subscriptable container for all its children modules.<jupyter_code>for i, layer in enumerate(model):
    print('{}\t{}'.format(i, layer))<jupyter_output>0	Conv2d(1, 32, kernel_size=(3, 3), stride=(1, 1))
1	ReLU ()
2	MaxPool2d (size=(2, 2), stride=(2, 2), dilation=(1, 1))
3	Conv2d(32, 32, kernel_size=(3, 3), stride=(1, 1))
4	ReLU ()
5	Dropout (p = 0.25)
6	Flatten()
7	Linear (3872 -> 64)
8	ReLU ()
9	Dropout (p = 0.5)
10	Linear (64 -> 10)
11	LogSoftmax ()<jupyter_text>Moreover `.modules` and `.children` provide generators for accessing layers.<jupyter_code>for m in model.modules():
    print(m)
for c in model.children():
    print(c)<jupyter_output>Conv2d(1, 32, kernel_size=(3, 3), stride=(1, 1))
ReLU ()
MaxPool2d (size=(2, 2), stride=(2, 2), dilation=(1, 1))
Conv2d(32, 32, kernel_size=(3, 3), stride=(1, 1))
ReLU ()
Dropout (p = 0.25)
Flatten (
)
Linear (3872 -> 64)
ReLU ()
Dropout (p = 0.5)
Linear (64 -> 10)
LogSoftmax ()<jupyter_text>Getting the weigths.<jupyter_code>conv2d_1_weight = model[0].weight.data.numpy()
conv2d_1_weight.shape
for i in range(32):
    plt.imshow(conv2d_1_weight[i, 0])
    plt.show()<jupyter_output><empty_output><jupyter_text>Getting layer properties The layer objects themselfs expose most properties as attributes.<jupyter_code>conv2d_1 = model[0]
conv2d_1.kernel_size
conv2d_1.stride
conv2d_1.dilation
conv2d_1.in_channels, conv2d_1.out_channels
conv2d_1.padding
conv2d_1.output_padding
dropout_1 = model[5]
dropout_1.p
dense_1 = model[7]
dense_1.in_features, dense_1.out_features<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16190833959429002, 'fraction_numerical': 0.0638617580766341, 'mean_word_length': 5.386091127098322, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '7557', 'license': 'MIT', 'n_tokens_mistral': 1145, 'n_tokens_neox': 1064, 'n_words': 234, 'path': 'notebooks/pytorch/introspection.ipynb', 'repo_name': 'Petr-By/qtpyvis', 'size': '2662'}