Keras 回调不断跳过保存检查点,声称缺少 val_acc
Keras callbacks keep skip saving checkpoints, claiming val_acc is missing
我会 运行 一些更大的模型并想尝试中间结果。
因此,我尝试使用检查点来保存每个 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)的解决方案,但这里仍然存在错误。
缺少它,不是因为缺少指标,而是因为您没有验证数据。通过validation_data
参数添加一些到fit
,或者使用validation_split
.
您正在尝试使用以下代码检查模型
# 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
以基于 acc
或 loss
工作像这样
# 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
,则必须将 mode
更改为 min
我遇到了同样的问题,只需将 'val_acc' 编辑为 'val_accuracy'
您只需将 monitor='val_acc'
更改为 -> monitor='val_accuracy'
因为您的指标是 metrics=['accuracy']
我会 运行 一些更大的模型并想尝试中间结果。
因此,我尝试使用检查点来保存每个 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)的解决方案,但这里仍然存在错误。
缺少它,不是因为缺少指标,而是因为您没有验证数据。通过validation_data
参数添加一些到fit
,或者使用validation_split
.
您正在尝试使用以下代码检查模型
# 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
以基于 acc
或 loss
工作像这样
# 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
mode
更改为 min
我遇到了同样的问题,只需将 'val_acc' 编辑为 'val_accuracy'
您只需将 monitor='val_acc'
更改为 -> monitor='val_accuracy'
因为您的指标是 metrics=['accuracy']