-
Notifications
You must be signed in to change notification settings - Fork 29
/
models.py
executable file
·281 lines (199 loc) · 8.87 KB
/
models.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!usr/bin/env python
#-*- coding:utf-8 -*-
from layers import *
from utils import *
import gzip
import pickle
#########################################
# LSTM-FC Model architecture #
#########################################
class LSTM_FC_Model:
def __init__(self, num_input=5, num_hidden=[64, 64], num_output=1, clip_at=0.0, scale_norm=0.0):
"""
LSTM-FC Model, lstm layer contributes to learning time series data, dropout helps to prevent overfitting.
Arguments:
num_input: the number of input variables
num_hidden: the number of neurons in each hidden layer
num_output: output size (one in this study)
clip_at: gradient clip
scale_norm: gradient norm scale
Returns:
output (water table depth in this study)
"""
print('Build LSTM_FC Model......')
X = T.fmatrix()
Y = T.fmatrix()
learning_rate = T.fscalar()
dropout_prob = T.fscalar()
self.num_input = num_input
self.num_hidden = num_hidden
self.num_output = num_output
self.clip_at = clip_at
self.scale_norm = scale_norm
inputs = InputLayer(X, name='inputs')
num_prev = num_input
prev_layer = inputs
self.layers = [inputs]
for i, num_curr in enumerate(num_hidden):
lstm = LSTMLayer(num_prev, num_curr, input_layers=[prev_layer], name="lstm{0}".format(i + 1))
num_prev = num_curr
prev_layer = lstm
self.layers.append(prev_layer)
prev_layer = DropoutLayer(prev_layer, dropout_prob)
self.layers.append(prev_layer)
fc = FullyConnectedLayer(num_prev, num_output, input_layers=[prev_layer], name="yhat")
self.layers.append(fc)
Y_hat = fc.output()
loss = T.sum((Y - Y_hat) ** 2)
params = get_params(self.layers)
updates, grads = sgd(loss, params, learning_rate)
self.train_func = theano.function([X, Y, learning_rate, dropout_prob], loss, updates=updates, allow_input_downcast=True)
self.predict_func = theano.function([X, dropout_prob], Y_hat, allow_input_downcast=True)
def fit(self, X, Y, learning_rate, dropout_prob):
return self.train_func(X, Y, learning_rate, dropout_prob)
def predict(self, X):
return self.predict_func(X, 0.0)
def save_model_params(self, filename):
to_save = {'num_input': self.num_input, 'num_hidden': self.num_hidden,
'num_output': self.num_output}
for layer in self.layers:
for p in layer.get_params():
assert (p.name not in to_save)
to_save[p.name] = p.get_value()
with gzip.open(filename, 'wb') as f:
pickle.dump(to_save, f)
def load_model_params(self, filename):
f = gzip.open(filename, 'rb')
to_load = pickle.load(f)
assert (to_load['num_input'] == self.num_input)
assert (to_load['num_output'] == self.num_output)
for layer in self.layers:
for p in layer.get_params():
p.set_value(floatX(to_load[p.name]))
##########################################
# FFNN Model architecture #
##########################################
class FFNN_Model:
def __init__(self, num_input=256, num_hidden=[64,64], num_output=1, clip_at=0.0, scale_norm=0.0):
"""
FFNN Model, two hidden fully-connected layers.
Arguments:
num_input: the number of input variables
num_hidden: the number of neurons in each hidden layer
num_output: output size (one in this study)
clip_at: gradient clip
scale_norm: gradient norm scale
Returns:
output (water table depth in this study)
"""
print('Build FFNN Model......')
X = T.fmatrix()
Y = T.fmatrix()
learning_rate = T.fscalar()
dropout_prob = T.fscalar()
self.num_input = num_input
self.num_hidden = num_hidden
self.num_output = num_output
self.clip_at = clip_at
self.scale_norm = scale_norm
inputs = InputLayer(X, name='inputs')
num_prev = num_input
prev_layer = inputs
self.layers = [inputs]
fc = FullyConnectedLayer(num_prev, num_hidden, input_layers=[prev_layer], name="fc")
num_prev = num_hidden
prev_layer = fc
self.layers.append(prev_layer)
prev_layer = DropoutLayer(prev_layer, dropout_prob)
self.layers.append(prev_layer)
fc = FullyConnectedLayer(num_prev, num_output, input_layers=[prev_layer], name="yhat")
self.layers.append(fc)
Y_hat = fc.output()
loss = T.sum((Y - Y_hat) ** 2)
params = get_params(self.layers)
updates, grads = sgd(loss, params, learning_rate)
self.train_func = theano.function([X, Y, learning_rate, dropout_prob], loss, updates=updates, allow_input_downcast=True)
self.predict_func = theano.function([X, dropout_prob], Y_hat, allow_input_downcast=True)
def fit(self, X, Y, learning_rate, dropout_prob):
return self.train_func(X, Y, learning_rate, dropout_prob)
def predict(self, X):
return self.predict_func(X, 0.0) # in predict time, dropout = 0
def save_model_params(self, filename):
to_save = {'num_input': self.num_input, 'num_hidden': self.num_hidden,
'num_output': self.num_output}
for layer in self.layers:
for p in layer.get_params():
assert (p.name not in to_save)
to_save[p.name] = p.get_value()
with gzip.open(filename, 'wb') as f:
pickle.dump(to_save, f)
def load_model_params(self, filename):
f = gzip.open(filename, 'rb')
to_load = pickle.load(f)
assert (to_load['num_input'] == self.num_input)
assert (to_load['num_output'] == self.num_output)
for layer in self.layers:
for p in layer.get_params():
p.set_value(floatX(to_load[p.name]))
#########################################
# Double-LSTM Model architecture #
#########################################
class Double_LSTM_Model:
"""
Double-LSTM Model, two hidden lstm layers.
Arguments:
num_input: the number of input variables
num_hidden: the number of neurons in each hidden layer
num_output: output size (one in this study)
clip_at: gradient clip
scale_norm: gradient norm scale
Returns:
output (water table depth in this study)
"""
def __init__(self, num_input=256, num_hidden=[64,64], num_output=1, clip_at=0.0, scale_norm=0.0):
print('Build Double_LSTM Model......')
X = T.fmatrix()
Y = T.fmatrix()
learning_rate = T.fscalar()
dropout_prob = T.fscalar()
self.num_input = num_input
self.num_hidden = num_hidden
self.num_output = num_output
self.clip_at = clip_at
self.scale_norm = scale_norm
inputs = InputLayer(X, name='inputs')
self.layers = [inputs]
lstm = LSTMLayer(num_input, num_hidden, input_layers=[inputs], name="lstm{0}".format(1))
self.layers.append(lstm)
lstm_dropout = DropoutLayer(lstm, dropout_prob)
self.layers.append(lstm_dropout)
lstm = LSTMLayer(num_hidden, num_output, input_layers=[lstm_dropout], name="lstm{0}".format(2))
self.layers.append(lstm)
Y_hat = lstm.output()
loss = T.sum((Y - Y_hat) ** 2)
params = get_params(self.layers)
updates, grads = sgd(loss, params, learning_rate, self.clip_at, self.scale_norm)
self.train_func = theano.function([X, Y, learning_rate, dropout_prob], loss, updates=updates,
allow_input_downcast=True)
self.predict_func = theano.function([X, dropout_prob], Y_hat, allow_input_downcast=True)
def fit(self, X, Y, learning_rate, dropout_prob):
return self.train_func(X, Y, learning_rate, dropout_prob)
def predict(self, X):
return self.predict_func(X, 0.0) # in predict time, dropout = 0
def save_model_params(self, filename):
to_save = {'num_input': self.num_input, 'num_hidden': self.num_hidden,
'num_output': self.num_output}
for layer in self.layers:
for p in layer.get_params():
assert (p.name not in to_save)
to_save[p.name] = p.get_value()
with gzip.open(filename, 'wb') as f:
pickle.dump(to_save, f)
def load_model_params(self, filename):
f = gzip.open(filename, 'rb')
to_load = pickle.load(f)
assert (to_load['num_input'] == self.num_input)
assert (to_load['num_output'] == self.num_output)
for layer in self.layers:
for p in layer.get_params():
p.set_value(floatX(to_load[p.name]))