/
QuadPoiss_old.py
181 lines (160 loc) · 7.77 KB
/
QuadPoiss_old.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
"""
Linear-Quadratic-Linear-Exponential-Poisson model.
All functions return a SYMBOLIC representation (using theano) of
some desired quantity, as a function of the symbolic arguments.
@author: kolia
"""
import theano.tensor as Th
from theano.sandbox.linalg import matrix_inverse, det
from kolia_theano import eig, logdet
#from IPython.Debugger import Tracer; debug_here = Tracer()
def quadratic_Poisson( theta = Th.dvector('theta'), M = Th.dmatrix('M') ,
STA = Th.dvector('STA') , STC = Th.dmatrix('STC'),
N_spike = Th.dscalar('N_spike'), logprior = 0 ,
**other):
'''
The actual quadratic-Poisson model, as a function of theta and M,
with a barrier on the log-det term and a prior.
'''
ImM = Th.identity_like(M)-(M+M.T)/2
ldet = logdet(ImM) # Th.log( det( ImM) ) # logdet(ImM)
return -0.5 * N_spike *(
ldet + logprior \
- 1./(ldet+250.)**2. \
- Th.sum(Th.dot(matrix_inverse(ImM),theta) * theta) \
+ 2. * Th.sum( theta * STA ) \
+ Th.sum( M * (STC + Th.outer(STA,STA)) ))
def LNLEP( theta = Th.dvector('theta'), M = Th.dmatrix('M') ,
STA = Th.dvector('STA') , STC = Th.dmatrix('STC'),
N_spike = Th.dscalar('N_spike'), **other):
'''
The actual quadratic-Poisson model, as a function of theta and M,
without any barriers or priors.
'''
ImM = Th.identity_like(M)-(M+M.T)/2
ldet = logdet(ImM) # Th.log( det( ImM) ) # logdet(ImM)
return -0.5 * N_spike *(
ldet \
- Th.sum(Th.dot(matrix_inverse(ImM),theta) * theta) \
+ 2. * Th.sum( theta * STA ) \
+ Th.sum( M * (STC + Th.outer(STA,STA)) ))
def eig_pos_barrier( theta = Th.dvector('theta'), M = Th.dmatrix('M') ,
STA = Th.dvector('STA'), STC = Th.dmatrix('STC'),
U = Th.dmatrix('U') , V1 = Th.dvector('V1'), **other):
'''
A barrier enforcing that the log-det of M should be > exp(-6),
and all the eigenvalues of M > 0. Returns true if barrier is violated.
'''
ImM = Th.identity_like(M)-(M+M.T)/2
w,v = eig( ImM )
return 1-(Th.sum(Th.log(w))>-250)*(Th.min(w)>0)*(Th.min(V1.flatten())>0) \
# (Th.min(V1.flatten())>0)*(Th.min(U.flatten())>0)
def eig_barrier( M = Th.dmatrix('M') ,
# theta = Th.dvector('theta'),
# STA = Th.dvector('STA'), STC = Th.dmatrix('STC'),
# U = Th.dmatrix('U') , V1 = Th.dvector('V1'),
**other):
'''
A barrier enforcing that the log-det of M should be > exp(-6),
and all the eigenvalues of M > 0. Returns true if barrier is violated.
'''
ImM = Th.identity_like(M)-(M+M.T)/2
w,v = eig( ImM )
return 1-(Th.sum(Th.log(w))>-250)*(Th.min(w)>0) #*(Th.min(V1.flatten())>0)
def eigsM( M = Th.dmatrix('M') , **result):
w,v = eig( Th.identity_like(M)-(M+M.T)/2 )
return w
def invM( M = Th.dmatrix('M') , **result):
return matrix_inverse( Th.identity_like(M)-(M+M.T)/2 )
def logdetIM( M = Th.dmatrix('M') , **result):
return logdet( Th.identity_like(M)-(M+M.T)/2 )
def log_detIM( M = Th.dmatrix('M') , **result):
return Th.log( det( Th.identity_like(M)-(M+M.T)/2 ) )
def M( M = Th.dmatrix('M') , **result): return M
def theta( theta = Th.dvector('M') , **result): return theta
def UV( U = Th.dmatrix('U') , V1 = Th.dvector('V1') , V2 = Th.dvector('V2') , **result):
'''
Reparameterize theta and M as a function of U, V1 and V2.
'''
result['theta'] = Th.dot( U.T , V1 )
result['M' ] = Th.dot( V1 * U.T , (V2 * U.T).T )
return result
def UVs(N):
'''
Reparameterize a list of N (theta,M) parameters as a function of a
common U,V2 and a matrix of N rows containing V1.
'''
def UV( U = Th.dmatrix('U') , V1 = Th.dmatrix('V1') , V2 = Th.dvector('V2') ,
STAs = Th.dmatrix('STAs'), STCs = Th.dtensor3('STCs'),
N_spikes = Th.dvector('N_spikes'), **other):
return [{'theta': Th.dot( U.T , V1[i,:] ) ,
'M' : Th.dot( V1[i,:] * U.T , (V2 * U.T).T ),
'STA': STAs[i,:],
'STC': STCs[i,:,:],
'N_spike': N_spikes[i]/(Th.sum(N_spikes)) ,
'U' : U,
'logprior': 0. } for i in range(N)]
return UV
def linear_reparameterization( T = Th.dtensor3('T') , u = Th.dvector('u') ,
# V1 = Th.dmatrix('V1') , V2 = Th.dvector('V2') ,
# STAs = Th.dmatrix('STAs'), STCs = Th.dtensor3('STCs'),
# N_spikes = Th.dvector('N_spikes'),
**other):
other['U'] = Th.sum( T*u , axis=2 )
# other[name] = Th.tensordot(T,u,axes=0)
return other
def UVs_old(N):
'''
Reparameterize a list of N (theta,M) parameters as a function of a
common U,V2 and a matrix of N rows containing V1.
'''
def UV( U = Th.dmatrix('U') , V1 = Th.dmatrix('V1') , V2 = Th.dvector('V2') ,
STAs = Th.dmatrix('STAs'), STCs = Th.dtensor3('STCs'),
centers= Th.dvector('centers'), indices = Th.dmatrix('indices'), lam=Th.dscalar('lam'),
lambdas= Th.dvector('lambdas') ,
N_spikes = Th.dvector('N_spikes'), Ncones = Th.dscalar('Ncones'), **other):
return [{'theta': Th.dot( U.T , V1[i,:] ) ,
'M' : Th.dot( V1[i,:] * U.T , (V2 * U.T).T ),
'STA': STAs[i,:],
'STC': STCs[i,:,:],
'N_spike': N_spikes[i]/(Th.sum(N_spikes)) ,
'U' : U,
'logprior': - Th.sum( Th.sqrt(Th.sum(V1**2.,axis=0) + 0.000001) * lambdas) } for i in range(N)]
# 'logprior': Th.sum(0.001*Th.log(V1)) - Th.sum( Th.sqrt(Th.sum(V1**2.,axis=0)) * lambdas) } for i in range(N)]
# 'logprior': Th.sum(0.001*Th.log(V1)) } for i in range(N)]
# 'logprior': 0. } for i in range(N)]
# 'logprior': Th.sum(0.001*Th.log(U)) - lam * Th.sum( (0.5-Th.cos((indices.T-centers)*2.*pi/Ncones).T)*4. * U**2. ) } for i in range(N)]
# 'logprior': lam * Th.sum( (1-Th.cos((indices.T-centers)*2.*pi/Ncones).T) * U**2. ) } for i in range(N)]
# 'logprior': Th.sum(0.001*Th.log(V1)) + lam * Th.sum( (1-Th.cos((indices.T-centers)*2.*pi/Ncones).T) * U**2. ) } for i in range(N)]
# 'logprior': Th.sum(0.001*Th.log(U)) + Th.sum(0.001*Th.log(V1)) } for i in range(N)]
return UV
def lUVs(N):
'''
Reparameterize a list of N (theta,M) parameters as a function of a
common log(U),V2 and a matrix of N rows containing log(V1).
'''
def UV( lU = Th.dmatrix('lU') , lV1 = Th.dmatrix('lV1') , V2 = Th.dvector('V2') ,
STAs = Th.dmatrix('STAs'), STCs = Th.dtensor3('STCs'), **other):
U = Th.exp(lU + 1e-10)
V1 = Th.exp(lV1+ 1e-10)
return [{'theta': Th.dot( U.T , V1[i] ) ,
'M' : Th.dot( V1[i] * U.T , (V2 * U.T).T ),
'STA': STAs[i,:],
'STC': STCs[i,:,:]} for i in range(N)]
return UV
def eigs( theta = Th.dvector('theta'), M = Th.dmatrix('M') ,
STA = Th.dvector('STA') , STC = Th.dmatrix('STC'), **other):
'''
Return eigenvalues of I-sym(M), for display/debugging purposes.
'''
ImM = Th.identity_like(M)-(M+M.T)/2
w,v = eig( ImM )
return w
def ldet( theta = Th.dvector('theta'), M = Th.dmatrix('M') ,
STA = Th.dvector('STA'), STC = Th.dmatrix('STC'), **other):
'''
Return log-det of I-sym(M), for display/debugging purposes.
'''
ImM = Th.identity_like(M)-(M+M.T)/2
w, v = eig(ImM)
return Th.sum(Th.log(w))