-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
173 lines (134 loc) · 5.3 KB
/
model.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
# running in parallel on a multi-core machine
import sys
try:
tag = sys.argv[1]
except:
tag = 'HULK'
print('tag =', tag)
try:
n_jobs = int(sys.argv[2])
except:
n_jobs = 4
n_jobs = 9
n_jobs = 10
n_jobs = 1
n_jobs = 35
n_jobs = 0
print('n_jobs =', n_jobs)
from shl_scripts.shl_experiments import SHL, prun
# pre-loading data
datapath = '../SparseHebbianLearning/database'
opts = dict(datapath=datapath, verbose=0)
shl = SHL(**opts)
data = shl.get_data(matname=tag)
# running main simulations
# Figure 1 & 3
N_cv = 10 # cross-validate with 10 different learnings
homeo_methods = ['None', 'OLS', 'HEH', 'HAP', 'EMP']
seed = 42
import numpy as np
np.set_printoptions(precision=2, suppress=True)
np.random.seed(seed)
if n_jobs>0:
# Figure 1 & 3
list_figures = []
from shl_scripts.shl_experiments import SHL_set
for homeo_method in homeo_methods:
opts_ = opts.copy()
opts_.update(homeo_method=homeo_method)
experiments = SHL_set(opts_, tag=tag + '_' + homeo_method, N_scan=N_cv)
experiments.run(variables=['seed'], n_jobs=n_jobs, verbose=0)
# Figure 2-B
variables = ['eta', 'eta_homeo']
list_figures = []
for homeo_method in homeo_methods:
opts_ = opts.copy()
opts_.update(homeo_method=homeo_method)
experiments = SHL_set(opts_, tag=tag + '_' + homeo_method, base=10)
experiments.run(variables=variables, n_jobs=n_jobs, verbose=0)
# Annex X.X
homeo_methods = ['None', 'EMP', 'HAP', 'HEH', 'OLS']
variables = ['l0_sparseness', 'n_dictionary']
list_figures = []
#n_dictionary=21**2
for homeo_method in homeo_methods:
opts_ = opts.copy()
opts_.update(homeo_method=homeo_method, datapath=datapath)
experiments = SHL_set(opts_, tag=tag + '_' + homeo_method)
experiments.run(variables=variables, n_jobs=1, verbose=0)
#for algorithm in ['lasso_lars', 'lasso_cd', 'lars', 'elastic', 'omp', 'mp']: # 'threshold',
# opts_ = opts.copy()
# opts_.update(homeo_method='None', learning_algorithm=algorithm, verbose=0)
# shl = SHL(**opts_)
# dico= shl.learn_dico(data=data, list_figures=[],
# matname=tag + ' - algorithm={}'.format(algorithm))
#for homeo_method in ['None', 'HAP']:
# for algorithm in ['lasso_lars', 'lars', 'elastic', 'omp', 'mp']: # 'threshold', 'lasso_cd',
# opts_ = opts.copy()
# opts_.update(homeo_method=homeo_method, learning_algorithm=algorithm, verbose=0)
# shl = SHL(**opts_)
# dico= shl.learn_dico(data=data, list_figures=[],
# matname=tag + ' - algorithm={}'.format(algorithm) + ' - homeo_method={}'.format(homeo_method))
shl = SHL(one_over_F=False, **opts)
dico_w = shl.learn_dico(data=data, matname=tag + '_WHITE', list_figures=[])
shl = SHL(one_over_F=True, **opts)
dico_1oF = shl.learn_dico(data=data, matname=tag + '_OVF', list_figures=[])
shl = SHL(beta1=0., **opts)
dico_fixed = shl.learn_dico(data=data, matname=tag + '_fixed', list_figures=[])
shl = SHL(**opts)
dico_default = shl.learn_dico(data=data, matname=tag + '_default', list_figures=[])
else:
# some overhead for the formatting of figures
import matplotlib.pyplot as plt
fontsize = 12
FORMATS = ['.pdf', '.eps', '.png', '.tiff']
FORMATS = ['.pdf', '.png']
dpi_export = 600
fig_width_pt = 318.670 # Get this from LaTeX using \showthe\columnwidth
fig_width_pt = 450 # Get this from LaTeX using \showthe\columnwidth
#fig_width_pt = 1024 #221 # Get this from LaTeX using \showthe\columnwidth / x264 asks for a multiple of 2
ppi = 72.27 # (constant) definition of the ppi = points per inch
inches_per_pt = 1.0/ppi # Convert pt to inches
#inches_per_cm = 1./2.54
fig_width = fig_width_pt*inches_per_pt # width in inches
grid_fig_width = 2*fig_width
phi = (np.sqrt(5) + 1. ) /2
#legend.fontsize = 8
#fig_width = 9
fig_height = fig_width/phi
figsize = (fig_width, fig_height)
def adjust_spines(ax, spines):
for loc, spine in ax.spines.items():
if loc in spines:
spine.set_position(('outward', 10)) # outward by 10 points
spine.set_smart_bounds(True)
else:
spine.set_color('none') # don't draw spine
# turn off ticks where there is no spine
if 'left' in spines:
ax.yaxis.set_ticks_position('left')
else:
# no yaxis ticks
ax.yaxis.set_ticks([])
if 'bottom' in spines:
ax.xaxis.set_ticks_position('bottom')
else:
# no xaxis ticks
ax.xaxis.set_ticks([])
import matplotlib
pylab_defaults = {
'font.size': 10,
'xtick.labelsize':'medium',
'ytick.labelsize':'medium',
'text.usetex': False,
'font.family' : 'sans-serif',
'font.sans-serif' : ['DejaVu Sans'],#['Optima'],#['Palatino'],#
}
#matplotlib.rcParams.update({'font.size': 18, 'font.family': 'STIXGeneral', 'mathtext.fontset': 'stix'})
matplotlib.rcParams.update(pylab_defaults)
#matplotlib.rcParams.update({'text.usetex': True})
import matplotlib.cm as cm
from IPython.display import Image
DEBUG = True
DEBUG = False
hl, hs = 10*'-', 10*' '