Akhan
Akhan

Reputation: 425

Keras Functional API changing layer names in every API

When I run the functional API in the model for k-fold cross-validation, the numbers in the naming the dense layer is increased in the return fitted model of each fold. Like in the first fold it’s dense_2_acc, then in 2nd fold its dense_5_acc.

By my model summary shows my model is correct. why is it changing the names in the fitted model history object of each fold?

enter image description here

Upvotes: 3

Views: 1142

Answers (1)

Marcin Możejko
Marcin Możejko

Reputation: 40506

This is a really good question which shows something really important about keras. The reason why names change in such manner is that keras is not clearing previously defined variables even when you overwrite the model. You can easily check that variables are still in session.graph by calling:

from keras import backend as K
K.get_session().graph.get_collection('variables')

In order to clear previous model variables one may call:

K.clear_session()

However - be careful - as you might lose an existing model. If you want to keep names the same you can simply name your layers by adding name parameter to your layer instantiation, e.g.:

Dense(10, activation='softmax', name='output')

Upvotes: 6

Related Questions