首页 > 解决方案 > lstm如何进行前向验证和保存模型

问题描述

我有一个经过训练的 LSTM 模型。但是,我现在想尝试对我的模型进行前向验证,我有点困惑如何去做。

假设我使用一年的每日数据(2010 年 1 月至 2010 年 12 月)对下个月(2011 年 1 月)进行一些预测。然后,一旦该月完成,我会包含该月的数据,因此我多年的训练数据现在是 2010 年 2 月到 2011 年 1 月,用于对 2011 年 2 月进行一些预测。

所以我有 2010 年 1 月到 2010 年 12 月的训练模型。然后我如何将最终模型权重传递给下一个训练批次(2010 年 2 月到 2011 年 1 月),这样它就不需要重新开始了?还是应该每次训练模型时都以随机的起始权重开始?

它只是像在循环中调用下面的函数并以某种方式使用返回的模型吗?是否可以在每个训练点保存模型,以便以后检索和检查?

 function train_model(X_train, y_train)

     model.fit(
        X_train,
        y_train,
        batch_size=1024,
        epochs=50,
        validation_split=0.05)

    return model

标签: pythontensorflowkeras

解决方案


两个问题都是。

1)无需重新编译模型,模型权重就绑定在模型中,因此

# model  = train and fit model for Jan 2010 to Jan 2011
#
for X, y in batches: # batches over new window (Feb 2010 to Feb 2011)
    model.fit(X, y) 

努力从以前的权重更新模型,但请考虑这是否正是您想要做的。

  1. 是的,保存和加载模型按预期工作
# weights model.get_weights()
# write weights  to disk 
... 
# weighs = read weights 
# model = load model architecture 
model.set_weights(weights)

应该做的伎俩。

为了在检查点保存,我建议使用ModelCheckpoint回调。这样可以轻松保存模型和特定的迭代点,以便您将来可以从该点进行训练。


推荐阅读