Keras: the number of trained parameters in the model

I install trainable=Falsein all my layers implemented through the API Model, but I want to check if this works. model.count_params()returns the total number of parameters, but is there a way I can get the total number of parameters trained, besides viewing the last few lines model.summary()?

+4
source share
1 answer
from keras import backend as K

trainable_count = int(
    np.sum([K.count_params(p) for p in set(model.trainable_weights)]))
non_trainable_count = int(
    np.sum([K.count_params(p) for p in set(model.non_trainable_weights)]))

print('Total params: {:,}'.format(trainable_count + non_trainable_count))
print('Trainable params: {:,}'.format(trainable_count))
print('Non-trainable params: {:,}'.format(non_trainable_count))

The above snippet can be found at the end of the definition layer_utils.print_summary(), which summary().

+8
source

Source: https://habr.com/ru/post/1681234/


All Articles