Is there a way to rename metrics and losses for Keras models?… here is a solution to the problem.
Is there a way to rename metrics and losses for Keras models?
I have a very large model with a lot of losses and indicators.
When I execute print(np.array(self.model.metrics_names)).
I see:
['loss' 'autoencoder_loss' 'autoencoder_loss' 'autoencoder_loss'
'autoencoder_loss' 's_regularisation_phase_loss'
'gen_regularisation_phase_loss' 's_regularisation_phase_loss'
'z_regularisation_phase_loss' 'gen_regularisation_phase_loss'
'z_regularisation_phase_loss' 'gen_regularisation_phase_loss'
'gen_regularisation_phase_loss' 'autoencoder_categorical_accuracy'
'autoencoder_output' 'autoencoder_categorical_accuracy_1'
'autoencoder_output_1' 'autoencoder_categorical_accuracy_2'
'autoencoder_output_2' 'autoencoder_categorical_accuracy_3'
'autoencoder_output_3' 's_regularisation_phase_categorical_accuracy'
's_regularisation_phase_output'
'gen_regularisation_phase_categorical_accuracy'
'gen_regularisation_phase_output'
's_regularisation_phase_categorical_accuracy_1'
's_regularisation_phase_output_1'
'z_regularisation_phase_categorical_accuracy'
'z_regularisation_phase_output'
'gen_regularisation_phase_categorical_accuracy_1'
'gen_regularisation_phase_output_1'
'z_regularisation_phase_categorical_accuracy_1'
'z_regularisation_phase_output_1'
'gen_regularisation_phase_categorical_accuracy_2'
'gen_regularisation_phase_output_2'
'gen_regularisation_phase_categorical_accuracy_3'
'gen_regularisation_phase_output_3']
Is there a way to give them more meaningful names?
Solution
The name before each _loss
and _accuracy
comes from the name of the output layer. If you want to modify the name, you should rename the output layer.
Consider the following model.
input_ = keras.layers.Input(shape=(8,))
x = keras.layers.Dense(16)(input_)
output1 = keras.layers.Dense(32, name="output1")(x)
output2 = keras.layers.Dense(32, name="output2")(x)
model = keras.models.Model(inputs=input_, outputs=[output1, output2])
model.compile(loss=["mse", "mae"], optimizer="adam", metrics={"output1":"accuracy","output2":"accuracy"})
Now model.metrics_names
will provide you with the following list
['loss', 'output1_loss', 'output2_loss', 'output1_acc', 'output2_acc']