-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
140 lines (103 loc) · 3.86 KB
/
main.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
import argparse
import imp
import os
import shutil
import sys
import numpy as np
import theano
from lasagne.updates import adam, sgd, momentum
from lasagne.layers import get_all_params, get_output
from raccoon.trainer import Trainer
from raccoon.extensions import (TrainMonitor, ValMonitor, VariableSaver,
MaxTime, MaxIteration)
from data import create_data_generators
from utilities import save_config
floatX = theano.config.floatX = 'float32'
# theano.config.optimizer = 'None'
# theano.config.compute_test_value = 'raise'
# np.random.seed(42)
def main(cf):
########
# DATA #
########
print 'Creating data generators...'
train_iterator, valid_iterator, test_iterator = create_data_generators(cf)
##############################
# COST, GRADIENT AND UPDATES #
##############################
print 'Building model...'
cost, accuracy = cf.model.compute_cost(deterministic=False)
cost_val, accuracy_val = cf.model.compute_cost(deterministic=True)
params = get_all_params(cf.model.net, trainable=True)
if cf.algo == 'adam':
updates = adam(cost, params, cf.learning_rate)
elif cf.algo == 'sgd':
updates = sgd(cost, params, cf.learning_rate)
elif cf.algo == 'momentum':
updates = momentum(cost, params, cf.learning_rate)
else:
raise ValueError('Specified algo does not exist')
##############
# MONITORING #
##############
print 'Creating extensions and compiling functions...',
train_monitor = TrainMonitor(
cf.train_freq_print, cf.model.vars, [cost, accuracy], updates)
monitoring_vars = [cost_val, accuracy_val]
valid_monitor = ValMonitor(
'Validation', cf.valid_freq_print, cf.model.vars, monitoring_vars,
valid_iterator)
test_monitor = ValMonitor(
'Test', cf.valid_freq_print, cf.model.vars, monitoring_vars,
valid_iterator)
train_saver = VariableSaver(
train_monitor, cf.dump_every_batches, cf.dump_path, 'train')
valid_saver = VariableSaver(
valid_monitor, cf.dump_every_batches, cf.dump_path, 'valid')
test_saver = VariableSaver(test_monitor, None, cf.dump_path, 'test')
# Ending conditions
end_conditions = []
if hasattr(cf, 'max_iter'):
end_conditions.append(MaxIteration(cf.max_iter))
if hasattr(cf, 'max_time'):
end_conditions.append(MaxTime(cf.max_iter))
extensions = [
valid_monitor,
test_monitor,
train_saver,
valid_saver,
test_saver
]
train_m = Trainer(train_monitor, train_iterator,
extensions, end_conditions)
############
# TRAINING #
############
train_m.train()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-e', '--exp_name', default='bazar')
parser.add_argument('-s', '--config_path')
parser.add_argument('-i', '--job_id', default=None)
options = parser.parse_args()
if not options.job_id:
job_id = np.random.randint(10**6)
folder = options.config_path + '_' + str(job_id)
else:
job_id = options.job_id
folder = job_id
print 'EXP name: {}'.format(options.exp_name)
print 'config file: {}'.format(options.config_path)
print 'job ID: {}'.format(job_id)
config = imp.load_source('config', options.config_path)
config_name = os.path.splitext(options.config_path)[0]
dump_path = os.path.join(os.getenv('TMP_PATH'), 'QA',
options.exp_name, folder)
if not os.path.exists(dump_path):
os.makedirs(dump_path)
config.dump_path = dump_path
# Copy config file in the dump experiment path
shutil.copy(options.config_path, os.path.join(dump_path, 'cf.py'))
# Save config parameters (some of them might be generated at test time)
print save_config(config, dump_path)
main(config)