Skip to content

Instantly share code, notes, and snippets.

@lukovkin
Last active November 25, 2022 16:23
Show Gist options
  • Save lukovkin/1aefa4509e066690b892 to your computer and use it in GitHub Desktop.
Save lukovkin/1aefa4509e066690b892 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])
@SyedHasnat
Copy link

Hello!
Would you like to help me?
I am stuck on a problem, how to fit a multiple input CNN or LSTM model.
I have a time series data divided (split) into train, test, and validation
I want to give the same training data to two different CNN models and concatenate them. I have built the model as shown in the figure, input shape =168,23
IMG-20220810-WA0058
My network is;
IMG-20220810-WA0055
but I am not getting how to fit this model.
once I tried to fit the model I face the error
IMG-20220810-WA0059(1)

@sarahboufelja @bv123 @kascesar @Sudarsan9966 will be waiting for your kind reply.
Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment