How to find number of parameters of a neuralfit model?


I am using the neuralfit library to evolve a neural network, but I can’t figure out the total number of hyperparameters of the model. I already monitor the size of the neural network, which should give the bias parameters, but it does not include weights of connections.

import neuralfit
import numpy as np

x = np.asarray([[0],[1],[2],[3],[4]])
y = np.asarray([[4],[3],[2],[1],[0]])

model = neuralfit.Model(1,1)
model.compile('alpha', loss='mse', monitors=['size'])
Epoch 100/100 - ... - loss: 0.000000 - size: 4
Asked By: emittance



There are two ways in which this is possible, but the method you want to use depends on the answer you want. If you want to get the total number of parameters that NeuralFit considers, you can do

num_param = len(model.get_nodes()) + len(model.get_connections())

which gives the number of biases + weights of the model.

Alternative method: via Keras

Since NeuralFit models can be exported to Keras, we can use this question to find the number of parameters for a Keras model. However, it adds an additional parameter for every output node of the original model. This is because of the way that NeuralFit’s models are converted.

import keras.backend as K

keras_model = model.to_keras()
num_param = np.sum([K.count_params(w) for w in keras_model.trainable_weights])
Answered By: emittance