Keras 回调不断跳过保存检查点,声称缺少 val_acc

新手上路,请多包涵

我将运行一些更大的模型并想尝试中间结果。

因此,我尝试在每个 epoch 之后使用检查点来保存最佳模型。

这是我的代码:

 model = Sequential()
model.add(LSTM(700, input_shape=(X_modified.shape[1], X_modified.shape[2]), return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(700, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(700))
model.add(Dropout(0.2))
model.add(Dense(Y_modified.shape[1], activation='softmax'))

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

# Save the checkpoint in the /output folder
filepath = "output/text-gen-best.hdf5"

# Keep only a single checkpoint, the best over test accuracy.
checkpoint = ModelCheckpoint(filepath,
                            monitor='val_acc',
                            verbose=1,
                            save_best_only=True,
                            mode='max')
model.fit(X_modified, Y_modified, epochs=100, batch_size=50, callbacks=[checkpoint])

但是在第一个纪元之后我仍然收到警告:

 /usr/local/lib/python3.6/site-packages/keras/callbacks.py:432: RuntimeWarning: Can save best model only with val_acc available, skipping.
  'skipping.' % (self.monitor), RuntimeWarning)

metrics=['accuracy'] 添加到模型中是其他 SO 问题(例如 Unable to save weights while using pre-trained VGG16 model )的解决方案,但此处错误仍然存在。

原文由 xentity 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 663
2 个回答

您正在尝试使用以下代码检查模型

# Save the checkpoint in the /output folder
filepath = "output/text-gen-best.hdf5"

# Keep only a single checkpoint, the best over test accuracy.
checkpoint = ModelCheckpoint(filepath,
                            monitor='val_acc',
                            verbose=1,
                            save_best_only=True,
                            mode='max')

ModelCheckpoint 将考虑参数 monitor 来决定是否保存模型。在您的代码中它是 val_acc 。因此,如果 val_acc 增加,它将保存权重。

现在在你的适合代码中,

 model.fit(X_modified, Y_modified, epochs=100, batch_size=50, callbacks=[checkpoint])

您还没有提供任何验证数据。 ModelCheckpoint 无法保存权重,因为它没有要检查的 monitor 参数。

为了根据 val_acc 进行检查指向,您必须提供一些这样的验证数据。

 model.fit(X_modified, Y_modified, validation_data=(X_valid, y_valid), epochs=100, batch_size=50, callbacks=[checkpoint])

如果您出于任何原因不想使用验证数据并实施检查点,则必须更改 ModelCheckpoint 以基于 accloss 像这样

# Save the checkpoint in the /output folder
filepath = "output/text-gen-best.hdf5"

# Keep only a single checkpoint, the best over test accuracy.
checkpoint = ModelCheckpoint(filepath,
                            monitor='acc',
                            verbose=1,
                            save_best_only=True,
                            mode='max')

Keep in mind that you have to change mode to min if you are going to monitor the loss

原文由 Sreeram TP 发布,翻译遵循 CC BY-SA 4.0 许可协议

我遇到了同样的问题,只需将“val_acc”编辑为“val_accuracy”

原文由 mj.beyrami 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
Stack Overflow 翻译
子站问答
访问
宣传栏