Last Updated on August 7, 2022

Time series prediction problems are a difficult type of predictive modeling problem.

Unlike regression predictive modeling, time series also adds the complexity of a sequence dependence among the input variables.

A powerful type of neural network designed to handle sequence dependence is called a recurrent neural network. The Long Short-Term Memory network or LSTM network is a type of recurrent neural network used in deep learning because very large architectures can be successfully trained.

In this post, you will discover how to develop LSTM networks in Python using the Keras deep learning library to address a demonstration time-series prediction problem.

After completing this tutorial, you will know how to implement and develop LSTM networks for your own time series prediction problems and other more general sequence problems. You will know:

- About the International Airline Passengers time-series prediction problem
- How to develop LSTM networks for regression, window, and time-step-based framing of time series prediction problems
- How to develop and make predictions using LSTM networks that maintain state (memory) across very long sequences

In this tutorial, we will develop a number of LSTMs for a standard time series prediction problem. The problem and the chosen configuration for the LSTM networks are for demonstration purposes only; they are not optimized.

These examples will show exactly how you can develop your own differently structured LSTM networks for time series predictive modeling problems.

**Kick-start your project** with my new book Deep Learning for Time Series Forecasting, including *step-by-step tutorials* and the *Python source code* files for all examples.

Let’s get started.

**Jul/2016**: First published**Update Oct/2016**: There was an error in how RMSE was calculated in each example. Reported RMSEs were just plain wrong. Now, RMSE is calculated directly from predictions, and both RMSE and graphs of predictions are in the units of the original dataset. Models were evaluated using Keras 1.1.0, TensorFlow 0.10.0, and scikit-learn v0.18. Thanks to all those that pointed out the issue and to Philip O’Brien for helping to point out the fix.**Update Mar/2017**: Updated example for Keras 2.0.2, TensorFlow 1.0.1 and Theano 0.9.0**Update Apr/2017**: For a more complete and better-explained tutorial of LSTMs for time series forecasting, see the post Time Series Forecasting with the Long Short-Term Memory Network in Python**Updated Apr/2019**: Updated the link to dataset**Updated Jul/2022**: Updated for TensorFlow 2.x

### Updated LSTM Time Series Forecasting Posts:

The example in this post is quite dated. You can view some better examples using LSTMs on time series with:

- LSTMs for Univariate Time Series Forecasting
- LSTMs for Multivariate Time Series Forecasting
- LSTMs for Multi-Step Time Series Forecasting

## Problem Description

The problem you will look at in this post is the International Airline Passengers prediction problem.

This is a problem where, given a year and a month, the task is to predict the number of international airline passengers in units of 1,000. The data ranges from January 1949 to December 1960, or 12 years, with 144 observations.

- Download the dataset (save as “
*airline-passengers.csv*“).

Below is a sample of the first few lines of the file.

1 2 3 4 5 6 |
"Month","Passengers" "1949-01",112 "1949-02",118 "1949-03",132 "1949-04",129 "1949-05",121 |

You can load this dataset easily using the Pandas library. You are not interested in the date, given that each observation is separated by the same interval of one month. Therefore, when you load the dataset, you can exclude the first column.

Once loaded, you can easily plot the whole dataset. The code to load and plot the dataset is listed below.

1 2 3 4 5 |
import pandas import matplotlib.pyplot as plt dataset = pandas.read_csv('airline-passengers.csv', usecols=[1], engine='python') plt.plot(dataset) plt.show() |

You can see an upward trend in the dataset over time.

You can also see some periodicity in the dataset that probably corresponds to the Northern Hemisphere vacation period.

Let’s keep things simple and work with the data as-is.

Normally, it is a good idea to investigate various data preparation techniques to rescale the data and make it stationary.

### Need help with Deep Learning for Time Series?

Take my free 7-day email crash course now (with sample code).

Click to sign-up and also get a free PDF Ebook version of the course.

## Long Short-Term Memory Network

The Long Short-Term Memory network, or LSTM network, is a recurrent neural network trained using Backpropagation Through Time that overcomes the vanishing gradient problem.

As such, it can be used to create large recurrent networks that, in turn, can be used to address difficult sequence problems in machine learning and achieve state-of-the-art results.

Instead of neurons, LSTM networks have memory blocks connected through layers.

A block has components that make it smarter than a classical neuron and a memory for recent sequences. A block contains gates that manage the block’s state and output. A block operates upon an input sequence, and each gate within a block uses the sigmoid activation units to control whether it is triggered or not, making the change of state and addition of information flowing through the block conditional.

There are three types of gates within a unit:

**Forget Gate**: conditionally decides what information to throw away from the block**Input Gate**: conditionally decides which values from the input to update the memory state**Output Gate**: conditionally decides what to output based on input and the memory of the block

Each unit is like a mini-state machine where the gates of the units have weights that are learned during the training procedure.

You can see how you may achieve sophisticated learning and memory from a layer of LSTMs, and it is not hard to imagine how higher-order abstractions may be layered with multiple such layers.

## LSTM Network for Regression

You can phrase the problem as a regression problem.

That is, given the number of passengers (in units of thousands) this month, what is the number of passengers next month?

You can write a simple function to convert the single column of data into a two-column dataset: the first column containing this month’s (t) passenger count and the second column containing next month’s (t+1) passenger count to be predicted.

Before you start, let’s first import all the functions and classes you will use. This assumes a working SciPy environment with the Keras deep learning library installed.

1 2 3 4 5 6 7 8 9 |
import numpy as np import matplotlib.pyplot as plt import pandas as pd import tensorflow as tf from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense from tensorflow.keras.layers import LSTM from sklearn.preprocessing import MinMaxScaler from sklearn.metrics import mean_squared_error |

Before you do anything, it is a good idea to fix the random number seed to ensure your results are reproducible.

1 2 |
# fix random seed for reproducibility tf.random.set_seed(7) |

You can also use the code from the previous section to load the dataset as a Pandas dataframe. You can then extract the NumPy array from the dataframe and convert the integer values to floating point values, which are more suitable for modeling with a neural network.

1 2 3 4 |
# load the dataset dataframe = pd.read_csv('airline-passengers.csv', usecols=[1], engine='python') dataset = dataframe.values dataset = dataset.astype('float32') |

LSTMs are sensitive to the scale of the input data, specifically when the sigmoid (default) or tanh activation functions are used. It can be a good practice to rescale the data to the range of 0-to-1, also called normalizing. You can easily normalize the dataset using the **MinMaxScaler** preprocessing class from the scikit-learn library.

1 2 3 |
# normalize the dataset scaler = MinMaxScaler(feature_range=(0, 1)) dataset = scaler.fit_transform(dataset) |

After you model the data and estimate the skill of your model on the training dataset, you need to get an idea of the skill of the model on new unseen data. For a normal classification or regression problem, you would do this using cross validation.

With time series data, the sequence of values is important. A simple method that you can use is to split the ordered dataset into train and test datasets. The code below calculates the index of the split point and separates the data into the training datasets, with 67% of the observations used to train the model, leaving the remaining 33% for testing the model.

1 2 3 4 5 |
# split into train and test sets train_size = int(len(dataset) * 0.67) test_size = len(dataset) - train_size train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:] print(len(train), len(test)) |

Now, you can define a function to create a new dataset, as described above.

The function takes two arguments: the **dataset**, which is a NumPy array you want to convert into a dataset, and the **look_back**, which is the number of previous time steps to use as input variables to predict the next time period—in this case, defaulted to 1.

This default will create a dataset where X is the number of passengers at a given time (t), and Y is the number of passengers at the next time (t + 1).

It can be configured by constructing a differently shaped dataset in the next section.

1 2 3 4 5 6 7 8 |
# convert an array of values into a dataset matrix def create_dataset(dataset, look_back=1): dataX, dataY = [], [] for i in range(len(dataset)-look_back-1): a = dataset[i:(i+look_back), 0] dataX.append(a) dataY.append(dataset[i + look_back, 0]) return np.array(dataX), np.array(dataY) |

Let’s take a look at the effect of this function on the first rows of the dataset (shown in the unnormalized form for clarity).

1 2 3 4 5 6 |
X Y 112 118 118 132 132 129 129 121 121 135 |

If you compare these first five rows to the original dataset sample listed in the previous section, you can see the X=t and Y=t+1 pattern in the numbers.

Let’s use this function to prepare the train and test datasets for modeling.

1 2 3 4 |
# reshape into X=t and Y=t+1 look_back = 1 trainX, trainY = create_dataset(train, look_back) testX, testY = create_dataset(test, look_back) |

The LSTM network expects the input data (X) to be provided with a specific array structure in the form of *[samples, time steps, features]*.

Currently, the data is in the form of [*samples, features*], and you are framing the problem as one time step for each sample. You can transform the prepared train and test input data into the expected structure using **numpy.reshape()** as follows:

1 2 3 |
# reshape input to be [samples, time steps, features] trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1])) testX = np.reshape(testX, (testX.shape[0], 1, testX.shape[1])) |

You are now ready to design and fit your LSTM network for this problem.

The network has a visible layer with 1 input, a hidden layer with 4 LSTM blocks or neurons, and an output layer that makes a single value prediction. The default sigmoid activation function is used for the LSTM blocks. The network is trained for 100 epochs, and a batch size of 1 is used.

1 2 3 4 5 6 |
# create and fit the LSTM network model = Sequential() model.add(LSTM(4, input_shape=(1, look_back))) model.add(Dense(1)) model.compile(loss='mean_squared_error', optimizer='adam') model.fit(trainX, trainY, epochs=100, batch_size=1, verbose=2) |

Once the model is fit, you can estimate the performance of the model on the train and test datasets. This will give you a point of comparison for new models.

Note that you will invert the predictions before calculating error scores to ensure that performance is reported in the same units as the original data (thousands of passengers per month).

1 2 3 4 5 6 7 8 9 10 11 12 13 |
# make predictions trainPredict = model.predict(trainX) testPredict = model.predict(testX) # invert predictions trainPredict = scaler.inverse_transform(trainPredict) trainY = scaler.inverse_transform([trainY]) testPredict = scaler.inverse_transform(testPredict) testY = scaler.inverse_transform([testY]) # calculate root mean squared error trainScore = np.sqrt(mean_squared_error(trainY[0], trainPredict[:,0])) print('Train Score: %.2f RMSE' % (trainScore)) testScore = np.sqrt(mean_squared_error(testY[0], testPredict[:,0])) print('Test Score: %.2f RMSE' % (testScore)) |

Finally, you can generate predictions using the model for both the train and test dataset to get a visual indication of the skill of the model.

Because of how the dataset was prepared, you must shift the predictions so that they align on the x-axis with the original dataset. Once prepared, the data is plotted, showing the original dataset in blue, the predictions for the training dataset in green, and the predictions on the unseen test dataset in red.

1 2 3 4 5 6 7 8 9 10 11 12 13 |
# shift train predictions for plotting trainPredictPlot = np.empty_like(dataset) trainPredictPlot[:, :] = np.nan trainPredictPlot[look_back:len(trainPredict)+look_back, :] = trainPredict # shift test predictions for plotting testPredictPlot = np.empty_like(dataset) testPredictPlot[:, :] = np.nan testPredictPlot[len(trainPredict)+(look_back*2)+1:len(dataset)-1, :] = testPredict # plot baseline and predictions plt.plot(scaler.inverse_transform(dataset)) plt.plot(trainPredictPlot) plt.plot(testPredictPlot) plt.show() |

You can see that the model did an excellent job of fitting both the training and the test datasets.

For completeness, below is the entire code example.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# LSTM for international airline passengers problem with regression framing import numpy as np import matplotlib.pyplot as plt from pandas import read_csv import math import tensorflow as tf from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense from tensorflow.keras.layers import LSTM from sklearn.preprocessing import MinMaxScaler from sklearn.metrics import mean_squared_error # convert an array of values into a dataset matrix def create_dataset(dataset, look_back=1): dataX, dataY = [], [] for i in range(len(dataset)-look_back-1): a = dataset[i:(i+look_back), 0] dataX.append(a) dataY.append(dataset[i + look_back, 0]) return np.array(dataX), np.array(dataY) # fix random seed for reproducibility tf.random.set_seed(7) # load the dataset dataframe = read_csv('airline-passengers.csv', usecols=[1], engine='python') dataset = dataframe.values dataset = dataset.astype('float32') # normalize the dataset scaler = MinMaxScaler(feature_range=(0, 1)) dataset = scaler.fit_transform(dataset) # split into train and test sets train_size = int(len(dataset) * 0.67) test_size = len(dataset) - train_size train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:] # reshape into X=t and Y=t+1 look_back = 1 trainX, trainY = create_dataset(train, look_back) testX, testY = create_dataset(test, look_back) # reshape input to be [samples, time steps, features] trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1])) testX = np.reshape(testX, (testX.shape[0], 1, testX.shape[1])) # create and fit the LSTM network model = Sequential() model.add(LSTM(4, input_shape=(1, look_back))) model.add(Dense(1)) model.compile(loss='mean_squared_error', optimizer='adam') model.fit(trainX, trainY, epochs=100, batch_size=1, verbose=2) # make predictions trainPredict = model.predict(trainX) testPredict = model.predict(testX) # invert predictions trainPredict = scaler.inverse_transform(trainPredict) trainY = scaler.inverse_transform([trainY]) testPredict = scaler.inverse_transform(testPredict) testY = scaler.inverse_transform([testY]) # calculate root mean squared error trainScore = np.sqrt(mean_squared_error(trainY[0], trainPredict[:,0])) print('Train Score: %.2f RMSE' % (trainScore)) testScore = np.sqrt(mean_squared_error(testY[0], testPredict[:,0])) print('Test Score: %.2f RMSE' % (testScore)) # shift train predictions for plotting trainPredictPlot = np.empty_like(dataset) trainPredictPlot[:, :] = np.nan trainPredictPlot[look_back:len(trainPredict)+look_back, :] = trainPredict # shift test predictions for plotting testPredictPlot = np.empty_like(dataset) testPredictPlot[:, :] = np.nan testPredictPlot[len(trainPredict)+(look_back*2)+1:len(dataset)-1, :] = testPredict # plot baseline and predictions plt.plot(scaler.inverse_transform(dataset)) plt.plot(trainPredictPlot) plt.plot(testPredictPlot) plt.show() |

**Note**: Your results may vary given the stochastic nature of the algorithm or evaluation procedure, or differences in numerical precision. Consider running the example a few times and compare the average outcome.

Running the example produces the following output.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
... Epoch 95/100 94/94 - 0s - loss: 0.0021 - 37ms/epoch - 391us/step Epoch 96/100 94/94 - 0s - loss: 0.0020 - 37ms/epoch - 398us/step Epoch 97/100 94/94 - 0s - loss: 0.0020 - 37ms/epoch - 396us/step Epoch 98/100 94/94 - 0s - loss: 0.0020 - 37ms/epoch - 391us/step Epoch 99/100 94/94 - 0s - loss: 0.0020 - 37ms/epoch - 394us/step Epoch 100/100 94/94 - 0s - loss: 0.0020 - 36ms/epoch - 382us/step 3/3 [==============================] - 0s 490us/step 2/2 [==============================] - 0s 461us/step Train Score: 22.68 RMSE Test Score: 49.34 RMSE |

You can see that the model has an average error of about 23 passengers (in thousands) on the training dataset and about 49 passengers (in thousands) on the test dataset. Not that bad.

## LSTM for Regression Using the Window Method

You can also phrase the problem so that multiple, recent time steps can be used to make the prediction for the next time step.

This is called a window, and the size of the window is a parameter that can be tuned for each problem.

For example, given the current time (t) to predict the value at the next time in the sequence (t+1), you can use the current time (t), as well as the two prior times (t-1 and t-2) as input variables.

When phrased as a regression problem, the input variables are t-2, t-1, and t, and the output variable is t+1.

The **create_dataset()** function created in the previous section allows you to create this formulation of the time series problem by increasing the **look_back** argument from 1 to 3.

A sample of the dataset with this formulation is as follows:

1 2 3 4 5 6 |
X1 X2 X3 Y 112 118 132 129 118 132 129 121 132 129 121 135 129 121 135 148 121 135 148 148 |

