/
launchers_hierarchicalnetwork.py
344 lines (234 loc) · 15 KB
/
launchers_hierarchicalnetwork.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
#!/usr/bin/env python
# encoding: utf-8
"""
launchers_hierarchicalnetwork.py
Created by Loic Matthey on 2013-05-18
Copyright (c) 2013 . All rights reserved.
"""
import matplotlib.pyplot as plt
import numpy as np
from randomfactorialnetwork import *
from datagenerator import *
from statisticsmeasurer import *
from slicesampler import *
from utils import *
from dataio import *
# from gibbs_sampler_continuous_fullcollapsed_randomfactorialnetwork import *
import launchers
def launcher_do_hierarchical_precision_M_Mlower(args):
'''
Compare the evolution of the precision curve as the number of neurons in a hierarchical network increases.
'''
all_parameters = vars(args)
M_space = np.linspace(5, 505, 26)
M_lower_space = np.arange(5, 31, 2)**2.
M_space = np.array([10, 25, 100])
M_lower_space = np.array([49, 100])
T_space = np.arange(1, all_parameters['T']+1)
results_precision_M_T = np.nan*np.empty((M_space.size, M_lower_space.size, T_space.size, num_repetitions), dtype=float)
# Show the progress
search_progress = progress.Progress(M_space.size*M_lower_space.size)
print M_space
print M_lower_space
for m_i, M in enumerate(M_space):
for m_l_i, M_layer_one in enumerate(M_lower_space):
# Current parameter values
all_parameters['M'] = M
all_parameters['M_layer_one'] = M_layer_one
### WORK UNIT
output = launcher_do_hierarchical_precision_M_Mlower_pbs(all_parameters)
results_precision_M_T[m_i, m_l_i] = output['results_precision_M_T'][0, 0]
### DONE WORK UNIT
search_progress.increment()
if run_counter % save_every == 0 or search_progress.done():
dataio.save_variables(variables_to_save, locals())
run_counter += 1
return locals()
def launcher_do_hierarchical_precision_M_sparsity_sigmaweight_feature(args):
'''
Compare the evolution of the precision curve as the sparsity, sigma and M change, for a hierarchical code with feature base
'''
all_parameters = vars(args)
code_type = 'hierarchical'
dataio = DataIO(output_folder=args.output_directory, label=args.label)
# variables_to_save = ['M_space', 'T_space', 'repet_i', 'num_repetitions', 'results_precision_N', 'result_responses', 'result_targets', 'result_nontargets']
variables_to_save = ['M_space', 'T_space', 'sparsity_space', 'sigma_weights_space', 'repet_i', 'num_repetitions', 'results_precision_N']
save_every = 5
run_counter = 0
num_repetitions = all_parameters['num_repetitions']
# M_space = np.array([all_parameters['M']])
# M_space = np.array([4*4, 5*5, 7*7, 8*8, 9*9, 10*10, 15*15, 20*20])
M_space = np.linspace(5, 500, 10)
sparsity_space = np.linspace(0.01, 1.0, 10.)
sigma_weights_space = np.linspace(0.1, 2.0, 10)
T_space = np.arange(1, all_parameters['T']+1)
results_precision_M_T = np.nan*np.empty((M_space.size, sparsity_space.size, sigma_weights_space.size, T_space.size, num_repetitions), dtype=float)
# result_responses = np.nan*np.empty((M_space.size, sparsity_space.size, sigma_weights_space.size, T_space.size, num_repetitions, all_parameters['N']))
# result_targets = np.nan*np.empty((M_space.size, sparsity_space.size, sigma_weights_space.size, T_space.size, num_repetitions, all_parameters['N']))
# result_nontargets = np.nan*np.empty((M_space.size, sparsity_space.size, sigma_weights_space.size, T_space.size, num_repetitions, all_parameters['N'], all_parameters['T']-1))
all_parameters['type_layer_one'] = 'feature'
# Show the progress
search_progress = progress.Progress(T_space.size*M_space.size*sigma_weights_space.size*sparsity_space.size*num_repetitions)
print M_space
print sparsity_space
print sigma_weights_space
print T_space
for repet_i in xrange(num_repetitions):
for m_i, M in enumerate(M_space):
for s_i, sparsity in enumerate(sparsity_space):
for sw_i, sigma_weights in enumerate(sigma_weights_space):
for t_i, t in enumerate(T_space):
# Will estimate the precision
print "Precision as function of N, hierarchical network, T: %d/%d, M %d, sparsity %.3f, weights: %.2f, (%d/%d). %.2f%%, %s left - %s" % (t, T_space[-1], M, sparsity, sigma_weights, repet_i+1, num_repetitions, search_progress.percentage(), search_progress.time_remaining_str(), search_progress.eta_str())
# Current parameter values
all_parameters['M'] = M
all_parameters['T'] = t
all_parameters['code_type'] = code_type
all_parameters['sparsity'] = sparsity
all_parameters['sigma_weights'] = sigma_weights
### WORK UNIT
(random_network, data_gen, stat_meas, sampler) = launchers.init_everything(all_parameters)
if all_parameters['inference_method'] == 'sample':
# Sample thetas
sampler.sample_theta(num_samples=all_parameters['num_samples'], burn_samples=100, selection_method=all_parameters['selection_method'], selection_num_samples=all_parameters['selection_num_samples'], integrate_tc_out=False, debug=False)
elif all_parameters['inference_method'] == 'max_lik':
# Just use the ML value for the theta
sampler.set_theta_max_likelihood(num_points=150, post_optimise=True)
results_precision_M_T[m_i, s_i, sw_i, t_i, repet_i] = sampler.get_precision()
print results_precision_M_T[m_i, s_i, sw_i, t_i, repet_i]
# (result_responses[m_i, s_i, t_i, repet_i], result_targets[m_i, s_i, t_i, repet_i], result_nontargets[m_i, s_i, t_i, repet_i, :, :t_i]) = sampler.collect_responses()
### DONE WORK UNIT
search_progress.increment()
if run_counter % save_every == 0 or search_progress.done():
dataio.save_variables(variables_to_save, locals())
run_counter += 1
return locals()
###### PBS runners #####
def launcher_do_hierarchical_precision_M_Mlower_pbs(args):
'''
Compare the evolution of the precision curve as the number of neurons in a hierarchical network increases.
'''
print "Doing a piece of work for launcher_do_hierarchical_precision_M_Mlower_pbs"
save_all_output = True
try:
# Convert Argparse.Namespace to dict
all_parameters = vars(args)
except TypeError:
# Assume it's already done
assert type(args) is dict, "args is neither Namespace nor dict, WHY?"
all_parameters = args
code_type = 'hierarchical'
dataio = DataIO(output_folder=all_parameters['output_directory'], label=all_parameters['label'])
variables_to_save = ['repet_i', 'num_repetitions']
save_every = 5
run_counter = 0
num_repetitions = all_parameters['num_repetitions']
M_space = np.array([all_parameters['M']])
M_lower_space = np.array([all_parameters['M_layer_one']])
T_space = np.arange(1, all_parameters['T']+1)
results_precision_M_T = np.nan*np.empty((M_space.size, M_lower_space.size, T_space.size, num_repetitions), dtype=float)
results_emfits_M_T = np.nan*np.empty((M_space.size, M_lower_space.size, T_space.size, 5, num_repetitions), dtype=float)
if save_all_output:
result_responses = np.nan*np.empty((M_space.size, M_lower_space.size, T_space.size, all_parameters['N'], num_repetitions))
result_targets = np.nan*np.empty((M_space.size, M_lower_space.size, T_space.size, all_parameters['N'], num_repetitions))
result_nontargets = np.nan*np.empty((M_space.size, M_lower_space.size, T_space.size, all_parameters['N'], all_parameters['T']-1, num_repetitions))
# Show the progress
search_progress = progress.Progress(T_space.size*M_space.size*M_lower_space.size*num_repetitions)
print M_space
print M_lower_space
print T_space
for repet_i in xrange(num_repetitions):
for m_i, M in enumerate(M_space):
for m_l_i, M_layer_one in enumerate(M_lower_space):
for t_i, t in enumerate(T_space):
# Will estimate the precision
print "Precision as function of N, hierarchical network, T: %d/%d, M %d, M_layer_one %d, (%d/%d). %.2f%%, %s left - %s" % (t, T_space[-1], M, M_layer_one, repet_i+1, num_repetitions, search_progress.percentage(), search_progress.time_remaining_str(), search_progress.eta_str())
# Current parameter values
all_parameters['M'] = M
all_parameters['T'] = t
all_parameters['code_type'] = code_type
all_parameters['M_layer_one'] = M_layer_one
### WORK UNIT
(random_network, data_gen, stat_meas, sampler) = launchers.init_everything(all_parameters)
# Sample / max like
sampler.run_inference(all_parameters)
print 'get precision...'
results_precision_M_T[m_i, m_l_i, t_i, repet_i] = sampler.get_precision()
print results_precision_M_T[m_i, m_l_i, t_i, repet_i]
print "fit mixture model..."
curr_params_fit = sampler.fit_mixture_model(use_all_targets=True)
curr_params_fit['mixt_nontargets_sum'] = np.sum(curr_params_fit['mixt_nontargets'])
results_emfits_M_T[m_i, m_l_i, t_i, :, repet_i] = [curr_params_fit[key] for key in ('kappa', 'mixt_target', 'mixt_nontargets_sum', 'mixt_random', 'train_LL')]
if save_all_output:
(result_responses[m_i, m_l_i, t_i, :, repet_i], result_targets[m_i, m_l_i, t_i, :, repet_i], result_nontargets[m_i, m_l_i, t_i, :, :t_i, repet_i]) = sampler.collect_responses()
### DONE WORK UNIT
search_progress.increment()
if run_counter % save_every == 0 or search_progress.done():
dataio.save_variables_default(locals(), variables_to_save)
run_counter += 1
print "All finished"
return locals()
def launcher_do_hierarchical_precision_M_sparsity_sigmaweight_feature_pbs(args):
'''
Compare the evolution of the precision curve as the sparsity, sigma and M change, for a hierarchical code with feature base
'''
print "Doing a piece of work for launcher_do_hierarchical_precision_M_sparsity_sigmaweight_feature_pbs"
save_all_output = False
all_parameters = vars(args)
code_type = 'hierarchical'
dataio = DataIO(output_folder=args.output_directory, label=args.label)
variables_to_save = ['M_space', 'T_space', 'sparsity_space', 'sigma_weights_space', 'repet_i', 'num_repetitions', 'results_precision_M_T']
save_every = 5
run_counter = 0
num_repetitions = all_parameters['num_repetitions']
# M_space = np.array([all_parameters['M']])
# M_space = np.array([4*4, 5*5, 7*7, 8*8, 9*9, 10*10, 15*15, 20*20])
M_space = np.array([all_parameters['M']])
sparsity_space = np.array([all_parameters['sparsity']])
sigma_weights_space = np.array([all_parameters['sigma_weights']])
T_space = np.arange(1, all_parameters['T']+1)
results_precision_M_T = np.nan*np.empty((M_space.size, sparsity_space.size, sigma_weights_space.size, T_space.size, num_repetitions), dtype=float)
if save_all_output:
variables_to_save.extend(['result_responses', 'result_targets', 'result_nontargets'])
result_responses = np.nan*np.empty((M_space.size, sparsity_space.size, sigma_weights_space.size, T_space.size, num_repetitions, all_parameters['N']))
result_targets = np.nan*np.empty((M_space.size, sparsity_space.size, sigma_weights_space.size, T_space.size, num_repetitions, all_parameters['N']))
result_nontargets = np.nan*np.empty((M_space.size, sparsity_space.size, sigma_weights_space.size, T_space.size, num_repetitions, all_parameters['N'], all_parameters['T']-1))
all_parameters['type_layer_one'] = 'feature'
# Show the progress
search_progress = progress.Progress(T_space.size*M_space.size*sigma_weights_space.size*sparsity_space.size*num_repetitions)
print M_space
print sparsity_space
print sigma_weights_space
print T_space
for repet_i in xrange(num_repetitions):
for m_i, M in enumerate(M_space):
for s_i, sparsity in enumerate(sparsity_space):
for sw_i, sigma_weights in enumerate(sigma_weights_space):
for t_i, t in enumerate(T_space):
# Will estimate the precision
print "Precision as function of N, hierarchical network, T: %d/%d, M %d, sparsity %.3f, weights: %.2f, (%d/%d). %.2f%%, %s left - %s" % (t, T_space[-1], M, sparsity, sigma_weights, repet_i+1, num_repetitions, search_progress.percentage(), search_progress.time_remaining_str(), search_progress.eta_str())
# Current parameter values
all_parameters['M'] = M
all_parameters['T'] = t
all_parameters['code_type'] = code_type
all_parameters['sparsity'] = sparsity
all_parameters['sigma_weights'] = sigma_weights
### WORK UNIT
(random_network, data_gen, stat_meas, sampler) = launchers.init_everything(all_parameters)
if all_parameters['inference_method'] == 'sample':
# Sample thetas
sampler.sample_theta(num_samples=all_parameters['num_samples'], burn_samples=100, selection_method=all_parameters['selection_method'], selection_num_samples=all_parameters['selection_num_samples'], integrate_tc_out=False, debug=False)
elif all_parameters['inference_method'] == 'max_lik':
# Just use the ML value for the theta
sampler.set_theta_max_likelihood(num_points=150, post_optimise=True)
results_precision_M_T[m_i, s_i, sw_i, t_i, repet_i] = sampler.get_precision()
print results_precision_M_T[m_i, s_i, sw_i, t_i, repet_i]
if save_all_output:
(result_responses[m_i, m_l_i, t_i, repet_i], result_targets[m_i, m_l_i, t_i, repet_i], result_nontargets[m_i, m_l_i, t_i, repet_i, :, :t_i]) = sampler.collect_responses()
### DONE WORK UNIT
search_progress.increment()
if run_counter % save_every == 0 or search_progress.done():
dataio.save_variables(variables_to_save, locals())
run_counter += 1
print "All finished"
return locals()