Time Series Forecasting with TensorFlow.js

Time Series Forecasting with TensorFlow.

jsPull stock prices from online API and perform predictions using Recurrent Neural Network & Long Short Term Memory (LSTM) with TensorFlow.

js frameworkJinglesBlockedUnblockFollowFollowingMay 17Machine learning is becoming increasingly popular these days and a growing number of the world’s population see it is as a magic crystal ball: predicting when and what will happen in the future.

This experiment uses artificial neural networks to reveal stock market trends and demonstrates the ability of time series forecasting to predict future stock prices based on past historical data.

Disclaimer: As stock markets fluctuation are dynamic and unpredictable owing to multiple factors, this experiment is 100% educational and by no means a trading prediction tool.

View demo or view code on GithubProject WalkthroughThere are 4 parts to this project walkthrough:Get stocks data from online APICompute simple moving average for a given time windowTrain LSTM neural networkPredict and compare predicted values to the actual valuesGet Stocks DataBefore we can train the neural network and make any predictions, we will first require data.

The type of data we are looking for is time series: a sequence of numbers in chronological order.

A good place to fetch these data are from alphavantage.


This API allows us to retrieve chronological data on specific company stocks prices from the last 20 years.

The API yields the following fields:open pricethe highest price of that daythe lowest price of that dayclosing price (this is used in this project)volumeTo prepare training dataset for our neural network, we will be using closing stocks price.

This also means that we will be aiming to predict the future closing price.

Below graph shows 20 years of Microsoft Corporation weekly closing prices.

20 years of Microsoft Corporation weekly closing prices data from alphavantage.

coSimple Moving AverageFor this experiment, we are using supervised learning, which means feeding data to the neural network and it learns by mapping input data to the output label.

One way to prepare the training dataset is to extract the moving average from that time series data.

Simple Moving Average (SMA) is a method to identify trends direction for a certain period of time, by looking at the average of all the values within that time window.

The number of prices in a time window is selected experimentally.

For example, let’s assume the closing prices for the past 5 days were 13, 15, 14, 16, 17, the SMA would be (13+15+14+16+17)/5 = 15.

So the input for our training dataset is the set of prices within a single time window, and its label is the computed moving average of those prices.

Let’s compute the SMA of Microsoft Corporation weekly closing prices data, with a window size of 50.

And this is what we get, weekly stock closing price in blue, and SMA in orange.

Because SMA is the moving average of 50 weeks, it is smoother than the weekly price, which can fluctuate.

Simple Moving Average of Microsoft Corporation closing prices dataTraining DataWe can prepare the data with the SMA computed.

Given the window size is 50, this means that we will use the closing price of every 50 consecutive weeks as our training features (also known as x), and the SMA of those 50 weeks as our training label (or y).

For example…+———————————–+———————+| Features (X) | Label (Y) |+———————————–+———————+| [week1 price, .

, week50 price] | SMA of week 1 to 50 |+———————————–+———————+| [week2 price, .

, week51 price] | SMA of week 2 to 51 |+———————————–+———————+Which looks something like…+———————————————+———–+| Features (X) | Label (Y) |+———————————————+———–+| [127,135.


25, .



81] | 107.

96 |+———————————————+———–+| [135.



19, .


81,141] | 108.

26 |+———————————————+———–+Next, we split our data into 2 sets, training and validation set.

If 70% of the data is used for training, then 30% for validation.

The API returns us approximate 1000 weeks of data, so 700 for training, and 300 for validation.

Train Neural NetworkNow that the training data is ready, it is time to create a model for time series prediction, to achieve this we will use TensorFlow.

js (TFJS) framework.

TFJS is a library for developing and training machine learning models in JavaScript, and we can deploy these machine learning capabilities in a web browser.

Sequential model is selected which simply connects each layer and pass the data from input to the output during the training process.

In order for the model to learn time series data which are sequential, recurrent neural network (RNN) layer is created and a number of LSTM cells are added to the RNN.

The model will be trained using Adam (research paper), a popular optimisation algorithm for machine learning.

Root mean square error which will determine the difference between predicted values and the actual values, so the model is able to learn by minimising the error during the training process.

These are the hyper-parameters (parameters used in the training process) available for tweaking in the frontend:Training Dataset Size (%): the amount of data used for training, and remaining data will be used for validationEpochs: number of times the dataset is used to train the model (learn more)Learning Rate: the amount of change in the weights during training in each step (learn more)Hidden LSTM Layers: to increase the model complexity to learn in higher dimensional space (learn more)Web frontend [https://lonedune.


io/tfjs-stocks/demo/], showing parameters available for tweakingClick the Begin Training Model button…Training model UIThe model seems to converge at around 15 epoch.

Validation and PredictionNow that the model is trained, it is time to use it for predicting future values, for our case, it is the moving average.

We are actually using the remaining 30% of the data for prediction which allows us to see how closely our predicted values are compared to the actual values.

The green line denotes the prediction of the validation dataThis means that the last 30% of the data is unseen by the model, and looks like the model does a good job plotting closely to the moving average line.

ConclusionThere are many ways to do time series prediction other than using a simple moving average.

Possible future work is to implement this with more data from various sources.

With TensorFlow.

js, machine learning on a web browser is possible, and it is actually pretty cool.

View demo or view code on Github, this experiment is 100% educational and by no means a trading prediction tool.

.. More details

Leave a Reply