Python 3.x Keras回调保持跳过保存检查点,声称缺少val_acc

Python 3.x Keras回调保持跳过保存检查点,声称缺少val_acc,python-3.x,keras,checkpointing,Python 3.x,Keras,Checkpointing,我将运行一些较大的模型,并希望尝试中间结果 因此,我尝试使用检查点在每个历元之后保存最佳模型 这是我的代码: 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.

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

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

这是我的代码:

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)

向模型中添加
度量=['accurity']
是其他SO问题(例如)的解决方案,但这里仍然存在错误。

它缺失,不是因为缺少度量,而是因为您没有验证数据。通过
validation\u data
参数将一些添加到
fit
,或使用
validation\u 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')

<代码>模型检查点将考虑参数<代码>监视器< /代码>以决定是否保存模型。在您的代码中,它是

val\u acc
。因此,如果
val\u acc
增加,它将保存权重

现在在你的fit代码中

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

请记住,如果要监测
丢失
的情况,您必须将
模式更改为
min
,我也遇到了同样的问题,只需将“val\u acc”编辑为“val\u accurity”

好的,这很尴尬。有一小会儿,我对
val_acc
中的
val
感到疑惑,但回头比较了2天的其他代码,除了fit方法。谢谢,它很管用@xentity很高兴听到这有帮助