-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlp.py
216 lines (173 loc) · 7.28 KB
/
mlp.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
#coding=utf-8
from __future__ import print_function
import sys
import os
import time
import matplotlib.pyplot as plt
import numpy as np
import theano
import theano.tensor as T
import theano.typed_list
import math
import lasagne
from mlp_util import load_data
def build_mlp(input_var=None):
l_in = lasagne.layers.InputLayer(shape=(None, 6),
input_var=input_var)
l_in_drop = lasagne.layers.DropoutLayer(l_in, p=0.001)
l_hid1 = lasagne.layers.DenseLayer(l_in_drop, num_units=10,
nonlinearity=lasagne.nonlinearities.rectify,
W=lasagne.init.HeNormal())
l_hid1_drop = lasagne.layers.DropoutLayer(l_hid1, p=0.001)
l_hid2 = lasagne.layers.DenseLayer(l_hid1_drop, num_units=10,
nonlinearity=lasagne.nonlinearities.rectify,
W=lasagne.init.HeNormal())
l_hid2_drop = lasagne.layers.DropoutLayer(l_hid2, p=0.001)
l_hid3=lasagne.layers.DenseLayer(l_hid2_drop, num_units=10,
nonlinearity=lasagne.nonlinearities.rectify,
W=lasagne.init.HeNormal())
l_hid3_drop = lasagne.layers.DropoutLayer(l_hid3, p=0.001)
l_hid4=lasagne.layers.DenseLayer(l_hid3_drop, num_units=50,
nonlinearity=lasagne.nonlinearities.rectify,
W=lasagne.init.HeNormal())
l_hid4_drop = lasagne.layers.DropoutLayer(l_hid4, p=0.001)
l_out = lasagne.layers.DenseLayer(
l_hid4_drop, num_units=1,
nonlinearity=lasagne.nonlinearities.rectify,
W=lasagne.init.HeNormal())
# lasagne.nonlinearities.linear(x)
return l_out
def iterate_minibatches(inputs, targets, batchsize, shuffle=True):
assert len(inputs) == len(targets)
if shuffle:
indices = np.arange(len(inputs))
np.random.shuffle(indices)
for start_idx in range(0, len(inputs) - batchsize + 1, batchsize):
if shuffle:
excerpt = indices[start_idx:start_idx + batchsize]
else:
excerpt = slice(start_idx, start_idx + batchsize)
yield inputs[excerpt], targets[excerpt]
def main(num_epochs=200):
# Load the dataset
print("Loading data...")
X_train, y_train, X_val, y_val, X_test, y_test = load_data()
# Prepare Theano variables for inputs and targets
input_var = T.matrix('inputs')
target_var = T.matrix('targets')
print("Building model and compiling functions...")
network = build_mlp(input_var)
prediction = lasagne.layers.get_output(network)
#loss = lasagne.objectives.squared_error(prediction, target_var)
#loss = loss.mean()
loss= (abs(prediction-target_var)).mean()+ (lasagne.objectives.squared_error(prediction, target_var)).mean()
params = lasagne.layers.get_all_params(network, trainable=True)
updates = lasagne.updates.nesterov_momentum(loss, params, learning_rate=0.03, momentum=0.9)
#updates = lasagne.updates.rmsprop(loss, params, learning_rate=0.003, rho=0.05,epsilon=1e-6)
#updates = lasagne.updates.adagrad(loss, params, learning_rate=0.05, epsilon=1e-6)
#updates = lasagne.updates.adadelta(loss, params, learning_rate=0.05, rho=0.1,epsilon=1e-6)
#updates = lasagne.updates.adam(loss, params, learning_rate=0.05, beta1=0.5, beta2=0.5, epsilon=1e-6)
test_prediction = lasagne.layers.get_output(network, deterministic=True)
#test_loss = lasagne.objectives.squared_error(test_prediction,target_var)
#nominator = abs(test_prediction-target_var)
#denominator = abs(target_var)
test_loss=(abs(test_prediction-target_var)).mean()
test_acc = T.mean(abs(test_prediction-target_var) > 0.1*abs(target_var), dtype = theano.config.floatX)
#outs= theano.typed_list.append(test_prediction, target_var)
train_fn = theano.function([input_var, target_var], loss, updates=updates)
# Compile a second function computing the validation loss and accuracy:
val_fn = theano.function([input_var, target_var], [test_loss, test_acc, test_prediction, target_var ])
# Finally, launch the training loop.
print("Starting training...")
# We iterate over epochs:
train_re=[]
valid_re=[]
valid_ac=[]
for epoch in range(num_epochs):
# In each epoch, we do a full pass over the training data:
train_err = 0
train_batches = 0
start_time = time.time()
for batch in iterate_minibatches(X_train, y_train, 500, shuffle=True):
inputs, targets = batch
train_err += train_fn(inputs, targets)
train_batches += 1
# And a full pass over the validation data:
val_err = 0
val_batches = 0
val_acc = 0
predicted = []
real = []
for batch in iterate_minibatches(X_val, y_val, 500, shuffle=True):
inputs, targets = batch
err, acc, out, tar = val_fn(inputs, targets)
predicted.append(out)
real.append(tar)
val_err += err
val_acc += acc
val_batches += 1
# print(out)
# Then we print the results for this epoch:
print("Epoch {} of {} took {:.3f}s".format(
epoch + 1, num_epochs, time.time() - start_time))
print(" training loss:\t\t{:.6f}".format(train_err / train_batches))
print(" validation loss:\t\t{:.6f}".format(val_err / val_batches))
print(" validation error rate:\t\t{:.6f}".format(val_acc / val_batches))
train_re.append(train_err / train_batches)
valid_re.append(val_err / val_batches)
valid_ac.append(val_acc / val_batches)
#After training, we compute and print the test error:
test_err = 0
test_batches = 0
print(predicted[0][0])
print(real[0][0])
e = 0.0
absError = 0.0
origLines = []
nnLines = []
with open('cmp.txt','w') as f:
for i in xrange(len(predicted)):
for j in xrange(len(predicted[0])):
f.write("%f\t%f\n"%(predicted[i][j],real[i][j]))
origLines.append(float(real[i][j]))
nnLines.append(float(predicted[i][j]))
cc = 0
for i in range(len(origLines)):
origPrice = float(origLines[i])
nnPrice = float(nnLines[i])
nominator = abs(origPrice - nnPrice)
denominator = abs(origPrice)
if(denominator == 0):
e = 1.0
elif(math.isnan(nominator) or (math.isnan(denominator))):
e = 1.0
elif ((nominator / denominator > 1)):
e = 1.0
else:
e = nominator / denominator
cc += 1
absError += e
print("*** Error: %f" % (absError/float(len(origLines))))
print(len(origLines))
print(cc)
for batch in iterate_minibatches(X_test, y_test, 500, shuffle=True):
inputs, targets = batch
err= val_fn(inputs, targets)
test_err += err
test_batches += 1
print("Final results:")
print(" test loss:\t\t\t{:.6f}".format(test_err / test_batches))
#plot the loss with epoch
'''
plt.figure(1)
line_up,=plt.plot(range(1,len(train_re)+1),train_re,'--*r',label='train loss')
line_down,=plt.plot(range(1,len(valid_re)+1),valid_re, '--*g',label='valid loss')
plt.xlabel("epoch")
plt.ylabel("loss of train and valid")
plt.ylim(0.03, 0.12)
plt.title('loss with epoch')
plt.legend(handles=[line_up,line_down])
plt.show()
'''
if __name__ == '__main__':
main(num_epochs=200)