You can re-run the example in the previous section with the larger window size. The whole code listing with just the window size change is listed below for completeness.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# LSTM for international airline passengers problem with window regression framing import numpy as np import matplotlib.pyplot as plt import tensorflow as tf from pandas import read_csv from keras.models import Sequential from keras.layers import Dense from keras.layers import LSTM from sklearn.preprocessing import MinMaxScaler from sklearn.metrics import mean_squared_error # convert an array of values into a dataset matrix def create_dataset(dataset, look_back=1): dataX, dataY = [], [] for i in range(len(dataset)-look_back-1): a = dataset[i:(i+look_back), 0] dataX.append(a) dataY.append(dataset[i + look_back, 0]) return np.array(dataX), np.array(dataY) # fix random seed for reproducibility tf.random.set_seed(7) # load the dataset dataframe = read_csv('airline-passengers.csv', usecols=[1], engine='python') dataset = dataframe.values dataset = dataset.astype('float32') # normalize the dataset scaler = MinMaxScaler(feature_range=(0, 1)) dataset = scaler.fit_transform(dataset) # split into train and test sets train_size = int(len(dataset) * 0.67) test_size = len(dataset) - train_size train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:] # reshape into X=t and Y=t+1 look_back = 3 trainX, trainY = create_dataset(train, look_back) testX, testY = create_dataset(test, look_back) # reshape input to be [samples, time steps, features] trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1])) testX = np.reshape(testX, (testX.shape[0], 1, testX.shape[1])) # create and fit the LSTM network model = Sequential() model.add(LSTM(4, input_shape=(1, look_back))) model.add(Dense(1)) model.compile(loss='mean_squared_error', optimizer='adam') model.fit(trainX, trainY, epochs=100, batch_size=1, verbose=2) # make predictions trainPredict = model.predict(trainX) testPredict = model.predict(testX) # invert predictions trainPredict = scaler.inverse_transform(trainPredict) trainY = scaler.inverse_transform([trainY]) testPredict = scaler.inverse_transform(testPredict) testY = scaler.inverse_transform([testY]) # calculate root mean squared error trainScore = np.sqrt(mean_squared_error(trainY[0], trainPredict[:,0])) print('Train Score: %.2f RMSE' % (trainScore)) testScore = np.sqrt(mean_squared_error(testY[0], testPredict[:,0])) print('Test Score: %.2f RMSE' % (testScore)) # shift train predictions for plotting trainPredictPlot = np.empty_like(dataset) trainPredictPlot[:, :] = np.nan trainPredictPlot[look_back:len(trainPredict)+look_back, :] = trainPredict # shift test predictions for plotting testPredictPlot = np.empty_like(dataset) testPredictPlot[:, :] = np.nan testPredictPlot[len(trainPredict)+(look_back*2)+1:len(dataset)-1, :] = testPredict # plot baseline and predictions plt.plot(scaler.inverse_transform(dataset)) plt.plot(trainPredictPlot) plt.plot(testPredictPlot) plt.show() |

**Note**: Your results may vary given the stochastic nature of the algorithm or evaluation procedure, or differences in numerical precision. Consider running the example a few times and compare the average outcome.

Running the example provides the following output:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Epoch 95/100 92/92 - 0s - loss: 0.0023 - 35ms/epoch - 384us/step Epoch 96/100 92/92 - 0s - loss: 0.0023 - 36ms/epoch - 389us/step Epoch 97/100 92/92 - 0s - loss: 0.0024 - 37ms/epoch - 404us/step Epoch 98/100 92/92 - 0s - loss: 0.0023 - 36ms/epoch - 392us/step Epoch 99/100 92/92 - 0s - loss: 0.0022 - 36ms/epoch - 389us/step Epoch 100/100 92/92 - 0s - loss: 0.0022 - 35ms/epoch - 384us/step 3/3 [==============================] - 0s 514us/step 2/2 [==============================] - 0s 533us/step Train Score: 24.86 RMSE Test Score: 70.48 RMSE |

You can see that the error was increased slightly compared to that of the previous section. The window size and the network architecture were not tuned: This is just a demonstration of how to frame a prediction problem.

## LSTM for Regression with Time Steps

You may have noticed that the data preparation for the LSTM network includes time steps.

Some sequence problems may have a varied number of time steps per sample. For example, you may have measurements of a physical machine leading up to the point of failure or a point of surge. Each incident would be a sample of observations that lead up to the event, which would be the time steps, and the variables observed would be the features.

Time steps provide another way to phrase your time series problem. Like above in the window example, you can take prior time steps in your time series as inputs to predict the output at the next time step.

Instead of phrasing the past observations as separate input features, you can use them as time steps of the one input feature, which is indeed a more accurate framing of the problem.

You can do this using the same data representation as in the previous window-based example, except when you reshape the data, you set the columns to be the time steps dimension and change the features dimension back to 1. For example:

1 2 3 |
# reshape input to be [samples, time steps, features] trainX = np.reshape(trainX, (trainX.shape[0], trainX.shape[1], 1)) testX = np.reshape(testX, (testX.shape[0], testX.shape[1], 1)) |

The entire code listing is provided below for completeness.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# LSTM for international airline passengers problem with time step regression framing import numpy as np import matplotlib.pyplot as plt from pandas import read_csv import tensorflow as tf from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense from tensorflow.keras.layers import LSTM from sklearn.preprocessing import MinMaxScaler from sklearn.metrics import mean_squared_error # convert an array of values into a dataset matrix def create_dataset(dataset, look_back=1): dataX, dataY = [], [] for i in range(len(dataset)-look_back-1): a = dataset[i:(i+look_back), 0] dataX.append(a) dataY.append(dataset[i + look_back, 0]) return np.array(dataX), np.array(dataY) # fix random seed for reproducibility tf.random.set_seed(7) # load the dataset dataframe = read_csv('airline-passengers.csv', usecols=[1], engine='python') dataset = dataframe.values dataset = dataset.astype('float32') # normalize the dataset scaler = MinMaxScaler(feature_range=(0, 1)) dataset = scaler.fit_transform(dataset) # split into train and test sets train_size = int(len(dataset) * 0.67) test_size = len(dataset) - train_size train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:] # reshape into X=t and Y=t+1 look_back = 3 trainX, trainY = create_dataset(train, look_back) testX, testY = create_dataset(test, look_back) # reshape input to be [samples, time steps, features] trainX = np.reshape(trainX, (trainX.shape[0], trainX.shape[1], 1)) testX = np.reshape(testX, (testX.shape[0], testX.shape[1], 1)) # create and fit the LSTM network model = Sequential() model.add(LSTM(4, input_shape=(look_back, 1))) model.add(Dense(1)) model.compile(loss='mean_squared_error', optimizer='adam') model.fit(trainX, trainY, epochs=100, batch_size=1, verbose=2) # make predictions trainPredict = model.predict(trainX) testPredict = model.predict(testX) # invert predictions trainPredict = scaler.inverse_transform(trainPredict) trainY = scaler.inverse_transform([trainY]) testPredict = scaler.inverse_transform(testPredict) testY = scaler.inverse_transform([testY]) # calculate root mean squared error trainScore = np.sqrt(mean_squared_error(trainY[0], trainPredict[:,0])) print('Train Score: %.2f RMSE' % (trainScore)) testScore = np.sqrt(mean_squared_error(testY[0], testPredict[:,0])) print('Test Score: %.2f RMSE' % (testScore)) # shift train predictions for plotting trainPredictPlot = np.empty_like(dataset) trainPredictPlot[:, :] = np.nan trainPredictPlot[look_back:len(trainPredict)+look_back, :] = trainPredict # shift test predictions for plotting testPredictPlot = np.empty_like(dataset) testPredictPlot[:, :] = np.nan testPredictPlot[len(trainPredict)+(look_back*2)+1:len(dataset)-1, :] = testPredict # plot baseline and predictions plt.plot(scaler.inverse_transform(dataset)) plt.plot(trainPredictPlot) plt.plot(testPredictPlot) plt.show() |

**Note**: Your results may vary given the stochastic nature of the algorithm or evaluation procedure, or differences in numerical precision. Consider running the example a few times and compare the average outcome.

Running the example provides the following output:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
... Epoch 95/100 92/92 - 0s - loss: 0.0023 - 45ms/epoch - 484us/step Epoch 96/100 92/92 - 0s - loss: 0.0023 - 45ms/epoch - 486us/step Epoch 97/100 92/92 - 0s - loss: 0.0024 - 44ms/epoch - 479us/step Epoch 98/100 92/92 - 0s - loss: 0.0022 - 45ms/epoch - 489us/step Epoch 99/100 92/92 - 0s - loss: 0.0022 - 45ms/epoch - 485us/step Epoch 100/100 92/92 - 0s - loss: 0.0021 - 45ms/epoch - 490us/step 3/3 [==============================] - 0s 635us/step 2/2 [==============================] - 0s 616us/step Train Score: 24.84 RMSE Test Score: 60.98 RMSE |

You can see that the results are slightly better than the previous example, although the structure of the input data makes a lot more sense.

## LSTM with Memory Between Batches

The LSTM network has memory capable of remembering across long sequences.

Normally, the state within the network is reset after each training batch when fitting the model, as well as each call to **model.predict()** or **model.evaluate()**.

You can gain finer control over when the internal state of the LSTM network is cleared in Keras by making the LSTM layer “stateful.” This means it can build a state over the entire training sequence and even maintain that state if needed to make predictions.

It requires that the training data not be shuffled when fitting the network. It also requires explicit resetting of the network state after each exposure to the training data (epoch) by calls to **model.reset_states()**. This means that you must create your own outer loop of epochs and within each epoch call **model.fit()** and **model.reset_states()**. For example:

1 2 3 |
for i in range(100): model.fit(trainX, trainY, epochs=1, batch_size=batch_size, verbose=2, shuffle=False) model.reset_states() |

Finally, when the LSTM layer is constructed, the **stateful** parameter must be set to **True. **Instead of specifying the input dimensions, you must hard code the number of samples in a batch, the number of time steps in a sample, and the number of features in a time step by setting the **batch_input_shape** parameter. For example:

1 |
model.add(LSTM(4, batch_input_shape=(batch_size, time_steps, features), stateful=True)) |

This same batch size must then be used later when evaluating the model and making predictions. For example:

1 |
model.predict(trainX, batch_size=batch_size) |

You can adapt the previous time step example to use a stateful LSTM. The full code listing is provided below.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# LSTM for international airline passengers problem with memory import numpy as np import matplotlib.pyplot as plt from pandas import read_csv import tensorflow as tf from keras.models import Sequential from keras.layers import Dense from keras.layers import LSTM from sklearn.preprocessing import MinMaxScaler from sklearn.metrics import mean_squared_error # convert an array of values into a dataset matrix def create_dataset(dataset, look_back=1): dataX, dataY = [], [] for i in range(len(dataset)-look_back-1): a = dataset[i:(i+look_back), 0] dataX.append(a) dataY.append(dataset[i + look_back, 0]) return np.array(dataX), np.array(dataY) # fix random seed for reproducibility tf.random.set_seed(7) # load the dataset dataframe = read_csv('airline-passengers.csv', usecols=[1], engine='python') dataset = dataframe.values dataset = dataset.astype('float32') # normalize the dataset scaler = MinMaxScaler(feature_range=(0, 1)) dataset = scaler.fit_transform(dataset) # split into train and test sets train_size = int(len(dataset) * 0.67) test_size = len(dataset) - train_size train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:] # reshape into X=t and Y=t+1 look_back = 3 trainX, trainY = create_dataset(train, look_back) testX, testY = create_dataset(test, look_back) # reshape input to be [samples, time steps, features] trainX = np.reshape(trainX, (trainX.shape[0], trainX.shape[1], 1)) testX = np.reshape(testX, (testX.shape[0], testX.shape[1], 1)) # create and fit the LSTM network batch_size = 1 model = Sequential() model.add(LSTM(4, batch_input_shape=(batch_size, look_back, 1), stateful=True)) model.add(Dense(1)) model.compile(loss='mean_squared_error', optimizer='adam') for i in range(100): model.fit(trainX, trainY, epochs=1, batch_size=batch_size, verbose=2, shuffle=False) model.reset_states() # make predictions trainPredict = model.predict(trainX, batch_size=batch_size) model.reset_states() testPredict = model.predict(testX, batch_size=batch_size) # invert predictions trainPredict = scaler.inverse_transform(trainPredict) trainY = scaler.inverse_transform([trainY]) testPredict = scaler.inverse_transform(testPredict) testY = scaler.inverse_transform([testY]) # calculate root mean squared error trainScore = np.sqrt(mean_squared_error(trainY[0], trainPredict[:,0])) print('Train Score: %.2f RMSE' % (trainScore)) testScore = np.sqrt(mean_squared_error(testY[0], testPredict[:,0])) print('Test Score: %.2f RMSE' % (testScore)) # shift train predictions for plotting trainPredictPlot = np.empty_like(dataset) trainPredictPlot[:, :] = np.nan trainPredictPlot[look_back:len(trainPredict)+look_back, :] = trainPredict # shift test predictions for plotting testPredictPlot = np.empty_like(dataset) testPredictPlot[:, :] = np.nan testPredictPlot[len(trainPredict)+(look_back*2)+1:len(dataset)-1, :] = testPredict # plot baseline and predictions plt.plot(scaler.inverse_transform(dataset)) plt.plot(trainPredictPlot) plt.plot(testPredictPlot) plt.show() |

**Note**: Your results may vary given the stochastic nature of the algorithm or evaluation procedure, or differences in numerical precision. Consider running the example a few times and compare the average outcome.

Running the example provides the following output:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
... 92/92 - 0s - loss: 0.0024 - 46ms/epoch - 502us/step 92/92 - 0s - loss: 0.0023 - 49ms/epoch - 538us/step 92/92 - 0s - loss: 0.0023 - 47ms/epoch - 514us/step 92/92 - 0s - loss: 0.0023 - 48ms/epoch - 526us/step 92/92 - 0s - loss: 0.0022 - 48ms/epoch - 517us/step 92/92 - 0s - loss: 0.0022 - 48ms/epoch - 521us/step 92/92 - 0s - loss: 0.0022 - 47ms/epoch - 512us/step 92/92 - 0s - loss: 0.0021 - 50ms/epoch - 540us/step 92/92 - 0s - loss: 0.0021 - 47ms/epoch - 512us/step 92/92 - 0s - loss: 0.0021 - 52ms/epoch - 565us/step 92/92 [==============================] - 0s 448us/step 44/44 [==============================] - 0s 383us/step Train Score: 24.48 RMSE Test Score: 49.55 RMSE |

You do see that results are better than some, worse than others. The model may need more modules and may need to be trained for more epochs to internalize the structure of the problem.

## Stacked LSTMs with Memory Between Batches

Finally, let’s take a look at one of the big benefits of LSTMs: the fact that they can be successfully trained when stacked into deep network architectures.

LSTM networks can be stacked in Keras in the same way that other layer types can be stacked. One addition to the configuration that is required is that an LSTM layer prior to each subsequent LSTM layer must return the sequence. This can be done by setting the **return_sequences** parameter on the layer to **True**.

You can extend the stateful LSTM in the previous section to have two layers, as follows:

1 2 |
model.add(LSTM(4, batch_input_shape=(batch_size, look_back, 1), stateful=True, return_sequences=True)) model.add(LSTM(4, batch_input_shape=(batch_size, look_back, 1), stateful=True)) |

The entire code listing is provided below for completeness.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# Stacked LSTM for international airline passengers problem with memory import numpy as np import matplotlib.pyplot as plt from pandas import read_csv import tensorflow as tf from keras.models import Sequential from keras.layers import Dense from keras.layers import LSTM from sklearn.preprocessing import MinMaxScaler from sklearn.metrics import mean_squared_error # convert an array of values into a dataset matrix def create_dataset(dataset, look_back=1): dataX, dataY = [], [] for i in range(len(dataset)-look_back-1): a = dataset[i:(i+look_back), 0] dataX.append(a) dataY.append(dataset[i + look_back, 0]) return np.array(dataX), np.array(dataY) # fix random seed for reproducibility tf.random.set_seed(7) # load the dataset dataframe = read_csv('airline-passengers.csv', usecols=[1], engine='python') dataset = dataframe.values dataset = dataset.astype('float32') # normalize the dataset scaler = MinMaxScaler(feature_range=(0, 1)) dataset = scaler.fit_transform(dataset) # split into train and test sets train_size = int(len(dataset) * 0.67) test_size = len(dataset) - train_size train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:] # reshape into X=t and Y=t+1 look_back = 3 trainX, trainY = create_dataset(train, look_back) testX, testY = create_dataset(test, look_back) # reshape input to be [samples, time steps, features] trainX = np.reshape(trainX, (trainX.shape[0], trainX.shape[1], 1)) testX = np.reshape(testX, (testX.shape[0], testX.shape[1], 1)) # create and fit the LSTM network batch_size = 1 model = Sequential() model.add(LSTM(4, batch_input_shape=(batch_size, look_back, 1), stateful=True, return_sequences=True)) model.add(LSTM(4, batch_input_shape=(batch_size, look_back, 1), stateful=True)) model.add(Dense(1)) model.compile(loss='mean_squared_error', optimizer='adam') for i in range(100): model.fit(trainX, trainY, epochs=1, batch_size=batch_size, verbose=2, shuffle=False) model.reset_states() # make predictions trainPredict = model.predict(trainX, batch_size=batch_size) model.reset_states() testPredict = model.predict(testX, batch_size=batch_size) # invert predictions trainPredict = scaler.inverse_transform(trainPredict) trainY = scaler.inverse_transform([trainY]) testPredict = scaler.inverse_transform(testPredict) testY = scaler.inverse_transform([testY]) # calculate root mean squared error trainScore = np.sqrt(mean_squared_error(trainY[0], trainPredict[:,0])) print('Train Score: %.2f RMSE' % (trainScore)) testScore = np.sqrt(mean_squared_error(testY[0], testPredict[:,0])) print('Test Score: %.2f RMSE' % (testScore)) # shift train predictions for plotting trainPredictPlot = np.empty_like(dataset) trainPredictPlot[:, :] = np.nan trainPredictPlot[look_back:len(trainPredict)+look_back, :] = trainPredict # shift test predictions for plotting testPredictPlot = np.empty_like(dataset) testPredictPlot[:, :] = np.nan testPredictPlot[len(trainPredict)+(look_back*2)+1:len(dataset)-1, :] = testPredict # plot baseline and predictions plt.plot(scaler.inverse_transform(dataset)) plt.plot(trainPredictPlot) plt.plot(testPredictPlot) plt.show() |

**Note**: Your results may vary given the stochastic nature of the algorithm or evaluation procedure, or differences in numerical precision. Consider running the example a few times and compare the average outcome.

Running the example produces the following output.

1 2 3 4 5 6 7 8 9 10 11 12 13 |
... 92/92 - 0s - loss: 0.0016 - 78ms/epoch - 849us/step 92/92 - 0s - loss: 0.0015 - 80ms/epoch - 874us/step 92/92 - 0s - loss: 0.0015 - 78ms/epoch - 843us/step 92/92 - 0s - loss: 0.0015 - 78ms/epoch - 845us/step 92/92 - 0s - loss: 0.0015 - 79ms/epoch - 859us/step 92/92 - 0s - loss: 0.0015 - 78ms/epoch - 848us/step 92/92 - 0s - loss: 0.0015 - 78ms/epoch - 844us/step 92/92 - 0s - loss: 0.0015 - 78ms/epoch - 852us/step 92/92 [==============================] - 0s 563us/step 44/44 [==============================] - 0s 453us/step Train Score: 20.58 RMSE Test Score: 55.99 RMSE |

The predictions on the test dataset are again worse. This is more evidence to suggest the need for additional training epochs.

## Summary

In this post, you discovered how to develop LSTM recurrent neural networks for time series prediction in Python with the Keras deep learning network.

Specifically, you learned:

- About the international airline passenger time series prediction problem
- How to create an LSTM for a regression and a window formulation of the time series problem
- How to create an LSTM with a time step formulation of the time series problem
- How to create an LSTM with state and stacked LSTMs with state to learn long sequences

Do you have any questions about LSTMs for time series prediction or about this post?

Ask your questions in the comments below, and I will do my best to answer.

### Updated LSTM Time Series Forecasting Posts:

The example in this post is quite dated. See these better examples available for using LSTMs on time series:

I like Keras, the example is excellent.

Thanks.

Hi, thanks for your awesome tutorial!

I just don’t get one thing… If you’d like to predict 1 step in the future, why does the red line stop before the blue line does?

So for example, we have the testset untill end of the year 1960. How can i predict the future year? Or passangers at the 1/1/1961 (if dataset ends at 12/31/1960).

Best,

Robin

Great question, there might be a small bug in how I am displaying the predictions in the plot.

Hey, great tutorial.

I have the same question about the future prediction. The “testPredict” has two fewer rows that the “test” once the algorithm is done running, how would I obtain the values for the a prediction 1 or 2 days ahead from the end date of the time series? Thanks.

Terrence

I think in the ”create_dataset” function, the range should be “len(dataset)-look_back” but not “len(dataset)-look_back-1”. No “1”should be subtracted here.

Hi Jason,

how to fix this bug? what modifications you need to make in the code to predict the values for 1/31/1961, if the dataset ends at 12/31/1960?

excellent lecture! but I still feel somehow cofused about lstm. I think the timesteps of 1 and feature_size of 3 have made a great job. However, when exchange them, with 3 timesteps in lstm, the prediction at time t+1 seems like a value a little lower than the ground truth value at time t, which make the prediction curve shift to the right of the true value. It seems that lstm only learn memory from last step and makes it a little delay.

Perhaps this might help:

https://machinelearningmastery.mystagingwebsite.com/faq/single-faq/what-is-the-difference-between-samples-timesteps-and-features-for-lstm-input

Sir,

Please let me know, how I can make a future prediction for next 30 or 60 or 90 days. Please help me making that forecast prediction.

Sincerely,

Bikash Kumar, India.

I show how here:

https://machinelearningmastery.mystagingwebsite.com/faq/single-faq/how-do-you-use-lstms-for-multi-step-time-series-forecasting

Hi,

Please, could you explain to us why the prediction (red) is shifted by one step from the blue plot, because when we try to predict 4 of 8 steps the plot is shifted by 4 or 8 steps too. Note that the test set construction is correct. The plot is correct.

Yes, the model has learned a persistence model:

https://machinelearningmastery.mystagingwebsite.com/faq/single-faq/why-is-my-forecasted-time-series-right-behind-the-actual-time-series

Yes, the model requires tuning, perhaps start here:

https://machinelearningmastery.mystagingwebsite.com/start-here/#deep_learning_time_series

Sir,did you fixed that bug in displaying the predictions in the plot. or do you know how to fix it?

Zhang, I think I arrived at this conclusion too.

Hi Robin,

Are you up with a solution for the bug? as you rightly said, the testpredict is smaller than test. How do you modify the code so that it predicts the value on 1/1/1961?

I have tried to check how to fix the bug. Simply delect the -1 in line 14 and for

testPredictPlot[len(trainPredict)+(look_back*2)+1:len(dataset)-1, :] = testPredict

replace it with

testPredictPlot[len(trainPredict)+(look_back):len(dataset)-1, :] = testPredict

The challenge would be that the length of the list would 1 less. but in that way the bug is fixed.

Hello Jason,

Thanks for sharing this great tutorial! Can you please also suggest the way to get the forward forecast based on the LSTM method. For example, if we want to forecast the value of the series for the next few weeks (ahead of current time–As we usually do for the any time series data), then what would be process to do that.

Regards

Shovon

Hi Shovon,

I would suggest reframing the problem to predict a long output sequence, then develop a model on this framing of the problem.

Hi Jason,

Can you elaborate a bit on what is the use of modeling a time-series without being able to make predictions of future time? I ask because I’m learning LSTMs and I’m facing the same issue as the person above: I can model a time series and make accurate predictions for data that I already have, but have difficulty predicting future observations.

Thanks a bunch.

Time series analysis is the study of time series without the interest in making predictions.

I am surprised that you mention “Time series analysis is the study of time series without the interest in making predictions.”.

But can this tutorial be used to predict future? Especially how to wrap the input data and un-wrap the prediction? My wild guess is:

normalize -> predict -> de-normalize

Is this correct?

Here we are differentiating between “analysis” and “forecasting” tasks.

I only have tutorials on forecasting, I don’t write about analysis.

Can RNN be used on Input with multi-variables?

Yes. LSTMs can take multiple input features.

Thanks for this great tutorial Jason. I’m still having trouble figuring out what kind of graph do you get when you do this:

# create and fit the LSTM network

model = Sequential()

model.add(LSTM(4, input_shape=(1, look_back)))

model.add(Dense(1))

for instance if your lookback=1: the input is one value xt, and the target output is xt+1. How is “LSTM(4, input_shape=(1, look_back))” linking your LSTM blocks with the input?

Or do you have 1 input => 1 LSTM block which hidden value (output of the LSTM) is fed to a 4X1 dense MLP? So that the output of the LSTM is actually the input of a 1x4x1 MLP…

And if your input is [xt-1, xt] with target xt+1 (lookback=2), you have two LSTMs blocks (fed with xt-1 and xt respectively) and the hidden value of the second block is the input of a 1x4x1 MLP.

I hope I’m being clear, I really have troubles answering this question. Your tutorial helps though!

The input_shape define the input, the LSTM is the first hidden layer, the Dense is the output layer.

Try this to get an idea of the graph:

Hi,

How we can expand future vector to use more than one indicator to predict future value?

Thanks

See this tutorial:

https://machinelearningmastery.mystagingwebsite.com/multivariate-time-series-forecasting-lstms-keras/

thanks Jason !! great tutorial .

I want to know how to use four variable like latitude,longitude,altitude and weather parameter for aircraft trajectory prediction using LSTM and HMM(Hidden Marcov Model). thanks.

Perhaps start here:

https://machinelearningmastery.mystagingwebsite.com/faq/single-faq/what-is-the-difference-between-samples-timesteps-and-features-for-lstm-input

Hi,

If I want to forecast next say 1000 values. What code would be look like for LSTM above algorithm?

I have examples here:

https://machinelearningmastery.mystagingwebsite.com/faq/single-faq/how-do-you-use-lstms-for-multi-step-time-series-forecasting

Hi Dr json,

Can we emphasize more on one variable in to predict via lstm???

The model will figure out what is important in the data during training. No need to tell it at inference/prediction time.

what about unsupervised learning?

Hi McMirr…You may find the following resource helpful:

https://machinelearningmastery.mystagingwebsite.com/supervised-and-unsupervised-machine-learning-algorithms/

Thanks for your sharing. I am still struggling how to do a real future 30 days prediction based on the current program. I am thinking to loop it as tomorrow prediction is based on latest 60 days in history. Then, the day after tomorrow is the latest 59 days plus the prediction of tomorrow.

How do it implement it & extend it? The current one is not applicable to real world application. Please suggest whether I am on the right track.

Change the output layer to have 30 nodes. Then train the model to make 30 day predictions.

Hi, thanks for the walkthrough. I’ve tried modifying the code to run the network for online prediction, but I’m having some issues. Would you be willing to take a look at my SO question? http://stackoverflow.com/questions/38313673/lstm-with-keras-for-mini-batch-training-and-online-testing

Cheers,

Alex

Sorry Alex, you’re question is a little vague.

It’s of the order “I have data like this…, what is the best way to model this problem”. It’s a tough StackOverflow question because it’s an open question rather than a specific technical question.

Generally, my answer would be “no idea, try lots of stuff and see what works best”.

I think your notion of online might also be confused (or I’m confused). Have you seen online implementations of LSTM? Keras does not support it as far as I know. You train your model then you make predictions. Unless of course you mean the maintained state of the model – but this is not online learning, it is just static model with state, the weights are not updated in an online manner unless you re-train your model frequently.

It might be worth stepping back from the code and taking some time to clearly define I/O of the problem and requirements to then figure out the right kind of algorithm/setup you need to solve it.

Hello Dr. Brownlee,

I have a question about the difference between the Time Steps and Windows method. Am I correct in understanding that the only difference is the shape of the data you feeding into the model? If so, can you give some intuition why the Time Steps method works better? If I have two sequences (For example, if I have 2 noisy signals, one noisier than the other), and I’m using them both to predict a sequence, which method do you think is better?

Best

Hi Tommy,

The window method creates new features, like new attributes for the model, where as timesteps are a sequence within a batch for a given feature.

I would not say one works better than another, the examples in this post are for demonstration only and are not tuned.

I would advise you to try both methods and see what works best, or frame your problem in the way that best makes sense.

Hi Jason,

What are the hyperparameters of your network?

Thanks,

Pedro

Hi Pedro, the hyperparameters for each network are available right there in the code.

Is it possible to perform hyperparameter optimization of the LTSM, for example using hyperopt?

I don’t see why not Evgeni., Sorry I don’t have an example.

Hi Jason,

Interesting post and a very useful website! Can I use LSTMS for time series classification, for a binary supervised problem? My data is arranged as time steps of 1 hr sequences leading up to an event and the occurrence and non-occurrence of the event are labelled in each instance. I have done a bit of research and have not seen many use cases in the literature. Do you think a different recurrent neural net or simpler MLP might work better in this case? Most of my the research done in my area has got OK results(70% accuracy) from feed forward neural networks and i thought to try out recurrent neural nets, specifically LSTMs to improve my accuracy.

I don’t see why not Jack.

I would suggest using a standard MLP with the window method as the baseline, then develop some LSTMs for comparison. I would expect LSTMs to generally perform better if there is information in the long sequences.

This post on binary classification may help, you can combine some of the details with the LSTMs in this post (e.g. the specifics of the Dense output layer and loss function):

https://machinelearningmastery.mystagingwebsite.com/binary-classification-tutorial-with-the-keras-deep-learning-library/

Hi Jason,

Thanks for this example. I ran the first code example (lookback=1) by just copying the code and can reproduce your train and test scores precisely, however my graph looks differently. Specifically for me the predicted graph (green and red lines) looks as if it is shifted by one to the right in comparison to what I see on this page. It also looks like the predicted graph starts at x=0 in your example, but my predicted graph starts at 1. So in my case it looks like the prediction is almost like predicting identity? Is there a way for me to verify what I could have done wrong?

Thanks,

Peter

Thanks Peter.

I think you’re right, I need to update my graphs in the post.

Hi Jason,

when outputting the train and test score, you scale the output of the model.evaluate with the minmaxscaler to match into the original scale. I am not sure if I understand that correctly. The data values are between 104 and 622, the trainScore (which is the mean squared error) will be scaled into that range using a linear mapping, right? So your transformed trainscore can never be lower than the minimum of the dataset, i.e. 104. Shouldn’t the square root of the trainScore be transformed and then the minimum of the range be subtracted and squared again to get the mean square error in the original domain range? Like numpy.square(scalar.inverse_transform([[nump.sqrt(trainScore)]])-scaler.data_min_)

Thanks,

Peter

Hi Peter, you may have found a bug, thanks.

I believe I thought the default evaluation metric was RMSE rather than MSE and I was using the scaler to transform the RMSE score back into original units.

I will update the examples ASAP.

Update: All estimates of model error were updated to first convert the error score to RMSE and then invert scale transform back to original units.

I would also point out that the normalization is learnt on the whole dataset.

Shouldn’t it be learnt after the train/test split?

Hi Peblo…Your understanding is correct! The following resource provides additional best practices regarding train/validation/test splits:

https://machinelearningmastery.mystagingwebsite.com/training-validation-test-split-and-cross-validation-done-right/

Thank you for your excellent post.

I have one question.

In your examples, you are discussing a predictor such as {x(t-2),x(t-1),x(t)} -> x(t+1).

I want to know how to implement a predictor like {x(t-2),x(t-1),x(t)} -> {x(t+1), x(t+2)}.

Could you tell me how to do so?

This is a sequence in and sequence out type problem.

I believe you prepare the dataset in this form and model it directly with LSTMs and MLPs.

I don’t have a worked example at this stage for you, but I believe it would be straight forward.

Hi,

First of all thanks for the tutorial. An excellent one at that.

However, I do have some questions regarding the underlying architecture that I’m trying to reconcile with what I’ve done learnt about. I posted a question here: http://stackoverflow.com/questions/38714959/understanding-keras-lstms which I felt was too long to post in this forum.

I would really appreciate your input, especially the question on time_steps vs features argument.

Thanks,

Sachin

If I understand correctly, you want more elaboration on time steps vs features?

Features are your input variables. In this airline example we only have one input variable, but we can contrive multiple input variables using past time steps in what is called the window method. Normally, multiple features would be a multivariate time series.

Timesteps are the sequence through time for a give attribute. As we comment in the tutorial, this is the natural mapping of the problem onto LSTMs for this airline problem.

You always need a 3D array as input for LSTMs [samples, features, timesteps], but you can reduce each dimension to one if needed. We explore this ability in reframe the problem in the tutorial above.

