Skip to content

Instantly share code, notes, and snippets.

@javierlorenzod
Forked from lukovkin/multi-ts-lstm.py
Created July 10, 2017 17:32
Show Gist options
  • Save javierlorenzod/920be6d48896fc0ee7984f75f8197df7 to your computer and use it in GitHub Desktop.
Save javierlorenzod/920be6d48896fc0ee7984f75f8197df7 to your computer and use it in GitHub Desktop.
Time series prediction with multiple sequences input - LSTM - 1
# Time Series Testing
import keras.callbacks
from keras.models import Sequential
from keras.layers.core import Dense, Activation, Dense, Dropout
from keras.layers.recurrent import LSTM
# Call back to capture losses
class LossHistory(keras.callbacks.Callback):
def on_train_begin(self, logs={}):
self.losses = []
def on_batch_end(self, batch, logs={}):
self.losses.append(logs.get('loss'))
# You should get data frames with prices somewhere, e.g. on Quandl - implementation is up to you
# merge data frames
merged = df1.merge(df2, left_index=True, right_index=True, how='inner').dropna()
# data prep
# use 100 days of historical data to predict 10 days in the future
data = merged.values
examples = 100
y_examples = 10
nb_samples = len(data) - examples - y_examples
# input - 2 features
input_list = [np.expand_dims(np.atleast_2d(data[i:examples+i,:]), axis=0) for i in xrange(nb_samples)]
input_mat = np.concatenate(input_list, axis=0)
# target - the first column in merged dataframe
target_list = [np.atleast_2d(data[i+examples:examples+i+y_examples,0]) for i in xrange(nb_samples)]
target_mat = np.concatenate(target_list, axis=0)
# set up model
trials = input_mat.shape[0]
features = input_mat.shape[2]
hidden = 64
model = Sequential()
model.add(LSTM(hidden, input_shape=(examples, features)))
model.add(Dropout(.2))
model.add(Dense(y_examples))
model.add(Activation('linear'))
model.compile(loss='mse', optimizer='rmsprop')
# Train
history = LossHistory()
model.fit(input_mat, target_mat, nb_epoch=100, batch_size=400, callbacks=[history])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment