Keras:如何在模型加载后设置学习阶段

时间:2016-10-13 15:21:30

标签: python theano keras

当我尝试训练从json config + weight文件加载的预训练模型时,我遇到了问题。

我使用以下代码(简化):

from keras.utils.layer_utils import layer_from_config    
with open("config.json", "rb") as f:
    config = json.loads(f.read())
    model = layer_from_config(config)

model.load_weights("weights.net")

history = model.fit(batch, target, verbose=1, 
                    validation_data=(test_batch, test_target), shuffle=True)

我得到以下例外:

  

theano.gof.fg.MissingInputError :("图表的输入,用于   没有提供计算DimShuffle {x,x}(keras_learning_phase)   没有给出一个值。使用Theano标志exception_verbosity =' high',for   有关此错误的更多信息。",keras_learning_phase)

我认为这是有道理的,因为我在模型中有辍学层,所以它应该知道当前的学习阶段。如何将学习阶段设置为“培训”?或者这里可能有不同的问题?

提前致谢!

1 个答案:

答案 0 :(得分:1)

Let me answer to this question by myself.

This issue is related only to keras 1.0.0 version and it was fixed in 1.0.2. So code snippet above is perfectly work on newer version of keras, no need to explicitly set learning phase.

More details in github issue tread