-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmultivariateLSTM.py
More file actions
37 lines (33 loc) · 1.13 KB
/
multivariateLSTM.py
File metadata and controls
37 lines (33 loc) · 1.13 KB
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
from numpy import array
from numpy import hstack
from basicLSTMs import get_vanilla_lstm, get_stacked_lstm
from getSeqs import dataset
# split a multivariate sequence into samples
def split_sequences(sequences, n_steps):
X, y = list(), list()
for i in range(len(sequences)):
# find the end of this pattern
end_ix = i + n_steps
# check if we are beyond the dataset
if end_ix > len(sequences):
break
# gather input and output parts of the pattern
seq_x, seq_y = sequences[i:end_ix, :-1], sequences[end_ix - 1, -1]
X.append(seq_x)
y.append(seq_y)
return array(X), array(y)
# choose a number of time steps
n_steps = 3
# convert into input/output
X, y = split_sequences(dataset, n_steps)
# the dataset knows the number of features, e.g. 2
n_features = X.shape[2]
# define model
model = get_stacked_lstm(n_steps, n_features)
# fit model
model.fit(X, y, batch_size=2, epochs=200, verbose=1)
# demonstrate prediction
x_input = array([[80, 85], [90, 95], [100, 105]])
x_input = x_input.reshape((1, n_steps, n_features))
yhat = model.predict(x_input, verbose=0)
print(yhat)