You also ask about the point of stateful. It is helpful to have memory between batches over one training run. If we keep all of out time series samples in order, the method can learn the relationships between values across batches. If we did not enable the stateful parameter, the algorithm we no knowledge beyond each batch, much like a MLP.

I hope that helps, I’m happy to dig into a specific topic further if you have more questions.

Dr. Jason,

I think this is a good place to bring this question. Suppose if I have X_train, X_test, y_train and y_test, should I transform all the values into a np.array? If I have in this format, should I still use ‘create_dataset’ function to create X and y?

Yes Jack.

Generally, prepare your data consistently.

Dr Jason,

I have an hourly time series with multiple predictor variables. I skipped

`create_dataset`

and just converted all my X_train, X_test, y_train and y_test into np arrays. The reason is, ex: I use past three months as my training and I would like to predict for next 7 days, which will be about 168 observations. If this is the case, if I happen to prepare consistent, would my ‘look_back = 168’ in`create_dataset`

function?I would recommend preparing data with the function in this post:

https://machinelearningmastery.mystagingwebsite.com/convert-time-series-supervised-learning-problem-python/

Dr. Jason,

After a deep thought and research I am thinking to just use my X_train, y_train, X_test and y_test without doing a look back. The reason is, y_train is dependent on on my X_train features. Therefore, my gut feeling is not use look back or sliding window. I just wanted to confirm with you and please let me know if I am on right track. BTW, when are you planning on doing a multivariate time series analysis? if you can educate us on that, it will be great. Thank you sir!

You may not need an LSTM if there is no input sequence, consider an MLP.

So does that mean (in reference to the LSTM diagram in http://colah.github.io/posts/2015-08-Understanding-LSTMs/) that the cell memory is not passed between consecutive lstms if stateful=false (i.e. set to zero)? Or do you mean cell memory is reset to zero between consecutive batches (In this tutorial batch_size is 1). Although I guess I should point out that the hidden layer values are passed on, so it will still be different to a MLP (wouldn’t it?)

On a side note, the fact that the output has to be a factor of batch_size seems to be confounding. Feels like it limits me to using a batch_size of one.

If stateful is set to false (the default), then I understand according to the Keras documentation that the state within each LSTM node is reset after each batch, either for prediction or training.

This is useful if you do not want to use LSTMs in a stateful manner of you want to train with all of the required memory to learn from within each batch.

This does tie into the limit on batch size for prediction. The TF/Theano structures created from this network definition are optimized for the batch size.

I’m super confused here. If the LSTM node is reset after each batch (in this case batch_size 1), does that mean in each forward-backprop session, the LSTM starts with a fresh state without any memory of previous inputs, and it’s only input is a single value? If that’s the case, how could it possibly learn anything?

E.g., let’s say on both time step 10 and 15 the input value is 150, how does the network predict step (10+1) to be 180 and step (15+1) to be 130 while the only input is 150 and the LSTM start with a fresh state?

Hi Mango, I think you’re right. If the number of time-steps is one and the LSTM is not stateful, then I don’t think he is using the recurrent property of the LSTM at all.

Hi!

First of all, thank you for that great post

I have just one small question: For some research work i am working on, I need to make a prediction, so I’ve been looking for the best possible solution and I am guessing its LSTM…

The app. that I am developing is used in a learning environment, so to predict is the probability of a certain student will submit one solution for a certain assignment…

I have data from previous years in this format:

A1 A2 A3 A4 …

Student 1 – Y Y Y Y N Y Y N

Student 2 – N N N N N Y Y Y

…

Where Y means that the student has submitted, and N otherwise…

From what I understood, the best to achieve what I need is by using the solution described in the section “LSTM For Regression Using the Window Method” where my data will be something like

I1 I2 I3 O

N N N N

Y Y Y Y

And when I present a new case like Y N N the “LSTM” will make a prediction according to what has been learnt in the training moment.

Did I understand it right? Do you suggest another way?

Sorry for the eventually dumb question…

Best regards

Looks fine Nuno, although I would suggest you try to frame the problem a few different ways and see what gives you the best results.

Also compare results from LSTMs to MLPs with the window method and ensure it is worth the additional complexity.

Hi Jason,

Very interesting. Is there a function to descale the scaled data (0-1)? You show the data from 0-1. I want to see the original scale data. This is a easy question. But, it is better to show the original scale data, I suppose.

Great point.

Yes, you can save the MinMaxScaler used to scale the training data and use it later to scale new input data and in turn descale predictions. The call is scaler.inverse_transform() from memory.

Why is the shift necessary for plotting the output? Isn’t it unavailable information at time ‘t+1’?

Hi Pacchu, the framing of the problem is to predict t+1, given t, and possibly some subset of t-n.

I hope that is clearer.

Does the output simply mimics the input ? (the copy is shifted by one)

Just like here : https://github.com/fchollet/keras/issues/2856 ?

No, the output is a prediction of the next time step given prior time steps.

Have you tried to use the input value as a prediction? It produces an RMSE similar to what you are getting, 48.66.

Yes, this is call persistence, here is an example:

https://machinelearningmastery.mystagingwebsite.com/persistence-time-series-forecasting-with-python/

i think the point is that the default DNN models are no better than persistence model?

Hi Jason, thanks for the tutorial. Is it because the input features or hyperparameter are not tuned so the prediction (t+1) is only using the input (t)? Thanks

Hi sir

I tried your code for time series prediction. On passing either univariate or multivariate data, the predictions of the target variable are same. Should’nt there be a difference in the predicted values. I expect the predictions to improve with the multivariate data. Please shed some light on this.

The performance of the model is dependent on both the framing of your problem and how the model is configured.

Hi Jason,

Thanks for the wonderful tutorial. It felt great following your code and implementing my first LSTM network. Looking forward to learning a lot more!!

Can we extend time series forecasting problems to multiple time series? I have the following problem in my mind. Suppose we have stock prices of 100 companies (instead of one) and we wanna forecast what happens in the next month for all the companies? Is it possible to use LSTMs and RNNs for such multiple time series problems?

Forecasting stock prices is not my area of expertise. Nevertheless, LSTMs can be used for multiple regression as well as sequence prediction (if you want to predict multiple steps ahead). Give it a shot.

i guess i have the same idea in mind as Madhav..^^ i want to predict multiple time series, each one represent the flow of one grid in the city(since i assume that the neighboured grids influence each other to some extend).. have you done your stock prediction with LSTM?? will you share me some tricks or experience? Thankyou~

I guess the function learnt is only an one-step lag identity (mimic) prediction.

If the code of your basic version runs, it will look like this:

http://imgur.com/BvPnwGu

I change the csv (setting all the data points after some time to be 400 until the end) and run the same code, it will look like this:

http://imgur.com/TvuQDRZ

If it is truly learning the dynamics of the pattern, the prediction should not look like a strict line. At least the previous information before the 400 values will pull down the curve a little bit.

Typo: a *straight line

Clarification: Of course what I said may not be correct. But I think this is an alarming concern to interrupt what the LSTM is really learning behind the scene.

A key to the examples is that the LSTM is capable of learning the sequence, not just the input-output pairs. From the sequence, it is able to predict the next step.

I think Liu is right. because even when I change LSTM to Dense, result is almost the same.

if you use time-step=1. it is actually not LSTM anymore.

Hi Liu,

after investigating a bit, I have concluded that the 1 time-step LSTM is indeed the trivial identity function (you can convince yourself by reducing the layer to 1 neuron, and adding ad-hoc data to the test set, as you have). But if we think about it, this makes alot of sense that the ‘mimic’ function would minimize MSE for such a simple network – it doesn’t see enough time steps to learn the sequence, anyways.

However, if you increase the number of timesteps, you will see that it can reach lower MSE on the test set by slowly moving away from the mimic function to actually learning the sequence, although for low #’s of neurons the approximation will be rougher-looking. I recommend experimenting with the look_back amount, and adding more layers to see how the MSE can be reduced further!

Hi Nicholas,

Thanks for the comment!

I guess the problem (or feature you can say) in the first example is that ‘time-step’ is set to 1 if I understand the API correctly:

trainX = numpy.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))

It means it is feeding sequence of length 1 to the network in each training. Therefore, the RNN/LSTM is not unrolled. The updated internal state is not used anywhere (as it is also resetting the states of the machine in each batch).

I agree with what you said. But by setting timestep and look_back to be 1 as in the first example, it is not learning a sequence at all.

For other readers, I think it worths to look at http://stackoverflow.com/questions/38714959/understanding-keras-lstms

Hi Nicholas,

This is a very good point, thanks for mentioning it.

I have implemented an LSTM on a 1,500 points sample size and indeed sometimes I was wondering whether there really was a big difference with a “mimic” function.

A lot of work to predict the value t+1 while value at t would have been a good enough predictor!

Will try more experiments as I have more data.

hey Liu, it’s a very good observation. I still on the basics and I think these sort of information is really important if we want to build something with quality. Thanks.

Thanks for the tutorial as well.

Hi Jason,

Thanks for this amazing tut, could you please tell me about what is the main role of batch_size in model.fit() and output shape of LSTM layer parameter ?

I read somewhere that using batch_size is depend on our dataset why you chose batch_size = 1 for fitting model and what is the effect of choosing it’s value on calculating gradient of the model?

thanks

Great question Chris.

The batch_size is the number of samples from your train dataset shown to the model at a time. After batch_size samples are run through the network and error calculated, an update to the weights is performed. Too many and the updates are too big, too few, and the updates are too noisy. The hardware you use is also a factor for batch_size and you want to ensure you can fit the batch of samples in memory (e.g. so your GPU can get at them).

I chose a batch_size of 1 because I want to explore and demonstrate LSTMs on time series working with only one sample at a time, but potentially vary the number of features or time steps.

Hi Jason,

Thanks for this series. I have a question for you.

I want to apply a multi-classification problem for videos using LSTM. also, video samples have a different number of frames.

Dataset: samples of videos for actions like boxing, jumping, hand waving, etc.. (Dataset like UCF1101) . each class of action has one label.

so, each video has a label.

Really, I do not know how to describe the data set to LSTM when a number of frames sequence are different from one action to another and I do not know how to use it when a number of frames are fixed as well.

if you have any example of how to use:

LSTM, stacked of LSTM, or CNN with LSTM with this problem this will help me too much.

I wait for your recommendations

Thanks

Hi Jason. Thanks for such a wonderful tutorial. it helped me a lot to get an insight on LSTM’s. I too have a similar question. Can you please comment on this question.

Hi Jason,

Thanks for this great tutorial! I would like to ask, suppose I have a set of X points : X1, X2, .. Xn that contributes to the total sales of the day represented by Y, and I have 60 days data (Y1 until Y60), how do I do time series forecast using these data? Assuming that I would like to predict Y65. Do you have any sample or coding references?

Thanks in advance

I believe you could adapt one of the examples in your post directly to your problem. Little effort required.

Consider normalizing or standardizing your input and output values when working with neural networks.

Hi Jason,

I just found out the question that I have is a multi step ahead prediction, where all the X contributes to Y, and I would like to predict ahead the value of Y n days ahead. Is the example that you gave in this tutorial still relevant?

Thanks!

Hi Alvin,

Yes, you could trivially extend the example to do sequence-to-sequence prediction as it is called in recurrent neural network parlance.

Hi Jason,

Thanks for your reply. I still would like to clarify after looking at the sequence to sequence concept. Assuming I would like to predict the daily total sales (Y), given x1 such as the total number of customers, total item A sold as x2, total item B sold as x3 and so on for the next few items, is sequence to sequence suitable for this?

Thanks

Hi Jason,

I have another question. Looking at your example for the Window method, on line 35:

# reshape input to be [samples, time steps, features]

trainX = numpy.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))

testX = numpy.reshape(testX, (testX.shape[0], 1, testX.shape[1]))

what if I would like to change the time steps to more than 1? What other parts of codes I would need to change? Currently when I change it, it says

ValueError: total size of new array must be unchanged.

Thanks

Hi Jason,

For using stateful LSTM, to predict multiple steps, I came across suggestions to feed the output vector back into the model for the next timestep’s prediction. May I know how to get an output vector from based on your LSTM stateful example?

Hi Alvin,

The LSTM will maintain internal state so that you only need to provide the next input pattern. The LSTM implementation in Keras does require that you provide your data in consistent batch sizes, but when experimenting with this you could reduce the batch size down to 1.

I hope that helps.

Dear Dr Jason,

I was following the instructions line-by-line. I too have this valueerror problem.

I refer to the section:

# create and fit the LSTM network

model = Sequential()

model.add(LSTM(4, input_shape=(1, look_back)))

model.add(Dense(1))

model.compile(loss=’mean_squared_error’, optimizer=’adam’)

model.fit(trainX, trainY, epochs=100, batch_size=1, verbose=2)

As soon as I enter the following code, REGARDLESS of the the size of the epochs, eg, epochs = 10, I get a very large error statement. The following is just some of the error.

ValueError: (‘The following error happened while compiling the node’, forall_inplace,cpu,scan_fn}(Elemwise{maximum,no_inplace}.0, Subtensor{int64:int64:int8}.0, IncSubtensor{InplaceSet;:int64:}.0, IncSubtensor{InplaceSet;:int64:}.0, Elemwise{Maximum}[(0, 0)].0, Subtensor{::, int64:int64:}.0, InplaceDimShuffle{x,0}.0, Subtensor{::, int64:int64:}.0, Subtensor{::, :int64:}.0, InplaceDimShuffle{x,0}.0, Subtensor{::, :int64:}.0, Subtensor{::, int64:int64:}.0, InplaceDimShuffle{x,0}.0, Subtensor{::, int64:int64:}.0, Subtensor{::, int64::}.0, InplaceDimShuffle{x,0}.0, Subtensor{::, int64::}.0), ‘\n’, ‘The following error happened while compiling the node’, Dot22(, ), ‘\n’, ‘invalid token “=” in ldflags_str: “-LC:openblas -lopenblas;gcc.cxxflags = -shared -Ic:ming64winclude -Lc:python34libs -lpython34 -DMS_WIN64″‘)

I have uninstalled Theano, then reinstalled Theano.

My Theano version is 1.0.1, and keras is version ‘2.1.5’

Theano is the not going to be upgraded according to team that ‘created’ it at the Uni of Montreal.

Should I downgrade the keras version and what do I need to do to downgrade.

Thank you,

Anthony of Sydney NSW

I uninstalled Theano and used the following command to install Theano via the git program on my Windows pc:

pip install git+https://github.com/Theano/Theano.git#egg=Theano

Sucessful installatation.

But again as soon as I enter the following line:

model.fit(trainX, trainY, epochs=100, batch_size=1, verbose=2)

I get the following (cut-down) errors:

TypeError: c_compile_args() takes 1 positional argument but 2 were given

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File “C:\Python34\lib\site-packages\theano\tensor\blas.py”, line 443, in _ldflags

t0, t1, t2 = t[0:3]

ValueError: need more than 1 value to unpack

During handling of the above exception, another exception occurred:

lots of files from the Theano package were mentioned (cut out of the edit)

ValueError: (‘The following error happened while compiling the node’, forall_inplace,cpu,scan_fn}(Elemwise{maximum,no_inplace}.0, Subtensor{int64:int64:int8}.0, IncSubtensor{InplaceSet;:int64:}.0, IncSubtensor{InplaceSet;:int64:}.0, Elemwise{Maximum}[(0, 0)].0, Subtensor{::, int64:int64:}.0, InplaceDimShuffle{x,0}.0, Subtensor{::, int64:int64:}.0, Subtensor{::, :int64:}.0, InplaceDimShuffle{x,0}.0, Subtensor{::, :int64:}.0, Subtensor{::, int64:int64:}.0, InplaceDimShuffle{x,0}.0, Subtensor{::, int64:int64:}.0, Subtensor{::, int64::}.0, InplaceDimShuffle{x,0}.0, Subtensor{::, int64::}.0), ‘\n’, ‘The following error happened while compiling the node’, Dot22(, ), ‘\n’, ‘invalid token “=” in ldflags_str: “-LC:openblas -lopenblas;gcc.cxxflags = -shared -Ic:ming64winclude -Lc:python34libs -lpython34 -DMS_WIN64″‘)

Any idea, would be appreciated,

Anthony of Belfield, NSW 2191

Perhaps try working through this tutorial to setup your environment:

https://machinelearningmastery.mystagingwebsite.com/setup-python-environment-machine-learning-deep-learning-anaconda/

Dear Dr Jason,

I did the following in the conda window:

activiate python34

conda update theano

# During this time, there were other packages which were installed including variations of gcc.

