我正在使用Keras和TensorFlow进行时间序列预测。我需要用未来的数据重新训练模型。我的问题是,Keras是否支持这一功能?如果支持,我们该如何实现?
我正在使用Keras和TensorFlow进行时间序列预测。我需要用未来的数据重新训练模型。我的问题是,Keras是否支持这一功能?如果支持,我们该如何实现?
是的。
将您的模型保存为.h5文件。
当您想要训练您的模型时,再次加载它并像正常情况下一样执行model.fit操作。
确保在加载模型后不要编译它,因为这将重置您的权重。
有关更多信息,请参见此链接。
model.save('./MyModel_tf',save_format='tf')
# loading the saved model
loaded_model = tf.keras.models.load_model('./MyModel_tf')
# retraining the model
loaded_model.fit(x_train, y_train, epochs = 10, validation_data = (x_test,y_test),verbose=1)
当您加载已保存的模型时,默认情况下compile=True
,它将显示如下警告。
WARNING:tensorflow:Error in loading the saved optimizer state. As a result, your model is starting with a freshly initialized optimizer
loaded_model.fit()
进行重新训练。model.save('./MyModel_h5.h5', save_format='h5')
# loading the saved model
loaded_model_h5 = tf.keras.models.load_model('./MyModel_h5.h5')
compile=False
,然后使用自定义对象编译模型。对于上述两种方法都是如此。