-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvanilla lstm.py
75 lines (51 loc) · 1.51 KB
/
vanilla lstm.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 18 03:37:09 2019
@author: shaheer
"""
from keras.models import Sequential
from keras.layers import LSTM,Dense,Dropout
import numpy as np
# split a univariate sequence into samples
def split_sequence(sequence, n_steps):
X, y = list(), list()
for i in range(len(sequence)):
# find the end of this pattern
end_ix = i + n_steps
# check if we are beyond the sequence
if end_ix > len(sequence)-1:
break
# gather input and output parts of the pattern
seq_x, seq_y = sequence[i:end_ix], sequence[end_ix]
X.append(seq_x)
y.append(seq_y)
return np.array(X), np.array(y)
#input sequence
raw_seq = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90])
# choose a number of time steps
n_steps = 3
#split into training and testing set
X,y = split_sequence(raw_seq,n_steps)
#sumarizing the data
for i in range(len(X)):
print(X[i], y[i])
# reshape from [samples, timesteps] into [samples, timesteps, features]
n_features = 1
X = X.reshape((X.shape[0], X.shape[1], n_features))
#define model
model = Sequential()
#adding the first hiddin layer
model.add(LSTM(64,activation='relu',input_shape = (n_steps,n_features)))
#adding dropout
model.add(Dropout(0.2))
#adding the output layer
model.add(Dense(1))
#compiling the model
model.compile(optimizer='adam',loss = 'mse')
#fiting the model
model.fit(X,y,epochs=200,verbose=True)
#unseen data
X_input = np.array([80,90,100])
X_input = X_input.reshape(1,n_steps,n_features)
y_pred = model.predict(X_input)