Search code examples
pythonneural-networkpytorchtorch

How to access the network weights while using PyTorch 'nn.Sequential'?


I'm building a neural network and I don't know how to access the model weights for each layer.

I've tried

model.input_size.weight

Code:

input_size = 784
hidden_sizes = [128, 64]
output_size = 10

# Build a feed-forward network
model = nn.Sequential(nn.Linear(input_size, hidden_sizes[0]),
                      nn.ReLU(),
                      nn.Linear(hidden_sizes[0], hidden_sizes[1]),
                      nn.ReLU(),
                      nn.Linear(hidden_sizes[1], output_size),
                      nn.Softmax(dim=1))

I expected to get the weights but I got

'Sequential' object has no attribute 'input_size'


Solution

  • I've tried many ways, and it seems that the only way is by naming each layer by passing OrderedDict

    from collections import OrderedDict
    model = nn.Sequential(OrderedDict([
                      ('fc1', nn.Linear(input_size, hidden_sizes[0])),
                      ('relu1', nn.ReLU()),
                      ('fc2', nn.Linear(hidden_sizes[0], hidden_sizes[1])),
                      ('relu2', nn.ReLU()),
                      ('output', nn.Linear(hidden_sizes[1], output_size)),
                      ('softmax', nn.Softmax(dim=1))]))
    

    So to access the weights of each layer, we need to call it by its own unique layer name.

    For example to access weights of layer 1 model.fc1.weight

    Parameter containing:
    tensor([[-7.3584e-03, -2.3753e-02, -2.2565e-02,  ...,  2.1965e-02,
          1.0699e-02, -2.8968e-02],
        [ 2.2930e-02, -2.4317e-02,  2.9939e-02,  ...,  1.1536e-02,
          1.9830e-02, -1.4294e-02],
        [ 3.0891e-02,  2.5781e-02, -2.5248e-02,  ..., -1.5813e-02,
          6.1708e-03, -1.8673e-02],
        ...,
        [-1.2596e-03, -1.2320e-05,  1.9106e-02,  ...,  2.1987e-02,
         -3.3817e-02, -9.4880e-03],
        [ 1.4234e-02,  2.1246e-02, -1.0369e-02,  ..., -1.2366e-02,
         -4.7024e-04, -2.5259e-02],
        [ 7.5356e-03,  3.4400e-02, -1.0673e-02,  ...,  2.8880e-02,
         -1.0365e-02, -1.2916e-02]], requires_grad=True)