I am trying to configure Keras LSTM for a simple regression task. There are several key explanations on the official page: Keras RNN Documentation
But to fully understand, example configurations with sample data would be extremely helpful.
I barely found examples for regression with Keras-LSTM. Most examples relate to classification (text or images). I studied the LSTM examples that come with the Keras distribution, and one example that I found on a Google search: http://danielhnyk.cz/ It offers some insight, although the author assumes that the approach is quite memory inefficient, as the data samples must be kept very redundant.
Although the comment was added by a commenter (Taha), the data store is still redundant, I doubt that it was the way it was done by the Keras developers.
I downloaded some simple examples of serial data, which turned out to be stock data from Yahoo finance. It is freely available from Yahoo Finance Data.
Date, Open, High, Low, Close, Volume, Adj Close 2016-05-18, 94.160004, 95.209999, 93.889999, 94.559998, 41923100, 94.559998 2016-05-17, 94.550003, 94.699997, 93.010002, 93.489998, 46507400, 93.489998 2016-05-16, 92.389999, 94.389999, 91.650002, 93.879997, 61140600, 93.879997 2016-05-13, 90.00, 91.669998, 90.00, 90.519997, 44188200, 90.519997
The table consists of over 8900 such Apple stock data rows. For each day, there are 7 columns = data. The forecast value will be "AdjClose", which is the value at the end of the day.
Thus, the goal would be to predict AdjClose the next day, based on the sequence of the previous few days. (This is probably almost impossible, but it's always good to see how the tool behaves in difficult conditions.)
I think this should be a very standard case of prediction / regression for LSTM and easily ported to other problem areas.
So, how should data be formatted (X_train, y_train) for minimal redundancy, and how to initialize a sequential model with just one LSTM layer and several hidden neurons?
Regards, Theo
PS: I started to code this:
... X_train Out[6]: array([[ 2.87500000e+01, 2.88750000e+01, 2.87500000e+01, 2.87500000e+01, 1.17258400e+08, 4.31358010e-01], [ 2.73750019e+01, 2.73750019e+01, 2.72500000e+01, 2.72500000e+01, 4.39712000e+07, 4.08852011e-01], [ 2.53750000e+01, 2.53750000e+01, 2.52500000e+01, 2.52500000e+01, 2.64320000e+07, 3.78845006e-01], ..., [ 9.23899994e+01, 9.43899994e+01, 9.16500015e+01, 9.38799973e+01, 6.11406000e+07, 9.38799973e+01], [ 9.45500031e+01, 9.46999969e+01, 9.30100021e+01, 9.34899979e+01, 4.65074000e+07, 9.34899979e+01], [ 9.41600037e+01, 9.52099991e+01, 9.38899994e+01, 9.45599976e+01, 4.19231000e+07, 9.45599976e+01]], dtype=float32) y_train Out[7]: array([ 0.40885201, 0.37884501, 0.38822201, ..., 93.87999725, 93.48999786, 94.55999756], dtype=float32)
While the data is ready. The introduction of redundancy is absent. Now the question is how to describe the KERS LSTM model / learning process from this data.
EDIT 3:
Here is the updated code with the three-dimensional data structure needed for repeating networks. (See Lorrit's Answer). However, this does not work.
EDIT 4: remove the extra comma after activation ("sigmoid"), form Y_train correctly. All the same mistakes.
import numpy as np from keras.models import Sequential from keras.layers import Dense, Activation, LSTM nb_timesteps = 4 nb_features = 5 batch_size = 32
There is still a data problem, Keras says:
Using Theano backend. Using gpu device 0: GeForce GTX 960 (CNMeM is disabled, cuDNN not available)Build model... Traceback (most recent call last): File "<ipython-input-1-3a6e9e045167>", line 1, in <module> runfile('C:/Users/admin/Documents/pycode/lstm/lstm5.py', wdir='C:/Users/admin/Documents/pycode/lstm') File "C:\Users\admin\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 699, in runfile execfile(filename, namespace) File "C:\Users\admin\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 74, in execfile exec(compile(scripttext, filename, 'exec'), glob, loc) File "C:/Users/admin/Documents/pycode/lstm/lstm5.py", line 79, in <module> Activation('sigmoid') File "d:\git\keras\keras\models.py", line 93, in __init__ self.add(layer) File "d:\git\keras\keras\models.py", line 146, in add output_tensor = layer(self.outputs[0]) File "d:\git\keras\keras\engine\topology.py", line 441, in __call__ self.assert_input_compatibility(x) File "d:\git\keras\keras\engine\topology.py", line 382, in assert_input_compatibility str(K.ndim(x))) Exception: Input 0 is incompatible with layer lstm_1: expected ndim=3, found ndim=2