/
sparse_coding.py
156 lines (111 loc) · 4.05 KB
/
sparse_coding.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
#!/Library/Frameworks/Python.framework/Versions/2.7/bin/python
"""Particle filtering for nonlinear dynamic systems observed through adaptive poisson neurons"""
import particlefilter as pf
import gaussianenv as ge
import poissonneuron as pn
import numpy as np
import cPickle as pic
import multiprocessing as mp
import sys
#parameter definitions
plotting = True
dthetas =np.arange(0.01,2.0,0.02)
alphas = np.arange(0.01,2.2,0.02)
sparse_eps = np.zeros((dthetas.size,alphas.size))
dense_eps = np.zeros((dthetas.size,alphas.size))
particle_eps = np.zeros((dthetas.size,alphas.size))
try:
filename = sys.argv[1]
except:
filename = 'dense_sparse.pik'
def run_filters(args):
i,j,dtheta,alpha = args
print i,j
dt = 0.001
phi = 1.0
zeta = 1.0
eta = 1.0
gamma = 1.0
timewindow = 30000
dm = 0.0
tau = 1.0
nparticles = 1000
#env is the "environment", that is, the true process to which we don't have access
env_rng = np.random.mtrand.RandomState()
env = ge.GaussianEnv(gamma=gamma,eta=eta,zeta=zeta,x0=0.0,
y0=.0,L=1.0,N=1,order=1,sigma=0.1,
Lx=1.0,Ly=1.0,randomstate=env_rng)
env.reset(np.array([0.0]))
#code is the population of neurons, plastic poisson neurons
code_rng = np.random.mtrand.RandomState()
code = pn.PoissonPlasticCode(phi=phi,tau=tau,alpha=alpha,
thetas=np.arange(-dtheta,1.1*dtheta,2*dtheta),
dm=dm,randomstate=code_rng)
env_rng.seed(12345)
code_rng.seed(67890)
env.reset(np.array([0.0]))
code.reset()
[densem,densevar,spsg,sg,dense_mse] = pf.gaussian_filter(code,env,timewindow=timewindow,dt=dt,
dense=True)
env_rng.seed(12345)
code_rng.seed(67890)
env.reset(np.array([0.0]))
code.reset()
[sparsem,sparsevar,spsg,sg,sparse_mse] = pf.gaussian_filter(code,env,timewindow=timewindow,dt=dt,
dense=False)
env_rng.seed(12345)
code_rng.seed(67890)
env.reset(np.array([0.0]))
code.reset()
[particle_mse,ws] = pf.mse_particle_filter(code, env, timewindow=timewindow,
dt=dt, nparticles=nparticles,
testf=(lambda x:x))
return i,j,dense_mse,sparse_mse,particle_mse
try:
dic = pic.load( open(filename,'r'))
sparse_eps = dic['sparse_eps']
dense_eps = dic['dense_eps']
particle_eps = dic['particle_eps']
alphas = dic['alphas']
dthetas = dic['dthetas']
print "ALL GOOD"
except:
args = []
for i,dtheta in enumerate(dthetas):
for j,alpha in enumerate(alphas):
args.append((i,j,dtheta,alpha))
p = mp.Pool(mp.cpu_count())
results = p.map(run_filters,args)
for res in results:
i,j,dense_mse,sparse_mse,msep = res
dense_eps[i,j] = dense_mse
sparse_eps[i,j] = sparse_mse
particle_eps[i,j] = msep
with open(filename,"wb") as f:
pic.dump({'dense_eps':dense_eps,
'sparse_eps':sparse_eps,
'particle_eps':particle_eps,
'dthetas':dthetas,
'alphas':alphas},
f)
if plotting:
from prettyplotlib import plt
import prettyplotlib as ppl
#matplotlib.rcParams['font.size']=10
xs,ys = np.where(np.isnan(sparse_eps))
for x,y in zip(xs,ys):
sparse_eps[x,y] = 0.5
max1 = np.max(sparse_eps)
max2 = np.max(dense_eps)
max3 = np.max(particle_eps)
maxtotal = np.max([max1,max2,max3])
min1 = np.min(sparse_eps)
min2 = np.min(dense_eps)
min3 = np.min(particle_eps)
mintotal = np.min([min1,min2,min3])
fig, (ax1,ax2,ax3) = ppl.subplots(3,1)
p1 = ppl.pcolormesh(fig,ax1,dense_eps)
p2 = ppl.pcolormesh(fig,ax2,sparse_eps)
p3 = ppl.pcolormesh(fig,ax3,particle_eps)
[p.set_clim(vmin=mintotal,vmax=maxtotal) for p in [p1,p2,p3]]
plt.show()