conda update keras. # Did not work, package does not exist

conda search -t conda keras # It told me that there was a package under some other directory, which is anaconda/keras.

conda install -c anaconda keras #unsuccessful – need Python v3.6.

When I am in python34 I run the lstm example and still get the same problem.

When I run in conda’s python34, I run the lstm but get different kind of compilation errors.

Conclusion: whether I use an external python34 or conda’s python34, the lstm example does not work. The kinds of errors are different.

Conclusion II: I may go to my alma mater’s wifi (very fast) and download the python36 and reinstall all packages to python36.

Thank you,

Anthony of Belfield

Let me know how you go re Py3.6.

Dear Dr Jason,

I will be transitioning from py3.4.4 to 3.6.

I had another attempt to try the lstm example.

Today, 27/03/2018, I did one more attempt using the anaconda shell.

I either ‘pipped’ or ‘conded’ the most up-to-date files of keras, scikit-learn and theano within the anaconda dos prompt.

So I tried the LSTM example and got the same ‘value error’ that I had under py3.4 which is a separate installation.

In sum, whether I used my independently installed py3.4 or used the anaconda’s py3.4 through the anaconda shell, with all necessary packages updated, the result was the same: run the example, eg in the anaconda shell, python lstmexample.py OR on my independently installed py3.4, python lstexample.py or using my independently installed py3.4 ‘Idle, exec(open(‘lstmexample.py’).read()), THE RESULT WAS THE SAME.

Conclusion: I will be transitioning from py3.4.4 to py3.6. The py3.6 installation will be an independent installation because I have had success with independently installing other python packages and I feel I have a sense of control.

On top of that, since Theano will cease being updated source http://www.i-programmer.info/news/105-artificial-intelligence/11183-theano-to-step-down-after-version-10.html , I will go to Tensorflow which appears to be a going concern. ‘Hopefully’ that should work. Fortunately my laptop’s NVIDIA GPU is suitable for the cuda, I may try that. Ultimately I would like to program on the RPi which does not have an NVIDIA GPU and live within the confines of the RPi’s performance.

I’ll keep you in touch, when my ‘alma mater’ is ‘student-free’ during the Easter holiday season and I can use their very fast wifi.

Thank you,

Anthony of Belfield

Hang in there Anthony. I’m surprised at all the issues you are having. I believe the example works fine with Python 2 and Python 3 and regardless of the Keras backend. Ensure you have copied all of the code required and maintained the indenting.

Dear Dr Jason,

After trying so many attempts at ‘hand-coding’ directly into the Python interpreter I got the “valueerror” problem.

Despite highlighting and saving the code below the sentence “For completeness, below is the entire code example,” I still got the “valueerror” problem. That is I copied the identical code and pasted the copied code in the Notepad++ editor. Indenting was no problem in situation otherwise the interpreter would cease.

I saved the copied code as lstmexample.py.

Execution:

In the Idle interpreter: exec(open(‘c:\python34\lstmexample.py’).read())

via a dos shell: python c:\python34\lstmexample.py

Result same even when I use either the anaconda shell or the dossshell.

So I’ll be resuming my activity when I go to my ‘alma mater’ during the Easter break.

Thank you,

Anthony of Belfield

What version of the Python libraries are you using? Keras? sklearn? pandas?

Here’s what I’m running as of today:

Here’s the exact code (from the first complete example), updated to use a URL to the dataset instead of a local file:

Here is the result:

Dear Dr Jason,

Thank you for your reply:

I saved the above 70 lines as ‘lstmexample2.py’

Based on your above listing of the relevant python packages, with the exception of py3.6, here they are:

python: 3.4.4, Dr Jason’s is v3.6.

scipy: 1.0.1

numpy: 1.14.2

matplotlib: 2.2.2

pandas: 0.22.0

statsmodels: 0.8.0

sklearn: 0.19.1

nltk: 3.2.1 Dr Jason’s 3.2.4 – to pip, updated to 3.2.5

gensim: 0.13.4.1 Dr Jason’s 3.4.0 – to pip, updated to 3.4.0

xgboost: 0.6

tensorflow: n/a since only available for python >= 3.5, Dr Jason’s 1.6

theano: 1.0.1

keras: 2.1.5

In other words, the necessary packages were updated today 29/03/2018 at 0738

Restarted Python either the shell version (in DOS) or Idle version.

Result: with the exception of python3.4.4 and tensorflow (for python >=3.5), all other relevant packages have been updated. The python shell (c:> python c:\python34\lstmexample2.py’) or the Idle (exec(open(‘c:\python34\lstmexample2.py’).read()) produced the same output, “ValueError”

ValueError: (‘The following error happened while compiling the node’, forall_inplace,cpu,scan_fn}(Elemwise{maximum,no_inplace}.0, Subtensor{int64:int64:int8}.0, IncSubtensor{InplaceSet;:int64:}.0, IncSubtensor{InplaceSet;:int64:}.0, Elemwise{Maximum}[(0, 0)].0, Subtensor{::, int64:int64:}.0, InplaceDimShuffle{x,0}.0, Subtensor{::, int64:int64:}.0, Subtensor{::, :int64:}.0, InplaceDimShuffle{x,0}.0, Subtensor{::, :int64:}.0, Subtensor{::, int64:int64:}.0, InplaceDimShuffle{x,0}.0, Subtensor{::, int64:int64:}.0, Subtensor{::, int64::}.0, InplaceDimShuffle{x,0}.0, Subtensor{::, int64::}.0), ‘\n’, ‘The following error happened while compiling the node’, Dot22(, ), ‘\n’, ‘invalid token “=” in ldflags_str: “-LC:openblas -lopenblas;gcc.cxxflags = -shared -Ic:ming64winclude -Lc:python34libs -lpython34 -DMS_WIN64″‘)

>>>

Still puzzled.

Will eventually be transferring to v3.6 during the Easter break.

Thank you,

Anthony of Belfield

Dear Dr Jason,

I have transitioned Python3.4.4 to 3.6.5. I reinstalled the latest scipy, numpy, keras, tensorflow, etc.

I installed tensoflow and ensured that keras ran tensorflow as the background. Incidentally,

I forgot about installing xgboost,

Despite that, I successfully ran the above LSTM example without problem. Went through 100 epochs. The matplotlib’s plot of the predicted and original data was the same.

So the example worked.

Thank you,

Anthony of Belfield

Well done!

Apparently, when using the tensor flow backend, you have to specify input_length in the LTSM constructor. Otherwise, you get an exception. I assume it would just be input_length=1

So like this:

model.add(LSTM(4, input_dim=look_back, input_length=1))

This references the first example where number of features and timesteps is 1. Here input_length corresponds to timesteps.

Hi DRD,

Is this the setting used to solve multi step ahead prediction?

Thanks in advance!

HI,

Haven’t tried it yet, but in the section titled: “LSTM With Memory Between Batches

” input_length should be 3. Basically the same as look_back

Hi Jason,

I applied your technique on stock prediction:

But, I am having some issues.

I take all the historical prices of a stock and frame it the same way the airline passenger prices are in a .csv file.

I use a look_back=20 and I get the following image:

https://postimg.org/image/p53hw2nc7/

Then I try to predict the next stock price and the prediction is not accurate.

Why is the model able to predict the airline passengers so precisely ?

https://postimg.org/image/z0lywvru1/

Thank you

I would suggest tuning the number of layers and number of blocks to suits your problem.

Thank you.

I will play around the network.

In general, For input_dim (windows size), is a smaller or larger number better ?

Hi Jason,

First off, thanks again for this great blog, without you I would be nowhere, with LSTM, and life!

I am running a LSTM model that works, but when I make predictions with “model.predict” it spits out 4000 predictions, which look fine. However, when I run “model.predict” again and save those 4000 predictions again, they are different. From prediction 50 onward, they are all essentially the same, but the first few (that are very important to me) are very different. To give you an idea, the correlation between the first 10 predictions of both rounds is 0.11.

Please help!

The problem wasn’t with numpy.random.seed(0) as I originally thought. I’ve tested this over and over, and even if on the exact same data, predictions are always different/inconsistent for the first few predictions, and only “converge” to some consistent predictions after about 50 predictions have been made previously (on the same or different input data).

It seems like I have made an error by neglecting to include “model.reset_states()” after one line of calling model.predict()

I’m glad to hear you worked it out Marcel.

A good lesson for all of to remember or calls to reset state.

In the part “LSTM For Regression with Time Steps”,

should’t the reshaping be in the form:

[Samples, Features, Time] = (trainX, (trainX.shape[0], trainX.shape[1], 1]

Because in the previous two section:

“LSTM Network For Regression” and

“LSTM For Regression Using the Window Method” we used:

[Samples, Time Steps, Features] = (trainX, (trainX.shape[0], 1, trainX.shape[1])

Thank you

Hi Jason,

Correct me if I’m wrong, but you don’t want to reset_state in the last training iteration do you? Basically my logic is that you want to carry through the last ‘state’ onto the test set because they occur right after the other.

Cheers,

Sachin

You do. The reason is that you can seed the network with state later when you are ready to use it to make a prediction.

Hello Jason,

Am I correct if I was to use Recurrent Neural Networks to predict Dengue Incidences against data on temperature, rainfall, humidity, and dengue incidences.. If so, how would I go about in the processing of my data. I already have the aforementioned data at hand and I have tried using a feed forward neural network using pybrain. It doesn’t seem to get the trend hence my trying of Recurrent Neural Network.

Thank you!

I am a little bit confused regarding the “statefulness”.

If I use a Sequential Model with LSTM layers and stateful set to false. Will this still be a recurrent network that feeds back into my nodes? How would I compare it to the standard LSTM model proposed by Hochreiter et al. (1997)? Do I have to use the stateful layers to mimic the behaviour presented in the original paper?

In essence, I have a simple time series of sales forecasts that show a weekly and partly a yearly pattern. It was easy to create a simple MLP with the Dense layer and the time window method. I put some sales values from the last week, the same week day a few weeks back and the sales of the days roughly a year before into my feature vector. Results are pretty good so far.

I now want to compare it to an LSTM approach. I am however not sure how I can model the weekly and yearly pattern correctly and if I need to use the stateful LSTM or not. Basically I want to use the power of an LSTM to predict a sequence of a longer period of time and hope that the forecasts will be better than with a standard (and much faster) MLP.

These lines don’t make sense to me:

# reshape input to be [samples, time steps, features]

trainX = numpy.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))

testX = numpy.reshape(testX, (testX.shape[0], 1, testX.shape[1]))

Isn’t it [samples, fetaures, timesteps] ?

When you switch to lookback = 3, you still use trainX.shape[0], 1, trainX.shape[1] as your reshape, and aren’t the timesteps the lookback? I noticed the Keras model doesn’t work unless you reshape in this way, which is really strange to me. Why do we have to have a matrix full of 1×1 vectors which hold another vector inside of them? Is this in case we have more features or something? Are there ever any cases where the ‘1’ in the reshape would be any other number?

I think the example given here is wrong in the sense, that each data point represents a sample with one single timestep. This doesn’t make sense at all if you think about the strengths of recurrent networks and especially LSTM. How is the LSTM going to memorize if there’s only one timestep for each sample?

Since we are working on a single time series, it should probably be the other way around, with one sample and n timesteps. One could also try and reduce the number of timesteps and split the time series up into samples of a week or a month depending on the data.

Not sure if this is all correct what I just said, but I get the feeling that this popular tutorial isn’t a 100% correct and therefore a bit misleading for the community.

I do provide multiple examples just so you can compare the methods, both learning by single sample, by window and by timestep.

For this specific problem, the timestep approach is probably the best way to model it, but if you want to use some of these examples as a template for your own problem, you have a few options to choose from.

If I want to predict t+1 in a test set with t, the prediction doesn’t make sense. If I shift the prediction, it make sense but I end up predicting t with t, using a next-step model that learnt the sequence. Nice exercise to get used with the implementation, but what’s the point in real life? I really want a t+1 prediction that match t+1 (not t) in the test set, What I’m missing?

Hi Jason,

This is an excellent tutuorial. I have a question. You have one LSTM (hidden) layer with 4 neurons. What if I construct a LSTM layer with only 1 neuron. Why should I have 4 neurons ? I suppose this is different from having two or layers (depth) ? Depth in my understanding is if you have more layers of LSTM.

If you have 4 LSTM neurons in first layers, does input get fed to all the 4 neurons in a fully connected fashion ? Can you explain that ?

Best Regards,

Easwar

Great question Easwar.

More neurons means more representational capacity at that layer. More layers means more opportunity to abstract hierarchical features.

The number of neurons (LSTM call them blocks) and layers to use is a matter of trial and error (tuning).

If we reduce the number of neurons BELOW the number of features fed into an RNN, then does the model simply use as many features as the neuron number allows ?

For example, if I have 10 features but define a model with only 5 neurons in the initial layer(s), would the model only use the FIRST 5 features ?

Thanks,

Sam

No, I expect it will cause an error. Try it and see.

NO, surprisingly it works very well and gives great prediction results.

Is there a requirement that each feature have a neuron ?

Hi I have found when running your raw example on the data, the training data seems to be shifted to the right of the true plot and not the same as your graph in your first example, why could this be?

Hi Jason,

Nice blog post.

I noticed however, that when you do not scale the input data and switch the activation of the LSTMs to ReLu, you are able to get performance comparable to the feedforward models in your other blog post (https://machinelearningmastery.mystagingwebsite.com/time-series-prediction-with-deep-learning-in-python-with-keras/). The performance becomes: Train Score: 23.07 RMSE, Test Score: 48.59 RMSE

Moreover, when you run the feedforward models in the other blog post with scaling of the input data their performance degrades.

Any idea why scaling the dataset seems to worsen the performance performance?

Cheers,

Stijn

Interesting finding Stijn, thanks for reporting it.

I need to experiment more myself. Not scaling and using ReLu would go hand in hand.

Hi Jason – actually I was able to verify Stjin’s results (could you please delete my inquiry to him).

But I am curious about this:

Train Score: 22.51 RMSE

Test Score: 49.75 RMSE

The error is almost twice as large on the out of sample data, what does that mean about the quality of our model?

I don’t know why my

Train Score: 159586.05 RMSE

Test Score: 189479.02 RMSE

become like this????

Hi Stijn – I wasn’t able to replicate your results, could you please post your code. Thanks!

http://stats.stackexchange.com/questions/59630/test-accuracy-higher-than-training-how-to-interpret

Great link, thanks Max.

Hey Jason,

As far as I can tell (and you’ll have to excuse me if I’m being naive) this isn’t predicting the next timestep at all? Merely doing a good job at mimicking the previous timestep?

For example the with the first example, if we take the first timestep of trainX (trainX[0]) the prediction from the model doesn’t seem to be trying to predict what t+1 (trainX[1]) is, but merely mimics what it thinks fits the model at that particular timestep (trainX[0]) i.e. tries to copy the current timestep. Same for trainX[1], the prediction is not a prediction of trainX[2] but a guess at trainX[1]… Hence which the graphs in the post (which as you mentioned above you need to update) look like they’re forwardlooking, but running the code actually produces graphs which have the predictions shifted t+look_back.

How would you make this a forward looking graph? Hence also, I tried to predict multiple future timesteps with your first model by initialising the first prediction with testX[0] and then feeding the next predictions with the prior predictions but the predictions just plummeted downwards into a downwards curve. Not predicting the next timesteps at all.

Am I being naive to the purpose of this blog post here?

All the best, love your work,

Jakob

Hi Jakob,

I believe you are correct.

I have tried these methods on many different time series and the same mimicking behavior occurs – the training loss is somehow minimized by outputting the previous timestep.

A similar mimicking behavior occurs when predicting multiple time steps ahead as well (for example, if predicting two steps ahead, the model learns to output the previous two timesteps).

There is a small discussion on this issue found here – https://github.com/fchollet/keras/issues/2856 – but besides that I haven’t discovered any ways to combat this (or if there is some underlying problem with Keras, my code, etc.).

I am in the process of writing a blog to uncover this phenomenon in more detail. Will follow up when I am done.

Any other advancements or suggestions would be greatly appreciated!

Thanks,

Jeremy

Hello,

I have been trying to play around with the code for a few days now and all I get is a curve plummeting to zero after a while.

I start to believe that we are using the wrong tool to do what we would like to, although it works for sine waves. We may want to try with stationary time series then, this is the last card I can play.

Are you simply using t-1 to predict t+1 in the time window, if so I don’t think there is enough data being fed into the neural network to learn effectively. with a bigger time window I notice that the model does start to fit better.

Hi, Jason

Thank you for your post.

I am still confused about LSTM for regression with window method and time steps.

Could you explain more about this point. Could you use some figures to show the difference between them?

Many thanks!

As my understanding, the LSTM for regression with window method is the same as a standard MLP method, which has 3 input features and one output as the example. Is this correct? What’s the difference?

Thank you very much for the detailed article!

Does anybody have a scientific source for the time window? I can’t seem to find one.

Great question.

I don’t know of any, but there must be something written up about it back in the early days. I also expect it’s in old neural net texts.

Have you experimented with having predictors (multivariate time series) versus a univariate? Is that possible in Keras?

Yes, you can have multiple input features for multiple regression.

Any chance you will add this type of example?

I will in coming weeks.

Me too will be interested in using multivariate(numerical) data !

Been trying for a few days , but the “reshaping/shaping/data-format-blackmagic” always breaks

Purely cause I don’t yet understand it !

Otherwise great example !

Understood, I’ll prepare tutorials.

Sir, Awesome work!!!

I am very interested in cross-sectional time series estimation… How can that be done?

I am starting your Python track, but will eventually target data with say 50 explanatory variables, with near infinite length of time series observations available on each one. Since the explanatory variables are not independent normal OLS is useless and wish to learn your methods.

I would be most interested in your approach to deriving an optimal sampling temporal window and estimation procedure.

Sorry Richard, I don’t know about cross-sectional time series estimation.

For good window sizes, I recommend designing a study and resolving the question empirically.

Hi Jason

As you mentioned before, you will prepare the tutorial for multiple input features for multiple regression. Could you provide us the link to that tutorial?

It will be part of a new book I am writing. I will put a version of it on the blog soon.

Multiple regression is straight forward with LSTMs, remember input is defined as [samples, timesteps, features]. Your multiple inputs are features.

For multiple output multi-step regression you can recurse the LSTM or change the number of outputs in the output layer.

Did you ever create a tutorial for multivariate LSTM? I can’t seem to find any!

I get this error when I run your script with the Theano backend:

ValueError: (‘The following error happened while compiling the node’, forall_inplace,cpu,scan_fn}(Elemwise{maximum,no_inplace}.0, Subtensor{int64:int64:int8}.0, IncSubtensor{InplaceSet;:int64:}.0, IncSubtensor{InplaceSet;:int64:}.0, Elemwise{Maximum}[(0, 0)].0, lstm_1_U_o, lstm_1_U_f, lstm_1_U_i, lstm_1_U_c), ‘\n’, ‘numpy.dtype has the wrong size, try recompiling’)

