Issue
How do I count the total number of parameters in a PyTorch model? Something similar to model.count_params()
in Keras.
Solution
PyTorch doesn't have a function to calculate the total number of parameters as Keras does, but it's possible to sum the number of elements for every parameter group:
pytorch_total_params = sum(p.numel() for p in model.parameters())
If you want to calculate only the trainable parameters:
pytorch_total_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
Answer inspired by this answer on PyTorch Forums.
Note: I'm answering my own question. If anyone has a better solution, please share with us.
Answered By - Fábio Perez
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.