Python Keras:如何确定保存了哪个迭代的最佳val模型?

Python Keras:如何确定保存了哪个迭代的最佳val模型?,python,keras,Python,Keras,我的部分培训代码如下所示: model_filepath = 'models/cnn_best.h5' datetime_str = ('{date:%Y-%m-%d-%H:%M:%S}'.format(date=datetime.now())) callbacks = [ ModelCheckpoint(model_filepath, monitor='val_loss', save_best_only=True, verbose=0),

我的部分培训代码如下所示:

    model_filepath = 'models/cnn_best.h5'
    datetime_str = ('{date:%Y-%m-%d-%H:%M:%S}'.format(date=datetime.now()))
    callbacks = [
        ModelCheckpoint(model_filepath, monitor='val_loss', save_best_only=True, verbose=0),
        TensorBoard(log_dir='./logs_'+args.model_postfix+'/'+datetime_str,
                    histogram_freq=0, write_graph=True, write_images=True),
        CustomCallback(generator.batch_generator(is_train=True), generator.batch_generator(is_train=False),
                       args.model_postfix)
    ]

    history = model.fit_generator(
        generator=generator.batch_generator(is_train=True),
        epochs=config.N_EPOCHS,
        steps_per_epoch=100,
        validation_data=generator.batch_generator(is_train=False),
        validation_steps=10,
        verbose=1,
        shuffle=False,
        callbacks=callbacks)
ModelCheckpoint使用model_文件路径名以最佳val分数保存模型,是否可以将步骤/历元也添加到模型文件名?i、 e.可能通过从ModelCheckpoint派生?或者唯一的方法是解析最小值丢失值的历史记录并重命名文件?或者不使用model.fit_generator,而是在循环中从生成器中馈送数据?这看起来很糟糕,因为在这种情况下,我需要在批处理生成器上方制作包装器,以使数据加载多进程。如果在检查点中放置verbose=1,您将看到它何时保存

您也可以自己创建一个快速LambdaCallback:

monitor = 'val_loss'    
bestLoss = 1000000000
bestEpoch = 0

def saveFunction(epoch, logs):
    loss = logs[monitor]
    if loss < bestLoss:
        model.save(name + "_" + str(epoch))
        bestLoss = loss
        bestEpoch = epoch

saverCallback = LambdaCallback(on_epoch_end = saveFunction)
#and fit
如果愿意,还可以创建一个自定义回调,而不是Lambda,以防将变量作为属性保留在其中