Any idea what might be happening?

Hi Bob, it might be a problem with your backend. Try reinstalling Theano or Tensorflow – whichever you are using. Try switching up the one you are using.

Excellent article, really insightful. Do you have an article which expands on this to forecast data? An approach that would mimic that of say arima.predict in statsmodels? So ideally we train/fit the model on historical data, but then attempt to predict future values?

Thanks Philip, I plan to prepare many more time series examples.

Hi,Mr.Brownlee!

I don’t see future values on your plots, as I understand your model don’t predict the Future, only describe History. Can you give advice, how can I do this? And how I can print predictive values?

Thanks a lot!

Great article Jason.

I was just wondering if there was any way I could input more than 1 feature , and have 1 output, which is what I am trying to predict? I am trying to build a stock market predictor. And yes I know, it is nearly impossible to predict the stock market, but I am just testing this, so lets say we live in a perfect world and it can be predicted. How would I do this?

Hi Tucker,

You can have multiple features as inputs.

The input structure for LSTMS is [samples, time steps, features], as explained above. In fact, there are examples of what you are looking for above (see the section on the window method).

Just specify the features (e.g. different indicators) in the third dimension. You may have 1 or more timesteps for each feature (second dimension).

I hope that helps.

I did what you said, but now it wants to output 2 sequences out of the activation layer, but I only wanted it to have a final output of 1. Basically what I am doing is trying to use open and close stock data, and use it to predict tomorrow’s close. So I need to input 2 sequences and have an output of 1. I hope I explained that right. What should I do?

Jason, you mentioned that LSTMs input shape must be [samples, time stamps, features]. What if my time series is sampled (t, x), i.e. each sample has its own time stamp, and the time stamps are NOT evenly spaced. Do I have to generate another time series in which all samples are evenly spaced? Is there any way to handle the original time series?

Really good question Joe. Thanks. I have not thought about this.

My instinct would be to pad the time series, fill in the spaces with zeros and make the time series steps equidistant. At least, I would try that against not doing it and evaluate the effect on performance.

Take samples in blocks via Sklearn.model_selection.TimeSeriesSplit

What an excellent article!

Recently I used LSTM to predict stock market index where the data is fluctuating and has no seasonal pattern like the air passanger data. I was just wondering about how does LSTM (or every gate) decide when to forget or keep a certain value of this type of series data. Any explanation about this? Thank you.

Great question Rio. I would love to work through how the gates compute/output on a specific problem.

I think this would be a new blog post in the future.

I’m looking forward to that post, Jason. Thank you

I used “LSTM For Regression Using the Window Method” with the following parameters

look_back = 20

LSTM(20,input_dim=look_back)

I got the following results

Train_Score: 113.67 RMSE

Test_Score: 122.88 RMSE

I computed R-squared and I got 0.93466300136

In addition I tried changing the hyperparameters in the other two models but R-squared was less in both comparing to this model.

Dear SalemAmeen,

I am currently working with lstm recurrent neural networks and I am curious how you calculated R-squared. Are you willing to share your code about the R-squared calculation with me? Thank you very much!

Kind regards

Cas

If you collect your predictions, you can calculate r^2 using the sklearn library:

http://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html

You can also use a custom metric to report r^2 each epoch:

https://keras.io/metrics/

Hi, Jason

First of all, this is really a fantastic post and thank you so much!

I’ve got confused on the “model.predict(x,batch_size)”.

I can’t figure what it means “predict in a batched way” on the keras official website.

My situation is like:

batch_size=10

I have a test sample [x_1] \in R^{2}, and I put it into the function,

[x_2] = model.predict([x_1],batch_size=batch_size)

(Let’s skip the numpy form issue)

Then, subsequently, I put [x_2] into it, similarly, and I get [x_3] = model.predict([x_2],batch_size=batch_size), and so on, till x_10.

I don’t know if the function “predict” treats [x_1],[x_2],…[x_3] as in a batch ?

I guess it does.(although I didn’t put them into the function at one time)

Otherwise, I’ve tried another way to compute [x_2],…[x_10] and I got the same as above.

Another strategy is like:

[x_2] = model.predict([x_1],batch_size=batch_size)

