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

Keras: how to set learning phase after model loading

我在尝试训练从 json 配置 + 权重文件加载的预训练模型时遇到问题。

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

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: ("An input of the graph, used to compute DimShuffle{x,x}(keras_learning_phase), was not provided and not given a value.Use the Theano flag exception_verbosity='high',for more information on this error.", keras_learning_phase)

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

提前致谢!

让我自己回答这个问题。

此问题仅与keras 1.0.0 版本相关,已在1.0.2 中修复。所以上面的代码片段在新版本的 keras 上完美运行,不需要明确设置学习阶段。

github issue tread 中有更多详细信息。