Reputation: 11
How to check my chainer model's parameters number? Is it something similar to model.count_params()
in Keras
.
Upvotes: 0
Views: 227
Reputation: 1888
If you want to see the values in the parameters then you can use this
# model.params gives you a generator object which you could iterate and see
for i in model.params():
print(i)
If you want to see the names of the layers and shapes then you can use this
for i in model.links():
print(i)
If you haven't initialized the input then you can only inspect the biases which are based on per unit. like this
total = 0
for idx, i in enumerate(model.params()):
if idx%2 != 0: # Skipping the weights and looking at biases
total += len(i)
print(total)
In case you have defined your inputs then the weights will not be None
and you can compute its values as well with slight modifications like this
total = 0
for idx, i in enumerate(model.params()):
if idx%2 != 0:
total += len(i)
else:
total += len(i)*len(i[0]) # len inputs * len units
print(total)
'''
Now for different models, there are different methods so we can use try-except to make it better but be careful as the results might be wrong in some cases. usually, they will be correct. There might be cases when you have nested layers in the model and you have to check recursively.
'''
total = 0
for idx, i in enumerate(model.params()):
try:
try:
total += len(i)
except:
total += len(i)*len(i[0])
except:
pass
Upvotes: 0