Path: blob/master/Documentation/Python/Predicting-Future-Prices-With-TensorFlow.ipynb
984 views
![]()
Predicting Future Prices With TensorFlow
An example of TF 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, split the data, and serialize/unserialize the model for saving and loading.
Gather & Prepare Data
Let's retreive some intraday data for the SPY by making a History request.
We'll use the last 5 closing prices of the SPY as inputs to our model. Here, we create a DataFrame containing this data.
We'd like the model to predict the closing price of the SPY 1 timestep into the future, so let's create a DataFrame containing this data.
Now, let's split the data into testing and training sets.
Define a Testing Method
To test the model, we'll setup a method to plot test set predictions ontop of the SPY price.
Manually Build the Model
Let's build the neural network architecture by utilizing the TensorFlow library. Note how we name the input and output nodes so we can retreive them when loading the model from the ObjectStore.
We'll train the neural network by iteratively minimizing the mean squared difference between the model predictions and the actual SPY price.
To ensure the model we've built and trained is working, let's plot it's predictions on the test set.
Save Model to the ObjectStore
We first serialize the TensorFlow graph and weights to JSON format, then save these in the ObjectStore by using the Save method.
Load Model from the ObjectStore
Let's first retreive the JSON for the TensorFlow graph and weights that we saved in the ObjectStore.
Now let's restore the TensorFlow graph from JSON and select the input and output nodes.
To avoid retraining the model after loading, let's restore the weights.
To ensure loading the model was successfuly, let's test the model.
Appendix
Below are some helper methods to manage the ObjectStore keys. We can use these to validate the saving and loading is successful.