Path: blob/master/Documentation/Python/Predicting-Future-Prices-With-Keras.ipynb
984 views
![]()
Predicting Future Prices With Keras
An example of Keras model building, training, saving in the ObjectStore, and loading.
Import Libraries
Let's start by importing the functionality we'll need to build the model and serialize/unserialize the model for saving and loading
Gather & Prepare Data
Let's retreive some daily data for the SPY by making a History request.
We create a function that prepares our data suitable for training and testing our Model. We use 5 steps of OHLCV data to predict the closing price of the bar right after. By tying this to a function, we increase clarity, as well as reusability, especially if we were to copy it into a class in a .py file.
Build the Model (Regression Neural Network)
Let's build the neural network using Keras. We create a function that creates our Model, building up the input and output layers, and the layers Between. We tie this to a function for the same reason mentioned before.
We'll train the neural network by preparing our data with the function we defined earlier and feeding the result into our model
Analyze Performance
We then make predictions on the testing data set. We compare our Predicted Values with the Expected Values by plotting both to see if our Model has predictive power.
Save the Model to ObjectStore
We first serialize our model into a JSON string, then we save our model to ObjectStore. This way, the model doesn't need to be retrained, saving time and computational resources.
Load Model from the ObjectStore
Let's first retreive the JSON for the Keras model that we saved in the ObjectStore, then restore our model from this JSON string
To ensure loading the model was successfuly, let's test the model by having it make predictions.
Appendix
Below are some helper methods to manage the ObjectStore keys. We can use these to validate the saving and loading is successful.