-
Notifications
You must be signed in to change notification settings - Fork 0
/
lstm.py
59 lines (45 loc) · 1.52 KB
/
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
from tensorflow.keras.layers import Dense, Activation, Dropout, LSTM # Corrected import statement
from keras.models import Sequential
from keras.metrics import mean_squared_error
def build_improved_model(input_dim, output_dim, return_sequences):
"""
Builds an improved Long Short term memory model using keras.layers.recurrent.lstm
:param input_dim: input dimension of model
:param output_dim: ouput dimension of model
:param return_sequences: return sequence for the model
:return: a 3 layered LSTM model
"""
model = Sequential()
model.add(LSTM(
input_shape=(None, input_dim),
units=output_dim,
return_sequences=return_sequences))
model.add(Dropout(0.2))
model.add(LSTM(
128,
return_sequences=False))
model.add(Dropout(0.2))
model.add(Dense(
units=1))
model.add(Activation('linear'))
return model
def build_basic_model(input_dim, output_dim, return_sequences):
"""
Builds a basic lstm model
:param input_dim: input dimension of the model
:param output_dim: output dimension of the model
:param return_sequences: return sequence of the model
:return: a basic lstm model with 3 layers.
"""
model = Sequential()
model.add(LSTM(
input_shape=(None, input_dim),
units=output_dim,
return_sequences=return_sequences))
model.add(LSTM(
100,
return_sequences=False))
model.add(Dense(
units=1))
model.add(Activation('linear'))
return model