RNN - Number of Parameters

Hi,

I am trying to find out the number of parameters for a PyTorch model

class SimpleRNN(nn.Module):
    def __init__(self):
        super(SimpleRNN, self).__init__()
        self.rnn = nn.RNN(input_size=1,
                         hidden_size=20,
                         num_layers=1,
                         nonlinearity='tanh',
                         batch_first=True
                         )
        self.fc = nn.Linear(20, 1)
    def forward(self,X):
        out, _ = self.rnn(X)
        out = self.fc(out[:,-1, :])
        return out
        
model_sr = SimpleRNN()

from torchsummary import summary
summary(model_sr, (50,1))

But it says 0 params for the RNN layer! Is there a way for me to find out? (e.g. in Keras’ model.summary(), it does give a number)

Thanks

def count_parameters(model):
    return sum(param.numel() for param in model.parameters())

This function will help you to count the number of parameters in your model

1 Like