-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPrediction-DNN_Saaed.py
408 lines (258 loc) · 12.1 KB
/
Prediction-DNN_Saaed.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
import numpy as np
import tensorflow as tf
import matplotlib.pylab as plt
def Fc_layer(input, Cache, flag, channel_in, channel_out, Dropout, keepProb, is_training, name="FC"):
with tf.name_scope(name):
W1 = tf.get_variable(name=name + 'W1', shape=[channel_out, channel_in], dtype=tf.float32,
initializer=tf.contrib.layers.xavier_initializer())
W2 = tf.get_variable(name=name + 'W2', shape=[channel_out, channel_in], dtype=tf.float32,
initializer=tf.contrib.layers.xavier_initializer())
Z1 = tf.matmul(W1, input)
Z1 = tf.transpose(Z1)
param_initializers = {'beta': tf.zeros_initializer(), 'gamma': tf.ones_initializer()}
Z1 = tf.contrib.layers.batch_norm(Z1, decay=0.99, center=True, scale=True,
updates_collections=tf.GraphKeys.UPDATE_OPS, is_training=is_training,
data_format='NHWC', param_initializers=param_initializers,
scope=name + '2b')
Z1 = tf.transpose(Z1)
Z2 = tf.matmul(W2, input)
Z2 = tf.transpose(Z2)
param_initializers = {'beta': tf.zeros_initializer(), 'gamma': tf.ones_initializer()}
Z2 = tf.contrib.layers.batch_norm(Z2, decay=0.99, center=True, scale=True,
updates_collections=tf.GraphKeys.UPDATE_OPS, is_training=is_training,
data_format='NHWC', param_initializers=param_initializers,
scope=name + '2a')
Z2 = tf.transpose(Z2)
if flag == False:
A = tf.maximum(Z1, Z2)
else:
A = tf.maximum(Z1 + Cache, Z2 + Cache)
if Dropout:
A = tf.nn.dropout(A, keep_prob=keepProb, noise_shape=[channel_out, 1])
print("Dropout_added", keepProb)
L2 = tf.norm(W1, ord=2) + tf.norm(W2, ord=2)
return A, L2
def Fc_layerL(input, channel_in, channel_out, Dropout, keep_prob, name="FCL"):
with tf.name_scope(name):
W1 = tf.get_variable(name=name + 'W1', shape=[channel_out, channel_in], dtype=tf.float32,
initializer=tf.contrib.layers.xavier_initializer())
b1 = tf.get_variable(name=name + 'b1', shape=[channel_out, 1], dtype=tf.float32,
initializer=tf.zeros_initializer())
Z1 = tf.matmul(W1, input) + b1
A = tf.maximum(Z1, 0.0)
if Dropout:
A = tf.nn.dropout(A, keep_prob=keep_prob, noise_shape=[channel_out, 1])
print("Added Dropout", keep_prob)
L1 = tf.norm(W1, ord=1)
return A, L1
def Fc_layerLL(input, Cache, channel_in, channel_out, Dropout, keep_prob, is_training, name="FCL"):
with tf.name_scope(name):
W1 = tf.get_variable(name=name + 'W1', shape=[channel_out, channel_in], dtype=tf.float32,
initializer=tf.contrib.layers.xavier_initializer())
b1 = tf.get_variable(name=name + 'b1', shape=[channel_out, 1], dtype=tf.float32,
initializer=tf.zeros_initializer())
Z = tf.matmul(W1, input) + b1
A = Z
if Dropout:
A = tf.nn.dropout(A, keep_prob=1, noise_shape=[channel_out, 1])
L2 = tf.norm(W1, ord=2)
return A, L2
def Cost_function(Y, Yhat):
E = Y - Yhat
E2 = tf.pow(E, 2)
MSE = tf.squeeze(tf.reduce_mean(E2, axis=1))
RMSE = tf.pow(MSE, 0.5)
Loss = tf.losses.huber_loss(Y, Yhat, weights=1.0, delta=1.0)
return RMSE, MSE, E, Loss
def main_model(X_training,Y_training,X_test, Y_test, layer, Max_it,learning_rate,batch_size_tr, batch_size_te,keepProb,gamma1, gamma2, Dropout=False):
with tf.device("/gpu:0"):
X_t = tf.placeholder(shape=[n_x, None], dtype=tf.float32)
Y_t = tf.placeholder(shape=[1, None], dtype=tf.float32)
is_tarining = tf.placeholder(dtype=tf.bool)
lr = tf.placeholder(dtype=tf.float32)
H, L1 = Fc_layerL(X_t, n_x, layer[1], Dropout, keepProb[1], name="FC1")
Cashe = H
L2 = 0
for i in range(1, len(layer) - 2):
Name = "FCC" + str(i + 1)
if i % 2 == 0:
flag = True
H, L = Fc_layer(H, Cashe, flag, layer[i], layer[i + 1], Dropout, keepProb[i + 1], is_tarining,
name=Name)
Cashe = H
print("Shortcut Added", i + 1)
else:
flag = False
H, L = Fc_layer(H, Cashe, flag, layer[i], layer[i + 1], Dropout, keepProb[i + 1], is_tarining,
name=Name)
L2 = L2 + L
Dropout1 = False
# H = Cashe + H
Yhat3, L = Fc_layerLL(H, Cashe, layer[-2], layer[-1], Dropout1, 1, is_tarining, name="FC_L")
L2 = L2 + L
print(L2)
print(Yhat3)
Yhat3 = tf.identity(Yhat3, name='Yhat')
with tf.name_scope("Cost_function3"):
RMSE3, MSE, E, Loss3 = Cost_function(Y_t, Yhat3)
print(RMSE3)
RMSE3 = tf.identity(RMSE3, name='RMSE')
TLoss = tf.constant(1.0, dtype=tf.float32) * Loss3 \
+ tf.constant(gamma1, tf.float32) * L1 + tf.constant(gamma2, tf.float32) * L2
with tf.name_scope("Train"):
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(update_ops):
train_op = tf.train.AdamOptimizer(lr).minimize(TLoss)
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
m_t = X_training.shape[1]
LossTr = []
LossTe = []
total_parameters = 0
for variable in tf.trainable_variables():
# shape is an array of tf.Dimension
print(variable)
shape = variable.get_shape()
# print(shape)
# print(len(shape))
variable_parameters = 1
for dim in shape:
# print(dim)
variable_parameters *= dim.value
# print(variable_parameters)
total_parameters += variable_parameters
print("total_parameters", total_parameters)
for i in range(Max_it):
if i%50000==0 and i>0:
learning_rate=learning_rate/2
print('learning rate decayed',learning_rate)
I = np.random.randint(m_t, size=batch_size_tr)
Batch_X = X_training[:, I]
Batch_Y = Y_training[0, I]
Batch_Y = Batch_Y.reshape(1, len(Batch_Y))
sess.run(train_op, feed_dict={X_t: Batch_X, Y_t: Batch_Y, is_tarining: True,lr:learning_rate})
if i % 5000 == 0:
#I1 = np.random.randint(X_test.shape[1], size=batch_size_te)
rmse_tr,yhat_tr_n = sess.run([RMSE3,Yhat3], feed_dict={X_t:X_training, Y_t: Y_training, is_tarining: False})
ctr = np.corrcoef(np.squeeze(yhat_tr_n), np.squeeze(Y_training))[0, 1]
rmse_te,yhat_te_n = sess.run([RMSE3,Yhat3],
feed_dict={X_t: X_test, Y_t: Y_test.reshape(1, -1),
is_tarining: False})
LossTe.append(rmse_te)
LossTr.append(rmse_tr)
ce = np.corrcoef(np.squeeze(yhat_te_n), np.squeeze(Y_test))[0, 1]
print("Iteration %d , The training RMSE is %f and C is %f and test RMSE is %f and C %f" % (i, rmse_tr, ctr,rmse_te,ce))
rmse_te, Yhat_te = sess.run([RMSE3, Yhat3],
feed_dict={X_t: X_test, Y_t: Y_test, is_tarining: False})
rmse_tr, Yhat_tr = sess.run([RMSE3, Yhat3],
feed_dict={X_t: X_training, Y_t: Y_training, is_tarining: False})
saver = tf.train.Saver()
saver.save(sess, './saved_model_corn_2018_DNN21', global_step=i)
print(rmse_te,rmse_tr)
ctr = np.corrcoef(np.squeeze(Yhat_tr), np.squeeze(Y_training))[0, 1]
print(ctr)
ce = np.corrcoef(np.squeeze(Yhat_te), np.squeeze(Y_test))[0, 1]
print('test C',ce)
return rmse_tr, rmse_te, Yhat_tr, Yhat_te, Y_training, Y_test, LossTr, LossTe
X_training=np.load('X_training.npz')
Y_training=np.load('Y_training.npz')
X_test=np.load('X_test.npz')
Y_test=np.load('Y_test.npz')
n_x = X_training.shape[0]
n_l1 = 21 # Number of layers with n1 neurons
n_l2 = 0 # Number of layers with n2 neurons
n_l3 = 0 # Number of layers with n3 neurons
L1 = np.ones([1, n_l1], dtype=int) * 50 #Number of neurons n1
L2 = np.ones([1, n_l2], dtype=int) * 50 ##Number of neurons n2
L3 = np.ones([1, n_l3], dtype=int) * 50 # #Number of neurons n3
layer = np.concatenate(([[n_x]], L1, L2, L3, [[1]]), axis=1)
layer = np.squeeze(layer)
print(layer.shape)
Max_it = 300000 # Max iteration
learning_rate = 0.0003 # Learning rate
gamma1 = 0.0006 # 0.006 # L1 regularization amount for Layer 1
gamma2 = 0.0002 # 0.002 # L2 regularization amount for other layers
K1 = np.ones([1, n_l1], dtype=float) * 1
K2 = np.ones([1, n_l2], dtype=float) * 1
K3 = np.ones([1, n_l3], dtype=float) * 1
keepProb = np.concatenate(([[1]], K1, K2, K3, [[1]]), axis=1)
keepProb = np.squeeze(keepProb)
keepProb[-2] = 1
keepProb[-3] = 1
keepProb[2] = 1
print(keepProb)
batch_size_tr = 64 #train batch size
batch_size_te = 64 # test batch size
print("Training_shape", X_training.shape)
# print("Test_shape", X_test.shape)
rmse_tr, rmse_te, Yhat_tr, Yhat_te, Y_training, Y_test, LossTr, LossTe = main_model(X_training,Y_training,X_test, Y_test, layer, Max_it,
learning_rate,
batch_size_tr, batch_size_te,
keepProb,
gamma1, gamma2, Dropout=False)
print('***********', rmse_tr, rmse_te)
fig, ax = plt.plt.subplots(figsize=(10, 8))
I1 = np.random.randint(Yhat_tr.shape[1], size=100)
pred = Yhat_tr[0, I1]
GT = Y_training[0, I1]
# fig, ax = plt.subplots(figsize=(10,8))
# plot a black line between the
# ith prediction and the ith ground truth
for i in range(len(pred)):
ax.plot([i, i], [pred[i], GT[i]], c="k", linewidth=0.8)
ax.plot(pred, 'o', label='Prediction', markersize=8, color='g')
ax.plot(GT, '^', label='Ground Truth', markersize=8, color='r')
ax.set_xlim((-1, 101))
plt.xlabel('Experimental hybrids')
plt.ylabel('Yield')
plt.title('Performance on Training Set')
plt.legend()
plt.show()
fig, ax = plt.subplots(figsize=(10, 8))
# plt.subplot(132)
I2 = np.random.randint(Yhat_te.shape[1], size=100)
pred = Yhat_te[0, I2]
GT = Y_test[0, I2]
# fig, ax = plt.subplots(figsize=(10,8))
# plot a black line between the
# ith prediction and the ith ground truth
for i in range(len(pred)):
ax.plot([i, i], [pred[i], GT[i]], c="k", linewidth=0.8)
ax.plot(pred, 'o', label='Prediction', markersize=8, color='g')
ax.plot(GT, '^', label='Ground Truth', markersize=8, color='r')
ax.set_xlim((-1, 101))
plt.xlabel('Experimental hybrids')
plt.ylabel('Yield')
plt.title('Performance on Validation Set')
plt.legend()
plt.show()
plt.figure(1)
# plt.subplot(131)
I1 = np.random.randint(Yhat_tr.shape[1], size=100)
a1, = plt.plot(Yhat_tr[0, I1], 'r--', label="Prediction")
a2, = plt.plot(Y_training[0, I1], 'b--', label="Target")
plt.legend(handles=[a1, a2])
plt.title('Performance on Training Set')
plt.xlabel("Index")
plt.ylabel("Yield")
plt.show()
plt.figure(2)
# plt.subplot(132)
I2 = np.random.randint(Yhat_te.shape[1], size=100)
a1, = plt.plot(Yhat_te[0, I2], 'r--', label="Prediction")
a2, = plt.plot(Y_test[0, I2], 'b--', label="Target")
plt.legend(handles=[a1, a2])
plt.xlabel("Index")
plt.ylabel("Yield")
plt.title('Performance on Validation set')
plt.show()
plt.figure(3)
# plt.subplot(133)
a1, = plt.plot(LossTe, 'r--', label="Test_Lost")
a2, = plt.plot(LossTr, 'b--', label="Train_Lost")
plt.legend(handles=[a1, a2])
plt.xlabel("Index")
plt.ylabel("Loss")
plt.title('Loss_function')
plt.show()