Keras回调继续跳过保存检查点,声称缺少val_acc [英] Keras callbacks keep skip saving checkpoints, claiming val_acc is missing

查看:1116
本文介绍了Keras回调继续跳过保存检查点,声称缺少val_acc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将运行一些较大的模型,并尝试中间结果.

I'll run some larger models and want to try intermediate results.

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

Therefore, I try to use checkpoints to save the best model after each 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])

但是在第一个时期之后,我仍然收到警告:

But I am still getting the warning after the first epoch:

/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问题(例如

To add metrics=['accuracy'] to the model was in other SO questions (e.g. Unable to save weights while using pre-trained VGG16 model) the solution, but here the error still remains.

推荐答案

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

You are trying to checkpoint the model using the following code

# 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增加,它将节省权重.

ModelCheckpoint will consider the argument monitor to take the decision of saving the model or not. In your code it is val_acc. So it will save the weights if there is a increase in the val_acc.

现在输入适合的代码,

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

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

you haven't provided any validation data. ModelCheckpoint can't save the weights because it don't have the monitor argument to check.

为了基于val_acc进行检查指向,您必须提供一些验证数据.

In order to do check pointing based on val_acc you must provide some validation data like this.

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

如果您出于某种原因不想使用验证数据并实施检查点,则必须像这样根据accloss更改ModelCheckpoint以使其工作

If you don't want to use validation data for whatever be the reason and implement check pointing, you have to change the ModelCheckpoint to work based on acc or loss like this

# 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')

请记住,如果要转到monitor loss

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

这篇关于Keras回调继续跳过保存检查点,声称缺少val_acc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