When I create a PyTorch model, how do I print the number of trainable parameters? They have such features in Keras but I don’t know how to do it in PyTorch.
for parameter in model.parameters(): print(parameter)
how does one make sure
model.parameters() is not empty? It seems to be empty for me.
Can I see what you are trying to do? The parameters should not be empty unless you have something like:
class Model(nn.Module): def __init__(self): super(model, self).__init__() model = Model()
The above model has no parameters.
you can see it here: How does one make sure that the custom NN has parameters?
class NN(torch.nn.Module): def __init__(self, D_layers,act,w_inits,b_inits,bias=True): super(type(self), self).__init__() # actiaction func self.act = act #create linear layers self.linear_layers = [None] for d in range(1,len(D_layers)): linear_layer = torch.nn.Linear(D_layers[d-1], D_layers[d],bias=bias) self.linear_layers.append(linear_layer)
I posted my response on your original question!
def get_n_params(model): pp=0 for p in list(model.parameters()): nn=1 for s in list(p.size()): nn = nn*s pp += nn return pp
To compute the number of trainable parameters:
model_parameters = filter(lambda p: p.requires_grad, model.parameters()) params = sum([np.prod(p.size()) for p in model_parameters])
I like this solution!
To add my 50 cents, I would use
numel() instad of
np.prod() and compress the expression in one line:
def count_parameters(model): return sum(p.numel() for p in model.parameters() if p.requires_grad)
Provided the models are similar in keras and pytorch, the number of trainable parameters returned are different in pytorch and keras.
from torch import nn
from torchvision import models
a.fc = nn.Linear(512,2)
count = count_parameters(a)
Now in keras
import keras.applications.resnet50 as resnet
model =resnet.ResNet50(include_top=True, weights=None, input_tensor=None, input_shape=None, pooling=None, classes=2)
Total params: 23,591,810
Trainable params: 23,538,690
Non-trainable params: 53,120
Any reasons why this difference in numbers pop up?
Hi Alex, well spotted. I never did this comparison.
One easy check it to compare the layers one by one, (Linear, Conv2d, BatchNorm etc.), and see if there’s any difference in the number of params.
However, I don’t think there will be any difference, provided that you pay attention to the sneaky default parameters.
After that, you can patiently compare the graphs layer by layer and see if you spot any difference. Maybe it’s a matter of omitted/shared biases in some of the layers.
Btw, the first test is also a good check for the
count_parameters() function, let us now if you discover some unexpected behavior
Have you checked if they are the bias weights?
I guess this counts shared parameters multiple times, doesn’t it?
import torch from models.modelparts import count_parameters class tstModel(torch.nn.Module): def __init__(self): super().__init__() self.p = torch.nn.Parameter( torch.randn(1, 1, 1, requires_grad=True) .expand(-1, 5, -1) ) print(count_parameters(tstModel()))
If I understand correctly, expand just creates tensor with 5 views to the same parameter, so the right answer should be
But I don’t know how to fix that.
did anyone figure out a solution for shared parameters?
So I get that by default,
Conv2d includes the bias. But I’m unclear as to why they (the biases) are being included in ‘requires_grad’.
In : conv_3 = nn.Conv2d(512, 256, kernel_size=3, bias=True)
In : sum(p.numel() for p in conv_3.parameters())
In : sum(p.numel() for p in conv_3.parameters() if p.requires_grad)
bias is a trainable parameter, which requires gradients and is optimized in the same way as the
Do you have a use case, where the bias is fixed to a specific value?
Ah sorry. It was a conceptual error on my part. I had confused the idea of bias being a constant value with a weight with bias being a constant value.
Thanks for the clarification.
just out of curiosity, is there a
np.prod for pytorch?
torch.prod, but unlike numpy it accepts only tensors and does not accept tuples, lists, etc.
You may find this useful: https://pypi.org/project/pytorch-model-summary/