/
experiment.py
92 lines (73 loc) · 2.97 KB
/
experiment.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
# Local imports
import pylearn2.config.yaml_parse
import jobman
from jobman.tools import expand, flatten
class ydict(dict):
'''
YAML-friendly subclass of dictionary.
The special key "__builder__" is interpreted as the name of an object
constructor.
For instance, building a ydict from the following dictionary:
{
'__builder__': 'pylearn2.training_algorithms.sgd.EpochCounter',
'max_epochs': 2
}
Will be displayed like:
!obj:pylearn2.training_algorithms.sgd.EpochCounter {'max_epochs': 2}
'''
def __str__(self):
args_dict = dict(self)
builder = args_dict.pop('__builder__', '')
ret_list = []
if builder:
ret_list.append('!obj:%s {' % builder)
else:
ret_list.append('{')
for key, val in args_dict.iteritems():
# This will call str() on keys and values, not repr(), so unicode
# objects will have the form 'blah', not "u'blah'".
ret_list.append('%s: %s,' % (key, val))
ret_list.append('}')
return '\n'.join(ret_list)
def train_experiment(state, channel):
"""
Train a model specified in state, and extract required results.
This function builds a YAML string from ``state.yaml_template``, taking
the values of hyper-parameters from ``state.hyper_parameters``, creates
the corresponding object and trains it (like train.py), then run the
function in ``state.extract_results`` on it, and store the returned values
into ``state.results``.
To know how to use this function, you can check the example in tester.py
(in the same directory).
"""
yaml_template = state.yaml_template
# Convert nested DD into nested ydict.
hyper_parameters = expand(flatten(state.hyper_parameters), dict_type=ydict)
# This will be the complete yaml string that should be executed
final_yaml_str = yaml_template % hyper_parameters
# Instantiate an object from YAML string
train_obj = pylearn2.config.yaml_parse.load(final_yaml_str)
for ext in train_obj.extensions:
if hasattr(ext, 'set_train_obj'):
ext.set_train_obj( train_obj )
if hasattr(ext, 'set_jobman_channel'):
ext.set_jobman_channel( channel )
if hasattr(ext, 'set_jobman_state'):
ext.set_jobman_state( state )
try:
iter(train_obj)
iterable = True
except TypeError:
iterable = False
if iterable:
raise NotImplementedError(
('Current implementation does not support running multiple '
'models in one yaml string. Please change the yaml template '
'and parameters to contain only one single model.'))
else:
# print "Executing the model."
train_obj.main_loop()
# This line will call a function defined by the user and pass train_obj
# to it.
state.results = jobman.tools.resolve(state.extract_results)(train_obj)
return channel.COMPLETE