Pytorch - how to extract features of an MLP network (weights, biases, number of nodes, hidden layers)?
I would be interested to extract the weights, biases, number of nodes and number of hidden layers from an MLP/neural network built in pytorch. I wonder if anyone may be able to point me in the right direction?

Many thanks,

Max

Yes, we can do what you want by first creating a simple network:

input_dim = 400
hidden_dim = 512

net = nn.Sequential(nn.Linear(input_dim, hidden_dim),
                      nn.Sigmoid())

print(net)


When we print the net, we get to know the number of layers, number of nodes (out_features), and many other details:

Sequential(
  (0): Linear(in_features=400, out_features=512, bias=True)
  (1): Sigmoid()
)



Then if you want to load specific values of each parameter, you can print that too

model = Net(400, 512,10)

bias = model.fc1.bias

print(bias)


the output is:

tensor([ 3.4078e-02,  3.1537e-02,  3.0819e-02,  2.6163e-03,  2.1002e-03,
         4.6842e-05, -1.6454e-02, -2.9456e-02,  2.0646e-02, -3.7626e-02,
         3.5531e-02,  4.7748e-02, -4.6566e-02, -1.3317e-02, -4.6593e-02,
        -8.9996e-03, -2.6568e-02, -2.8191e-02, -1.9806e-02,  4.9720e-02,
        ---------------------------------------------------------------
        -4.6214e-02, -3.2799e-02, -3.3605e-02, -4.9720e-02, -1.0293e-02,
         3.2559e-03, -6.6590e-03, -1.2456e-02, -4.4547e-02,  4.2101e-02,
        -2.4981e-02, -3.6840e-03], requires_grad=True)


Hope it helps
