Saving the 04_q_learning_for_trading Model For Later Use

Hey everyone.

Here is a snippet of the book…

“… You can test your trained agent on new data using the saved model (see the notebook for Lunar Lander).”
Jansen, Stefan. Machine Learning for Algorithmic Trading: Predictive models to extract signals from market and alternative data for systematic trading strategies with Python, 2nd Edition (p. 1097). Packt Publishing. Kindle Edition.

However, there is no reference to anything about saving models in the Lunar Lander notebook (or elsewhere I can find).

Stefan is unable to have the time to add some sample code right now (see: Chapter 22 testing the agent · Issue #139 · stefan-jansen/machine-learning-for-trading · GitHub) but suggested here might be a good place for us to collaborate.

Before I spend the hours it’ll take me to figure this out on my own, I wondered if anybody has any thoughts on how we can save the model after it has trained, and then load that model for later use?

To better explain, I’m thinking that after the “Train the Agent” section of the machine-learning-for-trading/04_q_learning_for_trading.ipynb at master · stefan-jansen/machine-learning-for-trading · GitHub

We could create a “Save the Model” section whereby, whatever means, this model is saved and can then be loaded again at a different time, and we can feed it out of sample data.

Make sense? Any help appreciated.

I’m afraid this was a reference to the lunar lander example used in the first edition.

Saving and loading models is really not too difficult, see the Tensorflow docs. It’s also a really good exercise to apply the examples from the docs and the previous edition to your trading agent.

1 Like