简体   繁体   中英

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?

在此处输入图片说明

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, eg:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM