annotate deep/crbm/utils.py @ 363:14b28e43ce4e

Correction d'un bug dans le pre-train du SDA cause par tanh
author SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
date Thu, 22 Apr 2010 13:43:04 -0400
parents f37c0705649d
children 64fa85d68923
rev   line source
360
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
1 #!/usr/bin/python
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
2 # coding: utf-8
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
3
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
4 from __future__ import with_statement
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
5
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
6 import jobman
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
7 from jobman import DD
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
8
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
9 from pylearn.io.seriestables import *
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
10 import tables
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
11
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
12
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
13
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
14 # from pylearn codebase
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
15 # useful in __init__(param1, param2, etc.) to save
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
16 # values in self.param1, self.param2... just call
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
17 # update_locals(self, locals())
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
18 def update_locals(obj, dct):
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
19 if 'self' in dct:
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
20 del dct['self']
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
21 obj.__dict__.update(dct)
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
22
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
23 # from a dictionary of possible values for hyperparameters, e.g.
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
24 # hp_values = {'learning_rate':[0.1, 0.01], 'num_layers': [1,2]}
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
25 # create a list of other dictionaries representing all the possible
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
26 # combinations, thus in this example creating:
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
27 # [{'learning_rate': 0.1, 'num_layers': 1}, ...]
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
28 # (similarly for combinations (0.1, 2), (0.01, 1), (0.01, 2))
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
29 def produit_cartesien_jobs(val_dict):
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
30 job_list = [DD()]
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
31 all_keys = val_dict.keys()
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
32
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
33 for key in all_keys:
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
34 possible_values = val_dict[key]
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
35 new_job_list = []
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
36 for val in possible_values:
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
37 for job in job_list:
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
38 to_insert = job.copy()
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
39 to_insert.update({key: val})
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
40 new_job_list.append(to_insert)
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
41 job_list = new_job_list
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
42
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
43 return job_list
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
44
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
45 def jobs_from_reinsert_list(cols, job_vals):
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
46 job_list = []
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
47 for vals in job_vals:
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
48 job = DD()
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
49 for i, col in enumerate(cols):
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
50 job[col] = vals[i]
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
51 job_list.append(job)
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
52
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
53 return job_list
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
54
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
55 def save_params(all_params, filename):
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
56 import pickle
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
57 with open(filename, 'wb') as f:
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
58 values = [p.value for p in all_params]
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
59
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
60 # -1 for HIGHEST_PROTOCOL
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
61 pickle.dump(values, f, -1)
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
62
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
63 # Perform insertion into the Postgre DB based on combination
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
64 # of hyperparameter values above
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
65 # (see comment for produit_cartesien_jobs() to know how it works)
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
66 def jobman_insert_job_vals(job_db, experiment_path, job_vals):
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
67 jobs = produit_cartesien_jobs(job_vals)
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
68
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
69 db = jobman.sql.db(job_db)
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
70 for job in jobs:
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
71 job.update({jobman.sql.EXPERIMENT: experiment_path})
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
72 jobman.sql.insert_dict(job, db)
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
73
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
74 def jobman_insert_specific_jobs(job_db, experiment_path,
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
75 insert_cols, insert_vals):
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
76 jobs = jobs_from_reinsert_list(insert_cols, insert_vals)
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
77
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
78 db = jobman.sql.db(job_db)
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
79 for job in jobs:
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
80 job.update({jobman.sql.EXPERIMENT: experiment_path})
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
81 jobman.sql.insert_dict(job, db)
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
82
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
83 # Just a shortcut for a common case where we need a few
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
84 # related Error (float) series
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
85 def get_accumulator_series_array( \
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
86 hdf5_file, group_name, series_names,
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
87 reduce_every,
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
88 index_names=('epoch','minibatch'),
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
89 stdout_too=True,
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
90 skip_hdf5_append=False):
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
91 all_series = []
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
92
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
93 new_group = hdf5_file.createGroup('/', group_name)
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
94
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
95 other_targets = []
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
96 if stdout_too:
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
97 other_targets = [StdoutAppendTarget()]
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
98
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
99 for sn in series_names:
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
100 series_base = \
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
101 ErrorSeries(error_name=sn,
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
102 table_name=sn,
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
103 hdf5_file=hdf5_file,
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
104 hdf5_group=new_group._v_pathname,
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
105 index_names=index_names,
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
106 other_targets=other_targets,
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
107 skip_hdf5_append=skip_hdf5_append)
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
108
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
109 all_series.append( \
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
110 AccumulatorSeriesWrapper( \
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
111 base_series=series_base,
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
112 reduce_every=reduce_every))
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
113
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
114 ret_wrapper = SeriesArrayWrapper(all_series)
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
115
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
116 return ret_wrapper
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
117
f37c0705649d Problèmes de révisions hg... tentative de merger
fsavard
parents:
diff changeset
118