python - 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
解决方案
两个问题都是。
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)
努力从以前的权重更新模型,但请考虑这是否正是您想要做的。
- 是的,保存和加载模型按预期工作
# weights model.get_weights()
# write weights to disk
...
# weighs = read weights
# model = load model architecture
model.set_weights(weights)
应该做的伎俩。
为了在检查点保存,我建议使用ModelCheckpoint回调。这样可以轻松保存模型和特定的迭代点,以便您将来可以从该点进行训练。
推荐阅读
- mysql - sequelize-typescript:@PrimaryKey 装饰器不起作用
- python - CV2 不允许十进制 fps 并且视频显示大小已关闭
- flutter - 如何在 Flutter/Dart 中使用 Hive 框和索引以简单的方式将数据动态加载到 DataTable?
- python - 在创建有效的 unicode 以在 Matplotlib 中创建点标记时需要帮助
- r - 附加到数据框的末尾并在 Shiny 应用程序中显示为表格
- django - 想先保存用户资料数据,然后自动创建用户
- python - 如何解析 json 以获取数组 json 中特定键的所有值?
- algorithm - 数组元素的差异等于按位或和按位与的差异
- python-3.x - Visual Studio 代码错误:无法读取未定义的属性“then”
- postgresql - “日复一日”实际上是做什么的?