[x_3] = model.predict([[x_1],[x_2],batch_size=batch_size)

[x_4] = model.predict([[x_1],[x_2],[x_3]],batch_size=batch_size)

…

What’s the difference between the two ways?

Thanks!

btw, I am also confused at “batch”.

If batch_size=1, does that mean there’s no relation between samples? I mean the state s_t won’t be sent to affect the next step s_(t+1).

So, why we need RNN?

It’s a good point.

We need RNN because the state they can maintain gives results better than methods that do not maintain state.

As for batch_size=1 during calls to model.predict() I have not tested whether indeed state is lost as in training, but I expect it may be. I expect one would need batch_size=n_samples and replay data each time a prediction is needed.

I must experiment with this and get back to you.

I’m not sure I understand the “two ways” you’re comparing, sorry.

The batch size is the number of records that the network will process at once – as in load into memory and perform computation upon.

In training, this is the constraint on data before weight update. In test, it is data before computed predictions are returned.

Does that help at all?

yes, it does!

I appreciate it !!

This is a great post! Thanks for the guidance. I’m wondering about performance. I’ve setup my network very similarly to yours, just have a larger data set (about 2500 samples, each with 218 features). Up to about 20 epochs runs in a reasonable amount of time, but anything over that seems to take forever.

I’ve set-up random forests and MLPs, and nothing has run so slowly. I can see all CPUs are being used, so am wondering whether Keras and/or LSTM has performance issues with larger data sets.

Great question.

LSTMs do use more resources than classical networks because of all the internal gates. No significant, but you will notice it at scale.

I have not performed any specific studies on this, sorry.

Hi Jason,

I am confusing about deep learning and machine learning in Stock Market , forex . There are a lot of models which analyses via chart using amibroker or metastock which redraw the history price and take the prediction in that model . Does it call the machine learning or deep learning ?

How is it when we could do farther to make better prediction via deep learning if it’s right ?

Hi Jason, it sounds like you are already using predictive models.

It may fair to call them machine learning. Deep learning is one group of specific techniques you may or may not be using.

There are may ways to improve results, but it is trial and error. I offer some ideas here:

https://machinelearningmastery.mystagingwebsite.com/improve-deep-learning-performance/

Sorry, I don’t know the specifics of stockmarket data.

Hi Jason,

These models do not predict, they extrapolate current value 1 step ahead in more or less obscured way. As seen on the pictures, prediction is just shifted original data. For this data one can achieve much better RMSE 33.7 without neural net, in just one line of code:

trainScore = math.sqrt(mean_squared_error(x[:-1], x[1:]))

Hi Alexander, thanks.

This is good motivation for me/community to go beyond making LSTMs “just work” for time series and dive into how to train LSTMs effectively and even competitively on even very simple problems.

It’s an exciting open challenge.

This is a nice tutorial for starters. Thank you.

However, I have some concerns about the

`create_dataset`

function. I think it just make a simple problem complicated (or even wrong).When

`look_back=1`

, the function is simply equivalent to: dataX = dataset[:len(dataset)-look_back], dataY = dataset[look_back:].When

`look_back`

is larger than 1, the function is wrong: after each iteration,`dataX`

is appended by more than 1 data, but`dataY`

is appended by just 1 data. Finally, dataX will be`look_back`

times larger than dataY.Is that what

`create_dataset`

supposed to do?def create_dataset(dataset, look_back=1):

dataX, dataY = [], []

for i in range(len(dataset)-look_back-1):

a = dataset[i:(i+look_back), 0]

dataX.append(a)

dataY.append(dataset[i + look_back, 0])

return numpy.array(dataX), numpy.array(dataY)

Thanks for your great article! I have a question that when I use the model “Stacked LSTMs With Memory Between Batches” with my own data, I found that cpu is much faster than gpu.

May data contains many files and each files’ size is about 3M. I put each file into the model to

trian one by one. I guess that the data is too small so the gpu is useless, but I can’t sure. I use thano backend and I can sure that the type of the data is float32. So I want to know what reason would make this happen, or the only reason is the data too small? Thank you very much and best wishes to you.

Hi Jason,

Excellent tutorial. I am new to time series prediction. I have a basic question. In this example(international-airline-passengers) model predicted the values on test data from 1957-01 to 1960-12 time period.

How to predict the passengers in next one year from 1961-01 – 1961-12.

How to pass input values to model, so it will predict the passengers count in each month for next one year.

Hi,

Any inputs to solve below question

How to predict the passengers in next one year from 1961-01 – 1961-12.

Regards,

Rajesh

Hi,

“The dataset is available for free from the DataMarket webpage as a CSV download with the filename “international-airline-passengers.csv“.”

Not anymore I guess.

Any other way to get the file?

Thanks,

Nicolas

Yes, it is still available NicoAd.

Visit: https://datamarket.com/data/set/22u3/international-airline-passengers-monthly-totals-in-thousands-jan-49-dec-60

Click “Export” on the left-hand side, and choose the CSV file format.

Thanks !

Just a new location:

https://github.com/ufal/npfl114/blob/master/labs06/international-airline-passengers.tsv

Hi,

Great article on LSTM and keras. I was really struggling with this, until I read through your examples. Now I have a much better understanding and can use LSTM on my own data.

One thing I’d like to point out. The reuse of trainY and trainX on lines 55 & 57.

Line 55 trainY = scaler.inverse_transform([trainY])

This confused me a lot, because the model can’t run fit() or predict() again, after this is done. I was struggling to understand why it could not do a second predict or fit. Until i very carefully read each line of code.

I think renaming the above variables would make the example code clearer.

Unless I am missing something….. and being a novice programmer that’s very possible.

Thanks again for the great work.

Brian.

Thanks Brian, I’m glad the examples were useful to get you started.

Great point about remaining variables.

Hi, Jason, thank you for the example.

I have used the method on my own data. The data is about the prediction of the average temperature per month. I want to predict more than one month. But I can only predict one month now. Because the inputs are X1 X2 X3, the result is only y. I want to kown how to modify the code to use ,like, X1 X2 X3 X4 X5 X6 to predict Y1 Y2 Y3.

I don’t know if I have made it clear. I hope you will help me.

Thank you very much.

Hi Joaco, that is a sequence to sequence problem. I will prepare an example soon.

Hi Jason, Joaco,

I am also looking forward to a similar example 🙂

Je

Sorry do you have prepare this example? Thanks!

Yes, see here:

https://machinelearningmastery.mystagingwebsite.com/learn-add-numbers-seq2seq-recurrent-neural-networks/

Nice post Jason!

I read that GRU has a less complex architecture than the LSTM has, but many people still use LSTM for forecasting. I’d like to ask, what are the advantages LSTM compared to GRU? Thank you

Hi Nida, I would defer to model skill in most circumstances, rather than concerns of computational complexity – unless that is a requirement of your project.

Agreed, we do want the simplest and best performing model possible, so perhaps evaluate GRUs and then see of LSTMs can out perform them on your problem.

I’m a total newbie to Keras and LSTM and most things NN, but if you’ll excuse that, I’d like to run this idea past you just to see if I’m talking the same language let alone on the same page… :

I’m interested in time-series prediction, mostly stocks / commodities etc, and have encountered the same problem as others in these comments, namely, how is it prediction if it’s mostly constrained to within the time-span for which we already have data?

With most ML algorithms I could train the model and implement a shuffle, ie get the previous day’s prediction for today and append it in the input-variable column, get another prediction, … repeat. The worst that would happen is a little fudge around the last day in the learning dataset.

That seems rather laborious if we want to predict how expensive gold is going to be in 6 months’ time.

(Doubly so, since in other worlds (R + RSNNS + elman or jordan), the prediction is bound-up with training so a prediction would involve rebuilding the entire NN for every day’s result, but we digress.)

I saw somewhere Keras has a notion of “masking”, assigning a dummy value that tells the training the values are missing. Would it be possible to use this with LSTM, just append a bunch of 180 mask zeroes, let it train itself on this and then use the testing phase to impute the last values, thereby filling in the blanks for the next 6 months?

It would also be possible to run an ensemble of these models and draw a pretty graph similar to arima.predict with varying degrees of confidence as to what might happen.

Waffle ends.

Interesting idea.

My thoughts go more towards updating the model. A great thing about neural nets is that they are updatable. This means that you can prepare just some additional training data for today/this week and update the weights with the new knowledge, rather than training them from scratch.

Again, the devil is in the detail and often updating may require careful tuning and perhaps balance of old data to avoid overfitting.

Dear Jason,

I am trying to implement your code in order to make forecasting on a time-series that i am receiving from a server. My only problem is that the length of my dataset is continuously increasing. Is there any way to read the last N rows from my csv file? What changes do i have to make in code below in order to succeed it.

def create_dataset(dataset, look_back=1):

dataX, dataY = [], []

for i in range(len(dataset)-look_back-1):

a = dataset[i:(i+look_back), 0]

dataX.append(a)

dataY.append(dataset[i + look_back, 0])

return numpy.array(dataX), numpy.array(dataY)

# fix random seed for reproducibility

numpy.random.seed(7)

# load the dataset

dataframe = pandas.read_csv(‘timeseries.csv’, usecols=[1], engine=’python’, skipfooter=3)

dataset = dataframe.values

dataset = dataset.astype(‘float32’)

This post might help with loading your CSV into memory:

https://machinelearningmastery.mystagingwebsite.com/load-machine-learning-data-python/

If you load your data with Pandas, you can use DataFrame.tail() to select the last n records of your dataset, for example:

Hello, I would like to use that your model. But the problem I am using sliding window size greater than one. Type {[x-2], [x-1], [x]} ==> [x + 1]. But I found several problems in training. For example, when I turn your trainX in {[x-2], [x-1], [x]} and trainY in [x + 1], the keras tells me that the input and the target must have same size. Can you help me with this?

Hi Alisson, I think the error suggests that input and target do not have the same number of rows.

Check your prepared data, maybe even save it to file and look in a text editor or excel.

Thaks, Jason. I was able to solve my problem. But see, the use of the ReLu function in the memory cell and the sigmoid function on the output showed strange behavior. You have some experience with this setting.

Congratulations on the work, this page has helped me a lot.

Hi Jason,

Thanks for your great content.

As you did i upgraded to Keras 1.1.0 and scikit-learn v0.18. however i run Theano v.0.9.0dev3 as im on Windows 10. Also im on Anaconda 3.5. (installed from this article: http://ankivil.com/installing-keras-theano-and-dependencies-on-windows-10/)

Your examples run fine on my setup – but i seem to be getting slightly different results.

For eamples in your first example: # LSTM for international airline passengers problem with window regression framing – i get:

Train Score: 22.79 RMSE

Test Score: 48.80 RMSE

Should i be getting exact the same results as in your tutorial? If yes, any idea what i should be looking at changing?

Best regards

Soren

Great work Soren!

Don’t worry about small differences. It is hard to get 100% reproducible results with Keras/Theano/TensorFlow at the moment. I hope the community can work something out soon.

great post ! thank you so much. I was wondering how can be adapt the code to make multiple-step-ahead prediction. One of the commenters suggested defining the out-put like [x(t+1),x(t+2),x(t+3),…x(t+n)] , but is there a way to make prediction recursively ? More specifically, to build an LSTM with only one output. We first predict x(t+1), then use the predicted x(t+1) as the input for the next time step to predict x(t+2) and continue doing so ‘n’ times.

Hi Jason,

I am wondering how to apply LSTM to real time data. The first change I can see is the data normalisation. Concretely, a new sample could be well out of min max among previous observations. How would you go about this problem?

Thanks.

Could it be that in:

# calculate root mean squared error

math.sqrt(mean_squared_error(trainY[0], trainPredict[:,0]))

you mean :

# calculate root mean squared error

trainScore = math.sqrt(mean_squared_error(trainY[:], trainPredict[:,0]))

trainY is an array of (one) array. Compare:

print len(trainYi[0]) # 720

print len(trainYi[:]) # 1

print len(trainPredict[:,0]) # 720

So the original code is the right one.

Hi jason, i got a question,that have stacked me for many days,how can i add hidden layer into the LSTM model(By using model.add(LSTM()).what i tried : say ,in your first code example, I assume ‘model.add(LSTM(4, input_dim=look_back))’ this line was to create a hidden layer in the LSTM model. So i thought:oh , 1 hidden layer is so easy , why don’t add one hidden layer into it .So i try to add one layer. After the code:’model.add(LSTM(4, input_dim=look_back))’ , i try many ways to insert one hidden layer , such as : just copy model.add(LSTM(4,input_dim=look_back)) and insert after it . I try many ways ,but it always got the error that got the wrong input _ dimension. So can you show me how to add one hidden layer in example 1st . Or , i don’t got the LSTM model right ,it can’t be inserted ?

See the section titled “Stacked LSTMs With Memory Between Batches”.

It gives an example of multiple hidden layers in an LSTM network.

Thanks . I got that.

However, I got another question: compared to the other article you published ‘time series prediction with deep learning ‘(https://machinelearningmastery.mystagingwebsite.com/time-series-prediction-with-deep-learning-in-python-with-keras/?utm_source=tuicool&utm_medium=referral) , It seems that ‘LSTM’ model doesn’t predict as well as the simple neurons. Does that mean LSTM may not a good choice for some specific time series structure?

I would not agree, these are just demonstration projects and were not optimized for top performance.

These examples show how LSTMs could be used for time series projects (and how to use MLPs for time series projects), but not optimally tuned for the problem.

Hi, great post! Thanks

How could I set the input if I have several observations (time series) with same length of the same feature and I want to predict t+1? Would I concatenate them all? In that case the last sample of one observation would predict the first one of the next.. Or should I explicitly assign the length of each time series to the batch_size?

Hi, you’re predicting one day after your last entry, if i want to predict a day five days after what should i do?

Hi Mauro, that would be a sequence to sequence prediction.

Sorry, I don’t have an example just yet.

Hi

This is a great example. I am quite new in deep learning and keras. But this website has been very helpful. I want to learn more.

Like many commenters, I am also requesting to find out: how to predict future time periods. Is that possible? How can I achieve this using the example above? If there are multiple series or Ys and there are categorical predictors, how can I accomodate that?

Please help, and am very keen to learn this via other channels in this website if required. Please let me know.

Many thanks

The example does indeed predict future values.

You can adapt the example and call model.predict to make a prediction for new data, rather than just evaluate the performance of predictions on new data as in the example.

Hi,

I tried to predict future values, but have trouble finding the right way to do it

I work with the window method so my current data à t-3 t-2 t-1 t looks like this

[100,110,108]

If I try

data = [[100,110,108]]

model.predict(data)

I get the following error :

Attribute error : ‘list’ object has no attribute “shape”

I guess the format is not correct, and I need sort of reshape.

but for me the line

trainX = numpy.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1])) is not clear.

and if I try to apply the same transform on my new data I get this error :

IndexError : tuple index out of range

could you provide an exemple dealing with new data ?

Thanks !

I think I almost here but still have a last error (my window side is 8)

todayData = numpy.array([[1086.22,827.62,702.94,779.5,711.8399999999999,1181.25,1908.69,2006.39]])

todayData = todayData.astype(‘float32’)

todayData = scaler.fit_transform(todayData)

print “todayData scaled ” + str(todayData)

todayData = scaler.inverse_transform(todayData)

print “todayData inversed ” + str(todayData)

todayData = numpy.reshape(todayData, (todayData.shape[0], 1, todayData.shape[1]))

predictTomorrow = model.predict(todayData)

predictTomorrow = scaler.inverse_transform([predictTomorrow])

print “prediction” + str(predictTomorrow)

the inverse_transform line on predictTomorrow generate the following error

ValueError : Found array with dim 3 . Estimator expected <= 2

again a reshape issue 🙁

Dear Nico AD:

Glad to know you made it! I am getting into the same problem, can you post your full code?

Thanks!

I recommend starting here instead:

https://machinelearningmastery.mystagingwebsite.com/start-here/#deep_learning_time_series

I am working on a new example Nico, it may be a new blog post.

thanks Jason. I tried various things with no luck. for me some part of the tutorial (like the reshape part / scaling ) are pure magic 🙂 trying to get some help from the keras community on gitter 🙂

finally got it , I need to reshape in (1,1,8) ( where 8 is the look_back size)

Well done Nico.

Why this example does not indeed predict future values?

It does.

I recommend starting here:

https://machinelearningmastery.mystagingwebsite.com/start-here/#deep_learning_time_series

Hi Jason

Which book gives complete examples/codes with time series keras? I want to predict future time periods ahead and want add other predictor variables? Is that achievable?

Please let me know if you have any resources / book that I can purchase.

Many thanks

Deep Learning With Python:

https://machinelearningmastery.mystagingwebsite.com/deep-learning-with-python/

Hi Jason

Thank you for your great tutorial,

I have a question about number of features. How could I have input with 5 variables?

Thank you in advance

Sarah

Hi Sarah, LSTMs take input in the form [samples, timesteps, features], e.g. [n, 1, 5].

You can prepare your data in this way, then set the input configuration of your network appropriately, e.g. input_dim=5.

Nice tutorial, thanks.

I think the line

for i in range(len(dataset)-look_back-1):

should be

for i in range(len(dataset)-(look_back-1)):

Actually, I think its

`for i in range(len(dataset)-look_back):`

and

`testPredictPlot[train_size+(look_back-1):len(dataset)-1, :] = testPredict`

Thanks Adam, I’ll take a look.

Hi Adam, nice blog ! I only have a small suggestion for shifting data: use the shift() method from pandas. Cheers

Great suggestion Ben. I have been using this myself recent to create a lagged dataset.

Hi Jason,

As i am new to RNN, i would like to ask about the difference in stateful:

for i in range(100):

model.fit(trainX, trainY, nb_epoch=1, batch_size=batch_size, verbose=2, shuffle=False)

model.reset_states()

and the stateless:

model.fit(trainX, trainY, nb_epoch=100, batch_size=1, verbose=2)

Do the range of 100(nb_epoch=1) the same as nb_epoch=100?

What is the difference between these 2?

Good question Weixian.

In the stateful case, we are running each epoch manually and resetting the state of the network at the end of each epoch.

In the stateless case, we let the Keras infrastructure run the loop over epochs for us and we’re not concerned with resetting network state.

I hope that is clear.

Hi Jason,

Thanks for the reply.

In this case for the stateful:

if i reset the network, would the next input from the last trained epoch?

For the stateless:

Does it loop from the epochs that was previously trained?

How does the 2 affect the data trained or tested?

Sorry, I don’t understand your questions. Perhaps you could provide more context?

Hi Jason,

stateful:

I mean like the training results of the last epoch [Y1] output for example A

Would the [X2] input of the network be A from the last epoch?

Stateless:

How would the top situation be different from the epoch=2?

Yes, you need to have the same inputs in both cases. The difference is the LSTM is maintaining some internal state when stateful.

Hi Jason

Thank you for your LSTM tutorial.

But i found that an error always occurred, when i ran the first code in ‘model.add(LSTM(4, input_dim=look_back))’

The error is : TypeError: super() argument 1 must be type, not None

So, why?

Thanks

Check your white space Quinn, it’s possible to let extra white space sneak in when doing the copy-paste.

Many thanks for this article. I am trying to wrap my head around

trainX = numpy.reshape(trainX, (trainX.shape[0], trainX.shape[1], 1))

testX = numpy.reshape(testX, (testX.shape[0], testX.shape[1], 1))

(from LSTM for Regression with Time Steps section), since this is exactly what I need.

Let’s say I have 4 videos (prefix v) , of different lengths (say, 2,3,1 sec) (prefix t) , and for every 1 sec, I get a feature vector of length 3 (prefix f).

So, as I understand my trainX would be like this, right? –>

trainX=np.array (

[

[ [v1_t1_f1, v1_t1_f2, v1_t1_f3],

[v1_t2_f1, v1_t2_f2, v1_t2_f3] ],

[ [v2_t1_f1, v2_t1_f2, v2_t1_f3],

[v2_t2_f1, v2_t2_f2, v2_t2_f3],

[v2_t3_f1, v2_t3_f2, v2_t3_f3], ],

[ [v3_t1_f1, v3_t1_f2, v3_t1_f3] ] )

]

(=[ [v1], [v2], [v3] ], and v is Nt x Nf python list?)

If I have v1, v2,v3, how do I start with an **empty** xTrain and update them **recursively** to xTrain, so that xTrain can be used by Keras?

I have tried np.append, np.insert, np.stack methods, but no success as yet, I always get some error. Kindly help!!!

If I make my ‘v1′,’v2′,v3′ ..’v19’ as np arrays, and trainX as a list =[ v1, v2, v3…v19 ] using trainX.append(vn) –> and eventually outside of for loop: trainX=np.array(trainX), I get following error.

File “/usr/local/lib/python2.7/dist-packages/keras/engine/training.py”, line 100, in standardize_input_data

str(array.shape))

Exception: Error when checking model input: expected lstm_input_1 to have 3 dimensions, but got array with shape (19, 1)

Which makes sense since, Keras must be expecting input to have 3 dimensions = (sample,tstep, features).

But how do I fix this???

Your comment is awaiting moderation.

If I make my ‘v1′,’v2′,v3′ ..’v19’ as np arrays, and trainX as a list =[ v1, v2, v3…v19 ] using trainX.append(vn) –> and eventually outside of for loop: trainX=np.array(trainX), I get following error.

File “/usr/local/lib/python2.7/dist-packages/keras/engine/training.py”, line 100, in standardize_input_data

str(array.shape))

Exception: Error when checking model input: expected lstm_input_1 to have 3 dimensions, but got array with shape (19, 1)

Which makes sense since, Keras must be expecting input to have 3 dimensions = (sample,tstep, features).

But how do I fix this???

Guestion about the stateful data representation.

If I understood correctly prepare_data makes repeats the previous look_back sequences.

For example the original data

1

2

3

4

5

6

will become

1 2 3 -> 4

2 3 4 ->5

3 4 5 ->6

Then when you reshape for the stateful LSTM don’t you feed these sequences like this ?

batch 1 sequences [ 1, 2, 3] -> predict 4

batch 2 sequences [ 2, 3, 4] -> predict 5

batch 3 sequences [ 3, 4, 5] -> predict 6

In the stateful RNN shouldn’t it be two batches only that continue one from the next:

batch 1 sequences [ 1, 2, 3] -> predict 4

batch 2 sequences [ 3, 4, 5] -> predict 6

Or alternatively you can have it to return the full state and predict all of them

batch 1 sequences [ 1, 2, 3] -> predict [2, 3, 4]

batch 2 sequences [ 3, 4, 5] -> predict [4, 5, 6]

Thanks,

Ilias

Sorry i mean for the stateful RNN

batch 1 sequences [ 1, 2, 3] -> predict 4

batch 2 sequences [ 4, 5, 6] -> predict 7 (not 3,4,5)

Hi!

First of all, thanks for the tutorial. I’m trying to predict data that are very similar to the example ones. I was playing with the code you gave, but then something very strange happened: if I fit a model using the flight data and i use those hyper parameters to predict white noise I receive a very accurate results. Example:

#Data Generation:

dataset = numpy.random.randint(500, size=(200,1))

dataset = dataset.astype(‘float32’)

#Data Prediction:

scaler = MinMaxScaler(feature_range=(0, 1))

dataset = scaler.fit_transform(dataset)

prediction in red:

https://s17.postimg.org/oavua7uq7/download.png

How could that be possible? White noise should be not predictable, what am I doing wrong?

Sorry, i was doing something very stupid, just ignore my latest post.

Cheers

Ok, sorry again for the last correction, the result was obtained using:

plt.plot(dataset,”b”,model.predict(dataset[:,:,numpy.newaxis]),”r”)

so I’am actually predicting white noise, how could that be possible?

Hi Luca, glad you’re making progress.

If results are too good to be true, they usually are. There will be a bug somewhere.

Kindly reply to my question above as well, please?

How do I shape trainX for 4 videos (v1,..v4) , of different lengths (2,3,1 sec) and for every 1 sec, I get a feature vector [f1 f2 f3] ?

Sorry, I don’t have examples of working with video data. Hopefully soon.

oh, it is not about videos.. Question is about ‘instances/samples’ in general…

I am saying,

Instance1 through instance4 correspond to 2,3,1,5 feature vectors in time respectively, each of dimension 3. How do I shape these to train LSTM?

That is the whole idea behind the section “LSTM for Regression with Time Steps” above, right?

Features of instance1 should not be considered when training LSTM on instance2! Just as your paragraph says:

“Some sequence problems may have a varied number of time steps per sample. For example, you may have measurements of a physical machine leading up to a point of failure or a point of surge. Each incident would be a sample the observations that lead up to the event would be the time steps, and the variables observed would be the features.”

I don’t need *examples of working with video data.* Kindly advise only on how to shape trainX I mentioned above.

look careful, isn’t there +1 shift in your white noise prediction? )))

same as in charts in tutorial?

best prediction for weather tomorrow is: it’ll be exact the same as today. see?

hello

I see many factors for your handling this time series prediction:

-Number of LSTM blocks

-Lookback number

-Epochs

-Activation

-Optimizer

Can you show the order of importance for these in creating a prediction model? Also, you have chosen 4 LSTM blocks, any reason for this?

Great question Prakash. I would say framing of the problem and network topology as the biggest levers.

I have more info on improving performance in deep learning here:

https://machinelearningmastery.mystagingwebsite.com/improve-deep-learning-performance/

I chose 4 neurons (memory modules) with a little trial and error.

Hi Jason,

When I try your “Stacked LSTMs with Memory Between Batches” example as it is, I found the following error. I wonder if you could help to explain what went wrong and how to rectify it please?

Thank you.

ValueError Traceback (most recent call last)

in ()

41 model = Sequential()

42 model.add(LSTM(4, batch_input_shape=(batch_size, look_back, 1), stateful=True, return_sequences=True))

—> 43 model.add(LSTM(4, batch_input_shape=(batch_size, look_back, 1), stateful=True))

44 model.add(Dense(1))

45 model.compile(loss=’mean_squared_error’, optimizer=’adam’)

/home/nbuser/anaconda3_410/lib/python3.5/site-packages/keras/models.py in add(self, layer)

322 output_shapes=[self.outputs[0]._keras_shape])

323 else:

–> 324 output_tensor = layer(self.outputs[0])

