3

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

Marcin Możejko
  • 39,542
  • 10
  • 109
  • 120
Akhan
  • 425
  • 1
  • 7
  • 21

1 Answers1

6

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')
Marcin Możejko
  • 39,542
  • 10
  • 109
  • 120
  • @MarcinMożejko Can you please take a look into this question? https://stackoverflow.com/q/58900947/5904928 I am struggling for hours, couldn't find an answer. – Aaditya Ura Nov 17 '19 at 13:40