-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmainLoop.py
executable file
·162 lines (151 loc) · 6.2 KB
/
mainLoop.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
class Unbuffered:
def __init__(self, stream):
self.stream = stream
def write(self,data):
self.stream.write(data)
self.stream.flush()
def __getattr__(self, attr):
return getattr(self.stream, attr)
import sys
sys.stdout=Unbuffered(sys.stdout)
# Generic imports
import numpy
import cPickle
import gzip
import time
from utils import print_mem, print_time
class MainLoop(object):
def __init__(self, data, model, algo, state, channel):
###################
# Step 0. Set parameters
###################
self.data = data
self.state = state
self.channel = channel
self.model = model
self.algo = algo
self.state['validcost'] = 1e20
self.state['bvalidcost'] = 1e20
self.state['testcost'] = 1e20
self.state['traincost'] = 1e20
self.state['validtime'] = 0
self.state['btraincost'] = 1e20
self.state['bvalidcost'] = 1e20
n_elems = state['loopIters'] // state['trainFreq']
self.timings = {}
for name in self.algo.return_names:
self.timings[name] = numpy.zeros((n_elems,), dtype='float32')
n_elems = state['loopIters'] // state['validFreq'] + 1
self.timings['valid'] = numpy.zeros((n_elems,), dtype='float32')
self.timings['test'] = numpy.zeros((n_elems,), dtype='float32')
if self.channel is not None:
self.channel.save()
self.start_time = time.time()
self.batch_start_time = time.time()
def validate(self):
cost = self.model.validate()
print ('** validation cost %6.3f computed in %s'
', best cost is %6.3f, test %6.3f, whole time %6.3f min') % (
cost,
print_time(time.time() - self.batch_start_time),
self.state['bvalidcost'],
self.state['testcost'],
(time.time() - self.start_time)/60. )
self.batch_start_time = time.time()
pos = self.step // self.state['validFreq']
self.timings['valid'][pos] = float(cost)
self.timings['test'][pos] = float(self.state['testcost'])
self.state['validcost'] = float(cost)
self.state['validtime'] = float(time.time() - self.start_time)/60.
if self.state['bvalidcost'] > cost:
self.state['bvalidcost'] = float(cost)
self.state['btraincost'] = float(self.state['traincost'])
self.test()
print_mem('validate')
def test(self):
self.model.best_params = [(x.name, x.get_value()) for x in
self.model.params]
cost = self.model.test_eval()
print '>>> Test cost', cost
pos = self.step // self.state['validFreq']
self.timings['test'][pos] = float(cost)
self.state['testcost'] = float(cost)
def save(self):
numpy.savez(self.state['prefix']+'timing.npz',
**self.timings)
if self.state['overwrite']:
self.model.save(self.state['prefix']+'model.npz')
else:
self.model.save(self.state['prefix']+'model%d.npz' % self.save_iter)
cPickle.dump(self.state, open(self.state['prefix']+'state.pkl', 'w'))
self.save_iter += 1
def main(self):
print_mem('start')
self.state['gotNaN'] = 0
self.start_time = time.time()
self.batch_start_time = time.time()
self.step = 0
self.save_iter = 0
self.save()
if self.channel is not None:
self.channel.save()
self.save_time = time.time()
last_cost = 1.
start_time = time.time()
self.start_time = start_time
while self.step < self.state['loopIters'] and \
last_cost > .1*self.state['minerr'] and \
(time.time() - start_time)/60. < self.state['timeStop']:
if (time.time() - self.save_time)/60. > self.state['saveFreq']:
self.save()
if self.channel is not None:
self.channel.save()
self.save_time = time.time()
st = time.time()
try:
rvals = self.algo()
self.state['traincost'] = float(rvals['cost'])
self.state['step'] = self.step
last_cost = rvals['cost']
for name in rvals.keys():
pos = self.step // self.state['trainFreq']
self.timings[name][pos] = rvals[name]
if numpy.isinf(rvals['cost']) or numpy.isnan(rvals['cost']):
self.state['gotNaN'] = 1
self.save()
if self.channel:
self.channel.save()
print 'Got NaN while training'
last_cost = 0
if self.step % self.state['validFreq'] == 0:
self.validate()
self.step += 1
except:
self.state['wholetime'] = float(time.time() - start_time)
self.save()
if self.channel:
self.channel.save()
last_cost = 0
print 'Error in running natgrad (lr issue)'
print 'BEST SCORE'
print 'Validation', self.state['validcost']
print 'Validation time', print_time(self.state['validtime'])
print 'Train cost', self.state['traincost']
print 'Best Train', self.state['btraincost']
print 'Best Valid', self.state['bvalidcost']
print 'TEST', self.state['testcost']
print 'Took', (time.time() - start_time)/60.,'min'
raise
self.state['wholetime'] = float(time.time() - start_time)
self.validate()
self.save()
if self.channel:
self.channel.save()
print 'BEST SCORE'
print 'Validation', self.state['validcost']
print 'Validation time', print_time(self.state['validtime'])
print 'Train cost', self.state['traincost']
print 'Best Train', self.state['btraincost']
print 'Best Valid', self.state['bvalidcost']
print 'TEST', self.state['testcost']
print 'Took', (time.time() - start_time)/60.,'min'