-
Notifications
You must be signed in to change notification settings - Fork 7
/
linear.go
359 lines (299 loc) · 9.01 KB
/
linear.go
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// Copyright (c) 2024, The Emergent Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package kinase
import (
"fmt"
"log/slog"
"math/rand"
"cogentcore.org/core/math32"
"cogentcore.org/core/tensor"
"cogentcore.org/core/tensor/stats/glm"
"cogentcore.org/core/tensor/table"
)
// Linear performs a linear regression to approximate the synaptic Ca
// integration between send and recv neurons.
type Linear struct {
// Kinase Neuron params
Neuron NeurCaParams
// Kinase Synapse params
Synapse SynCaParams
// total number of cycles (1 MSec) to run per learning trial
NCycles int `min:"10" default:"200"`
// number of plus cycles
PlusCycles int `default:"50"`
// NumBins is the number of bins to accumulate spikes over NCycles
NumBins int `default:"8"`
// CyclesPerBin = NCycles / NumBins
CyclesPerBin int `edit:"-"`
// MaxHz is the maximum firing rate to sample in minus, plus phases
MaxHz int `default:"120"`
// StepHz is the step size for sampling Hz
StepHz int `default:"10"`
// NTrials is number of trials per Hz case
NTrials int `default:"100"`
// Total Trials is number of trials for all data
TotalTrials int `edit:"-"`
// Sending neuron
Send Neuron
// Receiving neuron
Recv Neuron
// Standard synapse values
StdSyn Synapse
// Linear synapse values
LinearSyn Synapse
// ErrDWt is the target error dwt: PlusHz - MinusHz
ErrDWt float32
// binned integration of send, recv spikes
SpikeBins []float32
// Data to fit the regression
Data table.Table
}
func (ls *Linear) Defaults() {
ls.Neuron.Defaults()
ls.Synapse.Defaults()
ls.Synapse.CaScale = 6 // 12 is too fast relative to prior std learning rates
ls.NCycles = 200
ls.PlusCycles = 50
ls.CyclesPerBin = 50
ls.MaxHz = 100
ls.StepHz = 10 // note: 5 gives same results
ls.NTrials = 2 // 20 "
ls.NumBins = 8
ls.Update()
}
func (ls *Linear) Update() {
ls.CyclesPerBin = ls.NCycles / ls.NumBins
ls.Neuron.Dt.PDTauForNCycles(ls.NCycles)
ls.Synapse.Dt.PDTauForNCycles(ls.NCycles)
nhz := ls.MaxHz / ls.StepHz
ls.TotalTrials = nhz * nhz * nhz * nhz * ls.NTrials
ls.SpikeBins = make([]float32, ls.NumBins)
ls.Send.SpikeBins = make([]float32, ls.NumBins)
ls.Recv.SpikeBins = make([]float32, ls.NumBins)
}
func (ls *Linear) Init() {
ls.Send.Init()
ls.Recv.Init()
ls.StdSyn.Init()
ls.LinearSyn.Init()
ls.InitTable()
}
func (ls *Linear) InitTable() {
if ls.Data.NumColumns() > 0 {
return
}
nneur := ls.NumBins
ls.Data.AddIntColumn("Trial")
ls.Data.AddFloat64Column("Hz", 4)
ls.Data.AddFloat64Column("State", nneur)
ls.Data.AddFloat64Column("StdCa", 2)
ls.Data.AddFloat64Column("PredCa", 2)
ls.Data.AddFloat64Column("ErrCa", 2)
ls.Data.AddFloat64Column("SSE") // total SSE
ls.Data.SetNumRows(ls.TotalTrials)
}
func (ls *Linear) StartTrial() {
ls.Send.StartTrial()
ls.Recv.StartTrial()
}
// Neuron has Neuron state
type Neuron struct {
// Neuron spiking (0,1)
Spike float32
// Neuron probability of spiking
SpikeP float32
// CaSyn is spike-driven calcium trace for synapse-level Ca-driven learning:
// exponential integration of SpikeG * Spike at SynTau time constant (typically 30).
// Synapses integrate send.CaSyn * recv.CaSyn across M, P, D time integrals for
// the synaptic trace driving credit assignment in learning.
// Time constant reflects binding time of Glu to NMDA and Ca buffering postsynaptically,
// and determines time window where pre * post spiking must overlap to drive learning.
CaSyn float32
// neuron-level spike-driven Ca integration
CaSpkM, CaSpkP, CaSpkD float32
TotalSpikes float32
// binned count of spikes, for regression learning
SpikeBins []float32
}
func (kn *Neuron) Init() {
kn.Spike = 0
kn.SpikeP = 1
kn.CaSyn = 0
kn.CaSpkM = 0
kn.CaSpkP = 0
kn.CaSpkD = 0
kn.StartTrial()
}
func (kn *Neuron) StartTrial() {
kn.TotalSpikes = 0
for i := range kn.SpikeBins {
kn.SpikeBins[i] = 0
}
}
// Cycle does one cycle of neuron updating, with given exponential spike interval
// based on target spiking firing rate.
func (ls *Linear) Cycle(nr *Neuron, expInt float32, cyc int) {
nr.Spike = 0
bin := cyc / ls.CyclesPerBin
if expInt > 0 {
nr.SpikeP *= rand.Float32()
if nr.SpikeP <= expInt {
nr.Spike = 1
nr.SpikeP = 1
nr.TotalSpikes += 1
nr.SpikeBins[bin] += 1
}
}
ls.Neuron.CaFromSpike(nr.Spike, &nr.CaSyn, &nr.CaSpkM, &nr.CaSpkP, &nr.CaSpkD)
}
// Synapse has Synapse state
type Synapse struct {
CaSyn float32
// CaM is first stage running average (mean) Ca calcium level (like CaM = calmodulin), feeds into CaP
CaM float32
// CaP is shorter timescale integrated CaM value, representing the plus, LTP direction of weight change and capturing the function of CaMKII in the Kinase learning rule
CaP float32
// CaD is longer timescale integrated CaP value, representing the minus, LTD direction of weight change and capturing the function of DAPK1 in the Kinase learning rule
CaD float32
// DWt is the CaP - CaD
DWt float32
}
func (ks *Synapse) Init() {
ks.CaSyn = 0
ks.CaM = 0
ks.CaP = 0
ks.CaD = 0
ks.DWt = 0
}
// Run generates data
func (ls *Linear) Run() {
nhz := ls.MaxHz / ls.StepHz
hz := make([]float32, nhz)
i := 0
for h := float32(ls.StepHz); h <= float32(ls.MaxHz); h += float32(ls.StepHz) {
hz[i] = h
i++
}
row := 0
for smi := 0; smi < nhz; smi++ {
sendMinusHz := hz[smi]
for spi := 0; spi < nhz; spi++ {
sendPlusHz := hz[spi]
for rmi := 0; rmi < nhz; rmi++ {
recvMinusHz := hz[rmi]
for rpi := 0; rpi < nhz; rpi++ {
recvPlusHz := hz[rpi]
for ti := 0; ti < ls.NTrials; ti++ {
ls.Trial(sendMinusHz, sendPlusHz, recvMinusHz, recvPlusHz, ti, row)
row++
}
}
}
}
}
}
func (ls *Linear) SetSynState(sy *Synapse, row int) {
ls.Data.Column("StdCa").SetFloatRowCell(float64(sy.CaP), row, 0)
ls.Data.Column("StdCa").SetFloatRowCell(float64(sy.CaD), row, 1)
}
func (ls *Linear) SetBins(sn, rn *Neuron, off, row int) {
for i, s := range sn.SpikeBins {
r := rn.SpikeBins[i]
bs := (r * s) / 10.0
ls.SpikeBins[i] = bs
ls.Data.Column("State").SetFloatRowCell(float64(bs), row, off+i)
}
}
// Trial runs one trial
func (ls *Linear) Trial(sendMinusHz, sendPlusHz, recvMinusHz, recvPlusHz float32, ti, row int) {
// ls.ErrDWt = (plusHz - minusHz) / 100
ls.Data.Column("Trial").SetFloatRow(float64(ti), row)
ls.Data.Column("Hz").SetFloatRowCell(float64(sendMinusHz), row, 0)
ls.Data.Column("Hz").SetFloat(float64(sendPlusHz), row, 1)
ls.Data.Column("Hz").SetFloat(float64(recvMinusHz), row, 2)
ls.Data.Column("Hz").SetFloat(float64(recvPlusHz), row, 3)
minusCycles := ls.NCycles - ls.PlusCycles
ls.StartTrial()
cyc := 0
for phs := 0; phs < 2; phs++ {
var maxcyc int
var rhz, shz float32
switch phs {
case 0:
rhz = recvMinusHz
shz = sendMinusHz
maxcyc = minusCycles
case 1:
rhz = recvPlusHz
shz = sendPlusHz
maxcyc = ls.PlusCycles
}
Rint := math32.Exp(-1000.0 / float32(rhz))
Sint := math32.Exp(-1000.0 / float32(shz))
for t := 0; t < maxcyc; t++ {
ls.Cycle(&ls.Send, Sint, cyc)
ls.Cycle(&ls.Recv, Rint, cyc)
ls.StdSyn.CaSyn = ls.Send.CaSyn * ls.Recv.CaSyn
ls.Synapse.FromCa(ls.StdSyn.CaSyn, &ls.StdSyn.CaM, &ls.StdSyn.CaP, &ls.StdSyn.CaD)
cyc++
}
}
ls.StdSyn.DWt = ls.StdSyn.CaP - ls.StdSyn.CaD
ls.SetSynState(&ls.StdSyn, row)
ls.SetBins(&ls.Send, &ls.Recv, 0, row)
}
// Regress runs the linear regression on the data
func (ls *Linear) Regress() {
r := glm.NewGLM()
err := r.SetTable(&ls.Data, "State", "StdCa", "PredCa", "ErrCa")
if err != nil {
slog.Error(err.Error())
return
}
r.DepNames = []string{"CaP", "CaD"}
r.L1Cost = 0.1
r.L2Cost = 0.1
r.StopTolerance = 0.00001
r.ZeroOffset = true
// NBins = 4
// r.Coeff.Values = []float64{
// 0.05, 0.25, 0.5, 0.6, 0, // linear progression
// 0.25, 0.5, 0.5, 0.25, 0} // hump in the middle
// NBins = 8, 200+50 cycles
// r.Coeff.Values = []float64{
// 0.3, 0.4, 0.55, 0.65, 0.75, 0.85, 1.0, 1.0, 0, // linear progression
// 0.5, 0.65, 0.75, 0.9, 0.9, 0.9, 0.65, 0.55, .0} // hump in the middle
// NBins = 8, 280+70 cycles
r.Coeff.Values = []float64{
0.0, 0.1, 0.23, 0.35, 0.45, 0.55, 0.75, 0.75, 0, // linear progression
0.2, 0.3, 0.4, 0.5, 0.5, 0.5, 0.4, 0.3, .0} // hump in the middle
fmt.Println(r.Coeffs())
r.Run()
fmt.Println(r.Variance())
fmt.Println(r.Coeffs())
/*
for vi := 0; vi < 2; vi++ {
r := new(regression.Regression)
r.SetObserved("CaD")
for bi := 0; bi < ls.NumBins; bi++ {
r.SetVar(bi, fmt.Sprintf("Bin_%d", bi))
}
for row := 0; row < ls.Data.Rows; row++ {
st := ls.Data.Tensor("State", row).(*tensor.Float64)
cad := ls.Data.TensorFloat1D("StdCa", row, vi)
r.Train(regression.DataPoint(cad, st.Values))
}
r.Run()
fmt.Printf("Regression formula:\n%v\n", r.Formula)
fmt.Printf("Variance observed = %v\nVariance Predicted = %v", r.Varianceobserved, r.VariancePredicted)
fmt.Printf("\nR2 = %v\n", r.R2)
str := "{"
for ci := 0; ci <= ls.NumBins; ci++ {
str += fmt.Sprintf("%8.6g, ", r.Coeff(ci))
}
fmt.Println(str + "}")
}
*/
ls.Data.SaveCSV("linear_data.tsv", tensor.Tab, table.Headers)
}