325 if type(output_tensor) is list:

326 raise Exception(‘All layers in a Sequential model ‘

/home/nbuser/anaconda3_410/lib/python3.5/site-packages/keras/engine/topology.py in __call__(self, x, mask)

515 if inbound_layers:

516 # This will call layer.build() if necessary.

–> 517 self.add_inbound_node(inbound_layers, node_indices, tensor_indices)

518 # Outputs were already computed when calling self.add_inbound_node.

519 outputs = self.inbound_nodes[-1].output_tensors

/home/nbuser/anaconda3_410/lib/python3.5/site-packages/keras/engine/topology.py in add_inbound_node(self, inbound_layers, node_indices, tensor_indices)

569 # creating the node automatically updates self.inbound_nodes

570 # as well as outbound_nodes on inbound layers.

–> 571 Node.create_node(self, inbound_layers, node_indices, tensor_indices)

572

573 def get_output_shape_for(self, input_shape):

/home/nbuser/anaconda3_410/lib/python3.5/site-packages/keras/engine/topology.py in create_node(cls, outbound_layer, inbound_layers, node_indices, tensor_indices)

153

154 if len(input_tensors) == 1:

–> 155 output_tensors = to_list(outbound_layer.call(input_tensors[0], mask=input_masks[0]))

156 output_masks = to_list(outbound_layer.compute_mask(input_tensors[0], input_masks[0]))

157 # TODO: try to auto-infer shape if exception is raised by get_output_shape_for.

/home/nbuser/anaconda3_410/lib/python3.5/site-packages/keras/layers/recurrent.py in call(self, x, mask)

225 constants=constants,

226 unroll=self.unroll,

–> 227 input_length=input_shape[1])

228 if self.stateful:

229 updates = []

/home/nbuser/anaconda3_410/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py in rnn(step_function, inputs, initial_states, go_backwards, mask, constants, unroll, input_length)

1304 loop_vars=(time, output_ta) + states,

1305 parallel_iterations=32,

-> 1306 swap_memory=True)

1307 last_time = final_outputs[0]

1308 output_ta = final_outputs[1]

/home/nbuser/anaconda3_410/lib/python3.5/site-packages/tensorflow/python/ops/control_flow_ops.py in while_loop(cond, body, loop_vars, shape_invariants, parallel_iterations, back_prop, swap_memory, name)

2634 context = WhileContext(parallel_iterations, back_prop, swap_memory, name)

2635 ops.add_to_collection(ops.GraphKeys.WHILE_CONTEXT, context)

-> 2636 result = context.BuildLoop(cond, body, loop_vars, shape_invariants)

2637 return result

2638

/home/nbuser/anaconda3_410/lib/python3.5/site-packages/tensorflow/python/ops/control_flow_ops.py in BuildLoop(self, pred, body, loop_vars, shape_invariants)

2467 self.Enter()

2468 original_body_result, exit_vars = self._BuildLoop(

-> 2469 pred, body, original_loop_vars, loop_vars, shape_invariants)

2470 finally:

2471 self.Exit()

/home/nbuser/anaconda3_410/lib/python3.5/site-packages/tensorflow/python/ops/control_flow_ops.py in _BuildLoop(self, pred, body, original_loop_vars, loop_vars, shape_invariants)

2448 for m_var, n_var in zip(merge_vars, next_vars):

2449 if isinstance(m_var, ops.Tensor):

-> 2450 _EnforceShapeInvariant(m_var, n_var)

2451

2452 # Exit the loop.

/home/nbuser/anaconda3_410/lib/python3.5/site-packages/tensorflow/python/ops/control_flow_ops.py in _EnforceShapeInvariant(merge_var, next_var)

584 “Provide shape invariants using either the

`shape_invariants`

”585 “argument of tf.while_loop or set_shape() on the loop variables.”

–> 586 % (merge_var.name, m_shape, n_shape))

587 else:

588 if not isinstance(var, (ops.IndexedSlices, sparse_tensor.SparseTensor)):

ValueError: The shape for while_2/Merge_2:0 is not an invariant for the loop. It enters the loop with shape (1, 4), but has shape (?, 4) after one iteration. Provide shape invariants using either the

`shape_invariants`

argument of tf.while_loop or set_shape() on the loop variables.Hi, Jason.

Thank you for your LSTM tutorial! I would like to use it to do some predictions, however, the input_dim is two variables, and the output_dim is one, just like: input: x(t) y(t) output: y(t+1) .I have known that you answered it with the window method. I still have no idea, any suggestions?

This is an informative and fun article. Thanks!

However, for this application, ARIMA and exponential smoothing perform better out of the box without any tuning.

# Compare ARIMA vs. NN

library(forecast)

library(ModelMetrics)

df.raw <- read.csv('international-airline-passengers.csv', stringsAsFactors=FALSE)

df <- ts(df.raw[,2], start=c(1949,1), end=c(1960,12), frequency=12)

# Same train/test split as example

train.size <- floor(length(df) * 0.67)

ts.train <- ts(df[1:train.size], start=c(1949,1), frequency=12)

ts.test <- ts(df[(train.size+1):length(df)], end=c(1960,12), frequency=12)

ts.fit <- auto.arima(ts.train)

ets.fit <- ets(ts.train)

fcast <- forecast(ts.fit, 4*12)

y_hat <- fcast$mean

# Simple ARIMA vs. NN has RMSE = 8.83/26.47 vs. 22.61/51.58 for train/test

ModelMetrics::rmse(ts.train, fcast$fitted)

ModelMetrics::rmse(ts.test, y_hat)

`# ETS is even better ... RMSE 7.25/23.05`

`ets.fcast <- forecast(ets.fit, 4*12)

ModelMetrics::rmse(ts.train, ets.fcast$fitted)

ModelMetrics::rmse(ts.test, ets.fcast$mean)

Agreed Benjamin. The post does show how LSTMs can be used, just not a very good use on this dataset.

Is this Python too?

I have a question is how to predict the data outside the dataset

Hi libra, train your model your training data and make predictions by calling model.predict().

The batch size/pattern dimensions must match what was used to train the network.

Hi, does the dataset need to be continuous … if i have intermittent missing data then is it supposed to work ?

You can use 0 to pad and to mark missing values Nilavra.

Also, try consider imputing and see how that affects performance.

Hi Jason,

This is a wonderful tutorial. As a beginner, just wondering, how do I know when I should add a layer and when I should add more neurons in a layer?

Great question.

More layers offer more “levels of abstraction” or indirection, depending on how you want to conceptualize.

More nodes/modules in a layer offers more “capacity” at one level of abstraction.

Increasing the capacity of the network in terms of layers or neurons in a layer will both require more learning (epochs) or faster learning (learning rate).

What is the magic bullet for a given problem? There’s none. Find a big computer with lots of CPU/RAM and grind away a suite of ideas on a sample of the dataset to see what works well.

Thanks for the reply, another question is, is there a typical scenario we should use stacked LSTM instead of the normal one?

When you need more representation capacity.

It’s a vague answer , because it’s a hard question to answer objectively, sorry.

Hi Jason,

Many thanks for the tutorial. Very useful indeed.

Following up the question from Aubrey Li and your response to that, does it mean that if I double the number of LSTM nodes (from four to eight), it will perform better?. In other words, how did you decide that number of LSTM nodes to be of 4 and not 6 or 8?

Thanks 🙂

Regards

Je

It may perform better but may require a lot more training.

It may also not converge or it may overfit the problem.

Sadly, there is no magic bullet, just a ton of trial and error. This is why we must develop a strong test harness for a given problem and a strong baseline performance for models to out-perform.

Thanks Jason. Please keep throwing all these nice and very informative blogs / tutorials.

Je

Thanks Je.

Hi Jason

I’ve been struggling with a particular problem and I am hoping you can assist. Basically, I’m running a stateful LSTM following the same logic and code as you’ve discussed above and in addition I’ve played around a bit by adding for example a convolutional layer. My issue is with the mean squared error given at the last epoch (where verbose=2 in model.fit) compared to the mean squared error calculated from trainPredict as in the formula you provide above. Please correct me if I am wrong, but my intuition tells me that these two mean square errors should be the same or at least approximately equal because we are predicting on the training set. However, in my case the mean square error calculated from trainPredict is nearly 50% larger than the mean square error at the last epoch of model.fit. Initially, I thought this had something to do with the resetting of states, but this seems not to be the case with only small differences noticed through my investigation. Does anything come to mind of why this may be? I feel like there is something obvious I’m missing here.

model.compile(loss=’mean_squared_error’, optimizer=ada)

for i in range(500):

XXm = model.fit(trainX, trainY, nb_epoch=1, batch_size=bats, verbose=0,

shuffle=False)

model.reset_states()

print(XXm.history)

at epoch 500: {‘loss’: [0.004482088498778204]}

trainPredict = model.predict(trainX, batch_size=bats)

mean_squared_error(trainY, trainPredict[:,0])

Out[68]: 0.0064886363673947768

Thanks

I agree with your intuition, I would expect the last reported MSE to match a manually calculated MSE. Also, it is not obvious from a quick scan where you might be going wrong.

Start off by confirming this expectation on a standalone small network with a contrived or well understand dataset. Say one hidden layer MLP on the normalized boston house price dataset.

This is a valuable exercise because it cuts out all of the problem specific and technique specific code and concerns and gets right to the heart of the matter.

Once achieved, now come back to your project and cut it back to the bone until it achieves the same outcome.

Let me know how you go.

Hi Jason

Thanks for getting back to me.

I followed your suggestion by running a simple MLP using the housing dataset but I’m still seeing differences. Here is my code as well as the output:

%reset -f

import numpy

seed = 50

numpy.random.seed(seed)

import pandas

from keras.models import Sequential

from keras.layers import Dense

from sklearn.metrics import mean_squared_error

dataframe = pandas.read_csv(“housing.csv”, delim_whitespace=True,

header=None)

dataset = dataframe.values

X = dataset[:,0:13]

Y = dataset[:,13]

model = Sequential()

model.add(Dense(13, input_dim=13, init=’normal’, activation=’relu’))

model.add(Dense(1, init=’normal’))

model.compile(loss=’mean_squared_error’, optimizer=’adam’)

nep=100

mhist = model.fit(X, Y, nb_epoch=nep, batch_size=3, verbose=0)

print ‘MSE on last epoch:’, mhist.history[“loss”][nep-1]

PX=model.predict(X)

print ‘Calculated MSE:’, mean_squared_error(Y, PX)

MSE on last epoch: 30.7131816067

Calculated MSE: 28.8423397398

Please advise. Thanks

Apologies. I forgot to scale. Used a MinMaxScaler

scalerX = MinMaxScaler(feature_range=(0, 1))

scalerY = MinMaxScaler(feature_range=(0, 1))

X = scalerX.fit_transform(dataset[:,0:13])

Y = scalerY.fit_transform(dataset[:,13])

MSE on last epoch: 0.00589414117318

Calculated MSE: 0.00565485540125

The difference is about 4%. Perhaps this is negligible?

Might be small differences due to random number generators and platform differences.

Hi Jason,

Thank you for an excellent introduction to using LSTM networks for time series prediction; I learned a great deal from this article. One question I did have: if I wanted to plot the difference between the data and prediction, would it be correct to use something like (in the case of the training data):

plt.plot(trainY[0]-trainPredict[:,0]),plt.show()

Once again, many thanks.

Regards,

David

For the codes with stacked ltsm, I’m getting the following error. Copy paste the whole thing doesn’t work either. Any help?

The shape for while_1/Merge_2:0 is not an invariant for the loop. It enters the loop with shape (1, 4), but has shape (?, 4) after one iteration. Provide shape invariants using either the

`shape_invariants`

argument of tf.while_loop or set_shape() on the loop variables.Ouch, I’ve not seen that before.

Perhaps try StackOverflow or the google group for the backend that you’re using?

Hi Jason.

Thanks for all you valuable advice here.

I have trained a model for time series prediction on a quite big data set, which took 12 hours for 100 epochs.

The results (validation accuracy) stayed flat for the first 90 epochs and then began to move up.

Now wonder how to add more training on top of a trained model in Keras without loosing the training gained from the first 100 epochs?

Best regards

Søren

Hi Søren,

You can save the weights of your network. Then later load them and continue training / refining weights.

See here for more info on saving and loading a network:

https://machinelearningmastery.mystagingwebsite.com/save-load-keras-deep-learning-models/

Hi Jason,

Another question towards the normalisation. Here, we are lucky to have all the data for training and testing. And this has enabled us to normalise the data (MinMaxScaler). However, in real-life, we may not have all the data in one go and in fact it is very likely the case that we will be receiving data from streams. In such cases, we will never has the max or min or even the sum. How do we handle this case (so that we can feed the RNN with the normalised values?).

One obvious solution, perhaps, is calculating this over the running data. But that will be an expensive approach. Or something to do with stochastic sampling strategy ? Any help Jason?

Thanks in advance

Kind Regards

Je

Great question Je.

For normalization we need to estimate the expected extremes of the data (min/max). For standardization we need to estimate the expected mean and standard deviation. These can be stored and used any time to validate and prepare data.

For more on normalizing and standardizing time series data, see this post:

https://machinelearningmastery.mystagingwebsite.com/normalize-standardize-time-series-data-python/

Hi Jason,

Thanks for the response and for the pointer. Useful – I have to say. 🙂

Kind Regards

Je

Glad to hear it!

Thanks Jason for the response and for the pointer. Useful – I have to say. 🙂

Hi Jason,

Great article! I got a lot of benefits from your work.

One question here, lots of LSTM code like yours use such

trainX[1,2,3,4] to target trainY[5]

trainX[2,3,4,5] to target trainY[6]

…

It is possible to make trainY also be time series? like

trainX[1,2,3,4] to target trainY[5,6]

trainX[2,3,4,5] to target trainY[6,7]

…

So the prediction will be done at once rather than 5 and then 6.

Best regards,

Yes, Shaun.

Reform the dataset with two output variables. Then change the number of neurons in the output layer to 2.

I will have an example of this on the blog in coming weeks.

Thanks, I look forward to your example! I really wonder the advantages and disadvantages in doing so.

Hi Jason, I am here again. I have achieved my goal to predict more than one day in this period of time. But now I have another question. I make X=[x1,x2…x30] and Y=[y1,y2…y7], which means I use 30 days to predict 7 days. When predicting y2, actually I used the real value. So here is the question. How can I put my predicted number,like y2,to the X sequence to predict y3? I am looking forward to your answer.

Thank you very much

Hi Jason, a great tutorial. I’m a newbie, and trying to understand this code. My understanding of Keras is that time steps refers to the number of hidden nodes that the system back propagates to through time, and input dimensions refers to the number of ‘features’ for a given input datum (e.g. if we had 2 categorical values, the input dimensions would be 2). So what confuses me about the code is that it tries to model past values (look back) as the number of input dimensions. Timesteps is always set to 1. In that case isn’t the system not behaving like a recurrent network at all but more like an MLP? Thanks!

Hi Kavitha,

The tutorial demonstrates a number of ways that you can use LSTMs, including using lag variables as input features and lag variables as time steps.

Got it, thank you!

hi jason,

thank you for this great Tuto

with one timestep what is the difference between an MLP and lstm

LSTMs are a very different architecture to MLP. The internal state and gates will result in a different mapping function being learned.

Using a single time step input would not be a good use for an LSTM.

Hi Jason, I am looking for a machine learning algorithm that can learn the timing issues like debounce and flip flops in logic circuits and predict an output.

Sounds like an interesting problem Nishat.

This post might help you frame your problem for predictive modeling:

https://machinelearningmastery.mystagingwebsite.com/how-to-define-your-machine-learning-problem/

I think this is wrong :len(dataset)-look_back-1

it should be len(dataset)-look_back

Hi,

I would like to point out that the graphics

LSTM Trained on Regression Formulation of Passenger Prediction Problem

is the most confusing part of the article.

The red line is NOT the actual prediction for 1,2,3, etc. steps ahead. As we can see from the data, you need to know the REAL value just at the time T to predict T+1, it is not based on your prediction in this setup.

If you need to do a prediction for more steps ahead a different approach is needed.

I am still grateful for the parts of the code you have provided, but this part led me way away from my goal.

Hi Jakub, do you have any idea on what approach to take for multi-step ahead prediction?

Hi,

I would control the input of the internal gate of the cell memory. is it a possible thing to do?

In case of yes, what are the function that allow it? Thanks!

I don’t believe this is the case in Keras Salvo. I’m happy to be corrected though.