How to get all weights of RNN in PyTorch

Suppose I have one hidden layer fc, with layer size 30, input size 5, output size 2.

I can get weight between hidden layer and output layer by fc.weight.grad. But how could I get the weight that between input layer and fc?

Your code will get you the gradient of the fc.weight parameter, not the weights themselves.
If you would like to get the parameter values directly, you should call fc.weight.
I guess your input feature size should be 5 and the hidden dimension 30.
Here is a small code snippet printing the weights of both layers for this model:

in_features = 5
hidden = 30
out_features = 2

model = nn.Sequential(
    nn.Linear(in_features, hidden),
    nn.Linear(hidden, out_features)

x = torch.randn(1, in_features)
output = model(x)
1 Like