working milestone with 25% accuracy
This commit is contained in:
@@ -34,7 +34,6 @@ using .learn
|
||||
|
||||
""" version 0.0.5
|
||||
Todo:
|
||||
-
|
||||
[4] implement dormant connection
|
||||
[] using RL to control learning signal
|
||||
[] consider using Dates.now() instead of timestamp because time_stamp may overflow
|
||||
@@ -43,34 +42,60 @@ using .learn
|
||||
|
||||
|
||||
Change from version: 0.0.4
|
||||
- compute error in main loop so one could decide how to calculate error
|
||||
- compute model error in main loop so one could decide when to calculate error
|
||||
- compute model error in main loop so one could decide when to calculate error in
|
||||
training sequence and how to calculate
|
||||
- fix ALIF adaptation formula, now n.a compute avery time step
|
||||
- add higher input signal to noise ratio
|
||||
- no noise generate
|
||||
- increase input signal by adding more input neuron population
|
||||
- add /100 every wRec and b
|
||||
- add integrateNeuron
|
||||
- ΔwRecChange during input signal ingestion will be merged at the end of learning
|
||||
- use Flux.logitcrossentropy for overall error
|
||||
- move timestep_forward!() in kfn's forward to the beginning so that v_t and z_t is reset
|
||||
- fix n.a formula in forward() and calculate both non-firing and firing state
|
||||
- RSNN use overall modelError to update while integrate neuron use error with respect to
|
||||
itself (yk - yk*) to update
|
||||
- all RSNN neuron connect to integrateNeuron
|
||||
- integrateNeuron does NOT repect RSNN excitatory and inhabitory sign
|
||||
- weaker connection should be harder to increase strength. It requires a lot of
|
||||
repeat activation to get it stronger. While strong connction requires a lot of
|
||||
inactivation to get it weaker. The concept is strong connection will lock
|
||||
correct neural pathway through repeated use of the right connection i.e. keep training
|
||||
on the correct answer -> strengthen the right neural pathway (connections) ->
|
||||
this correct neural pathway resist to change.
|
||||
Not used connection should dissapear (forgetting).
|
||||
|
||||
|
||||
All features
|
||||
- ΔwRecChange during input signal ingestion will be merged at the end of learning
|
||||
- all RSNN and output neuron learning associate.
|
||||
- synapticStrength apply at the end of learning
|
||||
- collect ΔwRecChange during online learning (0-784th) and merge with wRec at
|
||||
the end learning (1000th).
|
||||
- compute model error at the end learning. Model error times with 5 constant for
|
||||
higher learning impact than the error during online
|
||||
the end learning (800th).
|
||||
- multidispatch + for loop as main compute method
|
||||
- hard connection constrain yes
|
||||
- normalize output yes
|
||||
- allow -w_rec yes
|
||||
- voltage drop when neuron fires voltage drop equals to vth
|
||||
- v_t decay during refractory
|
||||
duration exponantial decay
|
||||
- voltage drop when neuron fires voltage drop equals to vRest
|
||||
- v_t decay during refractory
|
||||
- input data population encoding, each pixel data =>
|
||||
population encoding, ralative between pixel data
|
||||
- compute neuron weight init rand()
|
||||
- output neuron weight init randn()
|
||||
- each knowledgeFn should have its own noise generater
|
||||
- where to put pseudo derivative (n.phi)
|
||||
|
||||
- compute pseudo derivative (n.phi) every time step
|
||||
- add excitatory, inhabitory to neuron
|
||||
- implement "start learning", reset learning and "learning", "end_learning and
|
||||
"inference"
|
||||
- synaptic connection strength concept. use sigmoid, turn connection offline
|
||||
- neuroplasticity() i.e. change connection
|
||||
- add multi threads
|
||||
|
||||
|
||||
Removed features
|
||||
- normalize output yes
|
||||
<logitcrossentropy does not need normalization>
|
||||
- compute model error at the end learning. Model error times with 5 constant for
|
||||
higher learning impact than the error during online
|
||||
<there should be no difference between error in each timestep because error
|
||||
has equal importance>
|
||||
- output neuron connect to random multiple compute neurons and overall have
|
||||
the same structure as lif
|
||||
- time-based learning method based on new error formula
|
||||
@@ -79,28 +104,23 @@ using .learn
|
||||
(vth - vt)*100/vth as error
|
||||
if output neuron activates when it should NOT, use output neuron's
|
||||
(vt*100)/vth as error
|
||||
<use logitcrossentropy>
|
||||
- use LinearAlgebra.normalize!(vector, 1) to adjust weight after weight merge
|
||||
<it reduce instant respond of neuron. Sometime postsynaptic neuron need to
|
||||
respond quickly at differnt neural pathway. If wRec is normalized, weights that
|
||||
needs to be high to allow neuron instant respond get reduced.>
|
||||
- reset_epsilonRec after ΔwRecChange is calculated
|
||||
- synaptic connection strength concept. use sigmoid, turn connection offline
|
||||
- wRec should not normalized whole. it should be local 5 conn normalized.
|
||||
- neuroplasticity() i.e. change connection
|
||||
- add multi threads
|
||||
- add maximum weight cap of each connection
|
||||
|
||||
<training example does not require intermediate respond from RSNN>
|
||||
- add maximum weight cap of each connection
|
||||
<capping weight limit neuron ability to adjust its respond>
|
||||
- wRec should not normalized whole. it should be local 5 conn normalized.
|
||||
<it makes small weight bigger>
|
||||
|
||||
Removed features
|
||||
|
||||
Ideas to try
|
||||
- Δweight * connection strength
|
||||
- reset_epsilonRec after ΔwRecChange is calculated
|
||||
- ΔwRecChange that apply immediately during online learning
|
||||
- error by percent of vth-v_t1
|
||||
- Δweight * connection strength
|
||||
- weaker connection should be harder to increase strength. It requires a lot of
|
||||
repeat activation to get it stronger. While strong connction requires a lot of
|
||||
inactivation to get it weaker. The concept is strong connection will lock
|
||||
correct neural pathway through repeated use of the right connection i.e. keep training
|
||||
on the correct answer -> strengthen the right neural pathway (connections) ->
|
||||
this correct neural pathway resist to change.
|
||||
Not used connection should dissapear (forgetting).
|
||||
- during 0 training if 1-9 output neuron fires, adjust weight only those neurons
|
||||
- use Flux.logitcrossentropy for overall error
|
||||
"""
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
module forward
|
||||
|
||||
using Statistics, Random, LinearAlgebra, JSON3
|
||||
using Statistics, Random, LinearAlgebra, JSON3, Flux
|
||||
using GeneralUtils
|
||||
using ..types, ..snn_utils
|
||||
|
||||
@@ -26,7 +26,13 @@ end
|
||||
|
||||
function (kfn::kfn_1)(m::model, input_data::AbstractVector)
|
||||
kfn.timeStep = m.timeStep
|
||||
|
||||
for n in kfn.neuronsArray
|
||||
timestep_forward!(n)
|
||||
end
|
||||
for n in kfn.outputNeuronsArray
|
||||
timestep_forward!(n)
|
||||
end
|
||||
|
||||
kfn.learningStage = m.learningStage
|
||||
|
||||
if kfn.learningStage == "start_learning"
|
||||
@@ -54,19 +60,12 @@ function (kfn::kfn_1)(m::model, input_data::AbstractVector)
|
||||
end
|
||||
|
||||
# generate noise
|
||||
noise = [GeneralUtils.randomChoiceWithProb([true, false],[0.5,0.5])
|
||||
noise = [GeneralUtils.randomChoiceWithProb([true, false],[0.0, 1.0])
|
||||
for i in 1:length(input_data)]
|
||||
# noise = [rand(rng, Distributions.Binomial(1, 0.5)) for i in 1:10] # another option
|
||||
|
||||
input_data = [noise; input_data] # noise must start from neuron id 1
|
||||
|
||||
for n in kfn.neuronsArray
|
||||
timestep_forward!(n)
|
||||
end
|
||||
for n in kfn.outputNeuronsArray
|
||||
timestep_forward!(n)
|
||||
end
|
||||
|
||||
# pass input_data into input neuron.
|
||||
# number of data point equals to number of input neuron starting from id 1
|
||||
for (i, data) in enumerate(input_data)
|
||||
@@ -75,8 +74,8 @@ function (kfn::kfn_1)(m::model, input_data::AbstractVector)
|
||||
|
||||
kfn.firedNeurons_t0 = [n.z_t for n in kfn.neuronsArray]
|
||||
|
||||
# Threads.@threads for n in kfn.neuronsArray
|
||||
for n in kfn.neuronsArray
|
||||
Threads.@threads for n in kfn.neuronsArray
|
||||
# for n in kfn.neuronsArray
|
||||
n(kfn)
|
||||
end
|
||||
|
||||
@@ -84,19 +83,22 @@ function (kfn::kfn_1)(m::model, input_data::AbstractVector)
|
||||
append!(kfn.firedNeurons, findall(kfn.firedNeurons_t1)) # store id of neuron that fires
|
||||
kfn.firedNeurons |> unique! # use for random new neuron connection
|
||||
|
||||
# Threads.@threads for n in kfn.outputNeuronsArray
|
||||
for n in kfn.outputNeuronsArray
|
||||
Threads.@threads for n in kfn.outputNeuronsArray
|
||||
# for n in kfn.outputNeuronsArray
|
||||
n(kfn)
|
||||
end
|
||||
|
||||
out = [n.z_t1 for n in kfn.outputNeuronsArray]
|
||||
logit = [n.v_t1 for n in kfn.outputNeuronsArray]
|
||||
|
||||
return out::Array{Bool},
|
||||
sum(kfn.firedNeurons_t1),
|
||||
# _predict = Flux.softmax(logit)
|
||||
# predict = findall(isequal.(_predict, maximum(_predict)))[1]
|
||||
|
||||
return sum(kfn.firedNeurons_t1[kfn.kfnParams[:totalInputPort]+1:end])::Int,
|
||||
logit::Array{Float64},
|
||||
[n.v_t1 for n in kfn.outputNeuronsArray],
|
||||
[sum(i.wRec) for i in kfn.outputNeuronsArray],
|
||||
[sum(i.epsilonRec) for i in kfn.outputNeuronsArray],
|
||||
[i.phi for i in kfn.outputNeuronsArray]
|
||||
[sum(i.wRecChange) for i in kfn.outputNeuronsArray]
|
||||
end
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
@@ -128,11 +130,15 @@ function (n::lifNeuron)(kfn::knowledgeFn)
|
||||
|
||||
# decay of v_t1
|
||||
n.v_t1 = n.alpha * n.v_t
|
||||
|
||||
n.phi = 0.0
|
||||
n.decayedEpsilonRec = n.alpha * n.epsilonRec
|
||||
n.epsilonRec = n.decayedEpsilonRec
|
||||
else
|
||||
n.recSignal = sum(n.wRec .* n.z_i_t) # signal from other neuron that this neuron subscribed
|
||||
n.alpha_v_t = n.alpha * n.v_t
|
||||
n.v_t1 = n.alpha_v_t + n.recSignal
|
||||
n.v_t1 = no_negative!(n.v_t1)
|
||||
# n.v_t1 = no_negative!(n.v_t1)
|
||||
|
||||
if n.v_t1 > n.v_th
|
||||
n.z_t1 = true
|
||||
@@ -147,7 +153,7 @@ function (n::lifNeuron)(kfn::knowledgeFn)
|
||||
n.phi = (n.gammaPd / n.v_th) * max(0, 1 - (n.v_t1 - n.v_th) / n.v_th)
|
||||
n.decayedEpsilonRec = n.alpha * n.epsilonRec
|
||||
n.epsilonRec = n.decayedEpsilonRec + n.z_i_t
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
@@ -165,26 +171,30 @@ function (n::alifNeuron)(kfn::knowledgeFn)
|
||||
|
||||
# neuron is in refractory state, skip all calculation
|
||||
n.z_t1 = false # used by timestep_forward() in kfn. Set to zero because neuron spike last only 1 timestep follow by a period of refractory.
|
||||
n.a = (n.rho * n.a) + ((1 - n.rho) * n.z_t)
|
||||
n.a = (n.rho * n.a)
|
||||
n.recSignal = n.recSignal * 0.0
|
||||
|
||||
# decay of v_t1
|
||||
n.v_t1 = n.alpha * n.v_t
|
||||
n.phi = 0
|
||||
|
||||
n.phi = 0.0
|
||||
n.decayedEpsilonRec = n.alpha * n.epsilonRec
|
||||
n.epsilonRec = n.decayedEpsilonRec
|
||||
else
|
||||
n.a = (n.rho * n.a) + ((1 - n.rho) * n.z_t)
|
||||
n.av_th = n.v_th + (n.beta * n.a)
|
||||
n.recSignal = sum(n.wRec .* n.z_i_t) # signal from other neuron that this neuron subscribed
|
||||
n.alpha_v_t = n.alpha * n.v_t
|
||||
n.v_t1 = n.alpha_v_t + n.recSignal
|
||||
n.v_t1 = no_negative!(n.v_t1)
|
||||
# n.v_t1 = no_negative!(n.v_t1)
|
||||
if n.v_t1 > n.av_th
|
||||
n.z_t1 = true
|
||||
n.refractoryCounter = n.refractoryDuration
|
||||
n.firingCounter += 1
|
||||
n.v_t1 = n.vRest
|
||||
n.a = (n.rho * n.a) + 1.0
|
||||
else
|
||||
n.z_t1 = false
|
||||
n.a = (n.rho * n.a)
|
||||
end
|
||||
|
||||
# there is a difference from lif formula
|
||||
@@ -219,12 +229,16 @@ function (n::linearNeuron)(kfn::T) where T<:knowledgeFn
|
||||
# decay of v_t1
|
||||
n.v_t1 = n.alpha * n.v_t
|
||||
n.vError = n.v_t1 # store voltage that will be used to calculate error later
|
||||
|
||||
n.phi = 0.0
|
||||
n.decayedEpsilonRec = n.alpha * n.epsilonRec
|
||||
n.epsilonRec = n.decayedEpsilonRec
|
||||
else
|
||||
recSignal = n.wRec .* n.z_i_t
|
||||
n.recSignal = sum(recSignal) # signal from other neuron that this neuron subscribed
|
||||
n.alpha_v_t = n.alpha * n.v_t
|
||||
n.v_t1 = n.alpha_v_t + n.recSignal
|
||||
n.v_t1 = no_negative!(n.v_t1)
|
||||
# n.v_t1 = no_negative!(n.v_t1)
|
||||
n.vError = n.v_t1 # store voltage that will be used to calculate error later
|
||||
if n.v_t1 > n.v_th
|
||||
n.z_t1 = true
|
||||
@@ -242,6 +256,30 @@ function (n::linearNeuron)(kfn::T) where T<:knowledgeFn
|
||||
end
|
||||
end
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
""" integrateNeuron forward()
|
||||
"""
|
||||
function (n::integrateNeuron)(kfn::knowledgeFn)
|
||||
n.timeStep = kfn.timeStep
|
||||
|
||||
# pulling other neuron's firing status at time t
|
||||
n.z_i_t = getindex(kfn.firedNeurons_t0, n.subscriptionList)
|
||||
n.z_i_t_commulative += n.z_i_t
|
||||
|
||||
n.recSignal = sum(n.wRec .* n.z_i_t) # signal from other neuron that this neuron subscribed
|
||||
n.alpha_v_t = n.alpha * n.v_t
|
||||
if n.recSignal <= 0
|
||||
n.v_t1 = n.alpha_v_t
|
||||
else
|
||||
n.v_t1 = n.alpha_v_t + n.recSignal + n.b
|
||||
end
|
||||
|
||||
# there is a difference from alif formula
|
||||
n.decayedEpsilonRec = n.alpha * n.epsilonRec
|
||||
n.epsilonRec = n.decayedEpsilonRec + n.z_i_t
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
172
src/learn.jl
172
src/learn.jl
@@ -8,15 +8,9 @@ export learn!, compute_wRecChange!, computeModelError
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
|
||||
function computeModelError(modelRespond, correctAnswer; magnitude::Float64=1.0)
|
||||
error = ((correctAnswer .- modelRespond) .* magnitude)
|
||||
|
||||
return error::Vector{Float64}
|
||||
end
|
||||
|
||||
function compute_wRecChange!(m::model, error::Vector{Float64}, correctAnswer::AbstractVector)
|
||||
compute_wRecChange!(m.knowledgeFn[:I], error, correctAnswer)
|
||||
function compute_wRecChange!(m::model, modelError::Float64, outputError::Vector{Float64})
|
||||
# normalize!(modelError)
|
||||
compute_wRecChange!(m.knowledgeFn[:I], modelError, outputError)
|
||||
end
|
||||
|
||||
# function compute_wRecChange!(kfn::kfn_1, errors::Vector{Float64}, correctAnswer::AbstractVector)
|
||||
@@ -47,54 +41,83 @@ end
|
||||
# end
|
||||
|
||||
|
||||
function compute_wRecChange!(kfn::kfn_1, errors::Vector{Float64}, correctAnswer::AbstractVector)
|
||||
for (i, error) in enumerate(errors)
|
||||
if error < 0 # model fires too fast
|
||||
error = error *
|
||||
abs(kfn.outputNeuronsArray[i].v_th - kfn.outputNeuronsArray[i].vError)
|
||||
elseif error == 0 # model answer correctly. maintain membrain potential ≈ 0.5
|
||||
error = error *
|
||||
abs(kfn.outputNeuronsArray[i].v_th/2 - kfn.outputNeuronsArray[i].vError)
|
||||
else # model fires too slow
|
||||
error = error *
|
||||
abs(kfn.outputNeuronsArray[i].v_th - kfn.outputNeuronsArray[i].vError)
|
||||
function compute_wRecChange!(kfn::kfn_1, modelError::Float64, outputError::Vector{Float64})
|
||||
Threads.@threads for n in kfn.neuronsArray
|
||||
# for n in kfn.neuronsArray
|
||||
if typeof(n)<: computeNeuron
|
||||
# wIndex = findall(isequal.(oN.subscriptionList, n.id))
|
||||
wOut = abs.([oN.wRec[findall(isequal.(oN.subscriptionList, n.id))[1]]
|
||||
for oN in kfn.outputNeuronsArray])
|
||||
compute_wRecChange!(n, wOut, modelError)
|
||||
end
|
||||
Threads.@threads for n in kfn.neuronsArray # multithread is not atomic and causing error
|
||||
# for n in kfn.neuronsArray
|
||||
compute_wRecChange!(n, error)
|
||||
end
|
||||
compute_wRecChange!(kfn.outputNeuronsArray[i], error)
|
||||
end
|
||||
|
||||
for oN in kfn.outputNeuronsArray
|
||||
compute_wRecChange!(oN, outputError[oN.id])
|
||||
end
|
||||
end
|
||||
|
||||
function compute_wRecChange!(n::passthroughNeuron, error::Float64)
|
||||
function compute_wRecChange!(n::passthroughNeuron, wOut::AbstractVector, modelError::Vector{Float64})
|
||||
# skip
|
||||
end
|
||||
|
||||
function compute_wRecChange!(n::lifNeuron, error::Float64)
|
||||
function compute_wRecChange!(n::lifNeuron, wOut::AbstractVector, modelError::Float64)
|
||||
# how much error of this neuron 1-spike causing each output neuron's error
|
||||
nError = sum(wOut * modelError)
|
||||
|
||||
n.eRec = n.phi * n.epsilonRec
|
||||
ΔwRecChange = n.eta * error * n.eRec
|
||||
ΔwRecChange = -n.eta * nError * n.eRec
|
||||
# if sum(n.wRec) < 0 # prevent -sum(wRec) that causing neuron NOT fire at all
|
||||
# ΔwRecChange .+= (0.2*(abs(sum(n.wRec)) / length(n.wRec)))
|
||||
# end
|
||||
n.wRecChange .+= ΔwRecChange
|
||||
reset_epsilonRec!(n)
|
||||
# reset_epsilonRec!(n)
|
||||
end
|
||||
|
||||
function compute_wRecChange!(n::alifNeuron, error::Float64)
|
||||
function compute_wRecChange!(n::alifNeuron, wOut::AbstractVector, modelError::Float64)
|
||||
# how much error of this neuron 1-spike causing each output neuron's error
|
||||
# (prejected throug wOut)
|
||||
nError = sum(wOut * modelError)
|
||||
|
||||
n.eRec_v = n.phi * n.epsilonRec
|
||||
n.eRec_a = n.phi * n.beta * n.epsilonRecA
|
||||
n.eRec = n.eRec_v + n.eRec_a
|
||||
ΔwRecChange = n.eta * error * n.eRec
|
||||
ΔwRecChange = -n.eta * nError * n.eRec
|
||||
# if sum(n.wRec) < 0 # prevent -sum(wRec) that causing neuron NOT fire at all
|
||||
# ΔwRecChange .+= (0.2*(abs(sum(n.wRec)) / length(n.wRec)))
|
||||
# end
|
||||
n.wRecChange .+= ΔwRecChange
|
||||
reset_epsilonRec!(n)
|
||||
reset_epsilonRecA!(n)
|
||||
|
||||
# reset_epsilonRec!(n)
|
||||
# reset_epsilonRecA!(n)
|
||||
# n.alphaChange += compute_alphaChange(n.eta, nError)
|
||||
end
|
||||
|
||||
function compute_wRecChange!(n::linearNeuron, error::Float64)
|
||||
n.eRec = n.phi * n.epsilonRec
|
||||
ΔwRecChange = n.eta * error * n.eRec
|
||||
function compute_wRecChange!(n::integrateNeuron, error::Float64)
|
||||
ΔwRecChange = -n.eta * error * n.epsilonRec
|
||||
ΔbChange = -n.eta * error
|
||||
# if sum(n.wRec) < 0 # prevent -sum(wRec) that causing neuron NOT fire at all
|
||||
# ΔwRecChange .+= (abs(sum(n.wRec)) / length(n.wRec))
|
||||
# end
|
||||
n.wRecChange .+= ΔwRecChange
|
||||
reset_epsilonRec!(n)
|
||||
n.bChange += ΔbChange
|
||||
# reset_epsilonRec!(n)
|
||||
# n.alphaChange += compute_alphaChange(n.eta, error)
|
||||
end
|
||||
|
||||
# function compute_wRecChange!(n::linearNeuron, error::Float64)
|
||||
# n.eRec = n.phi * n.epsilonRec
|
||||
# ΔwRecChange = -n.eta * error * n.eRec
|
||||
# # if sum(n.wRec) < 0 # prevent -sum(wRec) that causing neuron NOT fire at all
|
||||
# # ΔwRecChange .+= (abs(sum(n.wRec)) / length(n.wRec))
|
||||
# # end
|
||||
# n.wRecChange .+= ΔwRecChange
|
||||
# # reset_epsilonRec!(n)
|
||||
# end
|
||||
|
||||
# add compute_alphaChange
|
||||
compute_alphaChange(learningRate::Float64, total_wRecChange) = -learningRate * total_wRecChange
|
||||
|
||||
function learn!(m::model)
|
||||
learn!(m.knowledgeFn[:I])
|
||||
end
|
||||
@@ -105,8 +128,8 @@ function learn!(kfn::kfn_1)
|
||||
# compute kfn error for each neuron
|
||||
Threads.@threads for n in kfn.neuronsArray # multithread is not atomic and causing error
|
||||
# for n in kfn.neuronsArray
|
||||
learn!(n, kfn.firedNeurons, kfn.nExInType)
|
||||
end
|
||||
learn!(n, kfn.firedNeurons, kfn.nExInType)
|
||||
end
|
||||
for n in kfn.outputNeuronsArray
|
||||
learn!(n, kfn.firedNeurons, kfn.nExInType, kfn.kfnParams[:totalInputPort])
|
||||
end
|
||||
@@ -124,35 +147,70 @@ end
|
||||
function learn!(n::T, firedNeurons, nExInType) where T<:computeNeuron
|
||||
wSign_0 = sign.(n.wRec) # original sign
|
||||
# n.wRecChange .*= (connStrengthAdjust.(n.synapticStrength))
|
||||
n.wRec += n.wRecChange # merge wRecChange into wRec
|
||||
reset_wRecChange!(n)
|
||||
|
||||
wRecChange_reduceCoeff = 1.0
|
||||
# wRecChange_max = 0.2 * abs(sum(n.wRec)) # max change 20%
|
||||
# y = abs(sum(n.wRecChange))
|
||||
# if y > wRecChange_max # capping weight update
|
||||
# wRecChange_reduceCoeff = wRecChange_max / y
|
||||
# end
|
||||
n.wRec += (wRecChange_reduceCoeff * n.wRecChange)
|
||||
# n.alpha += n.alphaChange
|
||||
|
||||
wSign_1 = sign.(n.wRec) # check for fliped sign, 1 indicates non-fliped sign
|
||||
nonFlipedSign = isequal.(wSign_0, wSign_1) # 1 not fliped, 0 fliped
|
||||
# normalize wRec peak to prevent input signal overwhelming neuron
|
||||
normalizePeak!(n.wRec, n.wRecChange, 2)
|
||||
# if sum(n.wRecChange) != 0
|
||||
# normalizePeak!(n.wRec, n.wRecChange, 2)
|
||||
# end
|
||||
# set weight that fliped sign to 0 for random new connection
|
||||
n.wRec .*= nonFlipedSign
|
||||
capMaxWeight!(n.wRec) # cap maximum weight
|
||||
# capMaxWeight!(n.wRec) # cap maximum weight
|
||||
synapticConnStrength!(n, "updown")
|
||||
neuroplasticity!(n, firedNeurons, nExInType)
|
||||
end
|
||||
|
||||
function learn!(n::T, firedNeurons, nExInType, totalInputPort) where T<:outputNeuron
|
||||
wSign_0 = sign.(n.wRec) # original sign
|
||||
# n.wRecChange .*= (connStrengthAdjust.(n.synapticStrength))
|
||||
n.wRec += n.wRecChange
|
||||
reset_wRecChange!(n)
|
||||
wSign_1 = sign.(n.wRec) # check for fliped sign, 1 indicates non-fliped sign
|
||||
nonFlipedSign = isequal.(wSign_0, wSign_1) # 1 not fliped, 0 fliped
|
||||
# normalize wRec peak to prevent input signal overwhelming neuron
|
||||
normalizePeak!(n.wRec, n.wRecChange, 2)
|
||||
# set weight that fliped sign to 0 for random new connection
|
||||
n.wRec .*= nonFlipedSign
|
||||
capMaxWeight!(n.wRec) # cap maximum weight
|
||||
synapticConnStrength!(n, "updown")
|
||||
neuroplasticity!(n,firedNeurons, nExInType, totalInputPort)
|
||||
function learn!(n::integrateNeuron, firedNeurons, nExInType, totalInputPort)
|
||||
wRecChange_reduceCoeff = 1.0
|
||||
# wRecChange_max = 0.2 * abs(sum(n.wRec)) # max change 20%
|
||||
# y = abs(sum(n.wRecChange))
|
||||
# if y > wRecChange_max # capping weight update
|
||||
# wRecChange_reduceCoeff = wRecChange_max / y
|
||||
# end
|
||||
n.wRec += (wRecChange_reduceCoeff * n.wRecChange)
|
||||
n.b += (wRecChange_reduceCoeff * n.bChange)
|
||||
# n.alpha += n.alphaChange
|
||||
end
|
||||
|
||||
# function learn!(n::linearNeuron, firedNeurons, nExInType, totalInputPort)
|
||||
# wSign_0 = sign.(n.wRec) # original sign
|
||||
# # n.wRecChange .*= (connStrengthAdjust.(n.synapticStrength))
|
||||
# wRecChange_max = 0.1 * abs(sum(n.wRec)) # max change 20%
|
||||
# y = abs(sum(n.wRecChange))
|
||||
# wRecChange_reduceCoeff = 1.0
|
||||
# # if y > wRecChange_max # capping weight update
|
||||
# # wRecChange_reduceCoeff = wRecChange_max / y
|
||||
# # end
|
||||
# n.wRec += (wRecChange_reduceCoeff * n.wRecChange)
|
||||
# n.alpha += n.alphaChange
|
||||
|
||||
# wSign_1 = sign.(n.wRec) # check for fliped sign, 1 indicates non-fliped sign
|
||||
# nonFlipedSign = isequal.(wSign_0, wSign_1) # 1 not fliped, 0 fliped
|
||||
# # normalize wRec peak to prevent input signal overwhelming neuron
|
||||
# if sum(n.wRecChange) != 0
|
||||
# # normalizePeak!(n.wRec, n.wRecChange, 2)
|
||||
# end
|
||||
# # set weight that fliped sign to 0 for random new connection
|
||||
# # n.wRec .*= nonFlipedSign
|
||||
# # capMaxWeight!(n.wRec) # cap maximum weight
|
||||
# # synapticConnStrength!(n, "updown")
|
||||
# # neuroplasticity!(n,firedNeurons, nExInType, totalInputPort)
|
||||
# end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
217
src/snn_utils.jl
217
src/snn_utils.jl
@@ -13,6 +13,7 @@ using GeneralUtils
|
||||
using ..types
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
rng = MersenneTwister(1234)
|
||||
|
||||
function timestep_forward!(x::passthroughNeuron)
|
||||
x.z_t = x.z_t1
|
||||
@@ -30,6 +31,7 @@ precision(x::Array{<:Array}) = ( std(mean.(x)) / mean(mean.(x)) ) * 100
|
||||
reset_last_firing_time!(n::computeNeuron) = n.lastFiringTime = 0.0
|
||||
reset_refractory_state_active!(n::computeNeuron) = n.refractory_state_active = false
|
||||
reset_v_t!(n::neuron) = n.v_t = n.vRest
|
||||
reset_v_t1!(n::neuron) = n.v_t1 = n.vRest
|
||||
reset_z_t!(n::computeNeuron) = n.z_t = false
|
||||
reset_epsilonRec!(n::computeNeuron) = n.epsilonRec = n.epsilonRec * 0.0
|
||||
reset_epsilonRec!(n::outputNeuron) = n.epsilonRec = n.epsilonRec * 0.0
|
||||
@@ -46,66 +48,23 @@ reset_firing_counter!(n::Union{computeNeuron, outputNeuron}) = n.firingCounter =
|
||||
reset_firing_diff!(n::Union{computeNeuron, outputNeuron}) = n.firingDiff = n.firingDiff * 0.0
|
||||
reset_refractoryCounter!(n::Union{computeNeuron, outputNeuron}) = n.refractoryCounter = n.refractoryCounter * 0.0
|
||||
reset_z_i_t_commulative!(n::Union{computeNeuron, outputNeuron}) = n.z_i_t_commulative = n.z_i_t_commulative * 0.0
|
||||
reset_alphaChange!(n::Union{computeNeuron, outputNeuron}) = n.alphaChange = n.alphaChange * 0.0
|
||||
|
||||
# reset function for output neuron
|
||||
reset_epsilon_j!(n::linearNeuron) = n.epsilon_j = n.epsilon_j * 0.0
|
||||
reset_out_t!(n::linearNeuron) = n.out_t = n.out_t * 0.0
|
||||
reset_w_out_change!(n::linearNeuron) = n.w_out_change = n.w_out_change * 0.0
|
||||
reset_b_change!(n::linearNeuron) = n.b_change = n.b_change * 0.0
|
||||
|
||||
|
||||
""" Reset a part of learning-related params that used to collect learning history during learning
|
||||
session
|
||||
"""
|
||||
# function reset_learning_no_wchange!(n::lifNeuron)
|
||||
# reset_epsilonRec!(n)
|
||||
# # reset_v_t!(n)
|
||||
# # reset_z_t!(n)
|
||||
# # reset_reg_voltage_a!(n)
|
||||
# # reset_reg_voltage_b!(n)
|
||||
# # reset_reg_voltage_error!(n)
|
||||
# reset_firing_counter!(n)
|
||||
# reset_firing_diff!(n)
|
||||
# reset_previous_error!(n)
|
||||
# reset_error!(n)
|
||||
|
||||
# # # reset refractory state at the end of episode. Otherwise once neuron goes into refractory state,
|
||||
# # # it will stay in refractory state forever
|
||||
# # reset_refractory_state_active!(n)
|
||||
# end
|
||||
# function reset_learning_no_wchange!(n::Union{alifNeuron, elif_neuron})
|
||||
# reset_epsilonRec!(n)
|
||||
# reset_epsilonRecA!(n)
|
||||
# reset_v_t!(n)
|
||||
# reset_z_t!(n)
|
||||
# # reset_a!(n)
|
||||
# reset_reg_voltage_a!(n)
|
||||
# reset_reg_voltage_b!(n)
|
||||
# reset_reg_voltage_error!(n)
|
||||
# reset_firing_counter!(n)
|
||||
# reset_firing_diff!(n)
|
||||
# reset_previous_error!(n)
|
||||
# reset_error!(n)
|
||||
|
||||
# # reset refractory state at the end of episode. Otherwise once neuron goes into refractory state,
|
||||
# # it will stay in refractory state forever
|
||||
# reset_refractory_state_active!(n)
|
||||
# end
|
||||
# function reset_learning_no_wchange!(n::linearNeuron)
|
||||
# reset_epsilon_j!(n)
|
||||
# reset_out_t!(n)
|
||||
# reset_error!(n)
|
||||
# end
|
||||
reset_bChange!(n::integrateNeuron) = n.bChange = n.bChange * 0.0
|
||||
|
||||
""" Reset all learning-related params at the END of learning session
|
||||
"""
|
||||
function resetLearningParams!(n::lifNeuron)
|
||||
reset_epsilonRec!(n)
|
||||
reset_wRecChange!(n)
|
||||
# reset_v_t!(n)
|
||||
# reset_z_t!(n)
|
||||
reset_v_t!(n)
|
||||
reset_z_t!(n)
|
||||
reset_firing_counter!(n)
|
||||
reset_firing_diff!(n)
|
||||
reset_alphaChange!(n)
|
||||
|
||||
# reset refractory state at the start/end of episode. Otherwise once neuron goes into
|
||||
# refractory state, it will stay in refractory state forever
|
||||
@@ -116,11 +75,12 @@ function resetLearningParams!(n::alifNeuron)
|
||||
reset_epsilonRec!(n)
|
||||
reset_epsilonRecA!(n)
|
||||
reset_wRecChange!(n)
|
||||
# reset_v_t!(n)
|
||||
# reset_z_t!(n)
|
||||
# reset_a!(n)
|
||||
reset_v_t!(n)
|
||||
reset_z_t!(n)
|
||||
reset_a!(n)
|
||||
reset_firing_counter!(n)
|
||||
reset_firing_diff!(n)
|
||||
reset_alphaChange!(n)
|
||||
|
||||
# reset refractory state at the start/end of episode. Otherwise once neuron goes into
|
||||
# refractory state, it will stay in refractory state forever
|
||||
@@ -135,21 +95,31 @@ function resetLearningParams!(n::passthroughNeuron)
|
||||
# skip
|
||||
end
|
||||
|
||||
function resetLearningParams!(n::linearNeuron)
|
||||
# function resetLearningParams!(n::linearNeuron)
|
||||
# reset_epsilonRec!(n)
|
||||
# reset_wRecChange!(n)
|
||||
# # reset_v_t!(n)
|
||||
# reset_firing_counter!(n)
|
||||
|
||||
# # reset refractory state at the start/end of episode. Otherwise once neuron goes into
|
||||
# # refractory state, it will stay in refractory state forever
|
||||
# # reset_refractoryCounter!(n)
|
||||
# reset_z_i_t_commulative!(n)
|
||||
# end
|
||||
|
||||
function resetLearningParams!(n::integrateNeuron)
|
||||
reset_epsilonRec!(n)
|
||||
reset_wRecChange!(n)
|
||||
# reset_v_t!(n)
|
||||
reset_bChange!(n)
|
||||
reset_v_t!(n)
|
||||
reset_firing_counter!(n)
|
||||
|
||||
# reset refractory state at the start/end of episode. Otherwise once neuron goes into
|
||||
# refractory state, it will stay in refractory state forever
|
||||
# reset_refractoryCounter!(n)
|
||||
reset_z_i_t_commulative!(n)
|
||||
reset_alphaChange!(n)
|
||||
end
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
function store_knowledgefn_error!(kfn::knowledgeFn)
|
||||
# condition to adjust nueron in KFN plane in addition to weight adjustment inside each neuron
|
||||
# condition to adjust neuron in KFN plane in addition to weight adjustment inside each neuron
|
||||
if kfn.learningStage == "start_learning"
|
||||
if kfn.recent_knowledgeFn_error === nothing && kfn.knowledgeFn_error === nothing
|
||||
kfn.recent_knowledgeFn_error = [[]]
|
||||
@@ -279,13 +249,13 @@ function synapticConnStrength(currentStrength::Float64, updown::String)
|
||||
|
||||
if updown == "up"
|
||||
if currentStrength > 4 # strong connection
|
||||
updatedStrength = currentStrength + (Δstrength * 1.0)
|
||||
updatedStrength = currentStrength + (Δstrength * 0.2)
|
||||
else
|
||||
updatedStrength = currentStrength + (Δstrength * 0.1)
|
||||
end
|
||||
elseif updown == "down"
|
||||
if currentStrength > 4
|
||||
updatedStrength = currentStrength - (Δstrength * 0.5)
|
||||
updatedStrength = currentStrength - (Δstrength * 0.1)
|
||||
else
|
||||
updatedStrength = currentStrength - (Δstrength * 0.2)
|
||||
end
|
||||
@@ -358,81 +328,80 @@ function neuroplasticity!(n::computeNeuron, firedNeurons::Vector,
|
||||
nExInTypeList::Vector)
|
||||
# if there is 0-weight then replace it with new connection
|
||||
zeroWeightConnIndex = findall(iszero.(n.wRec)) # connection that has 0 weight
|
||||
if length(zeroWeightConnIndex) != 0
|
||||
# new synaptic connection must sample fron neuron that fires
|
||||
nFiredPool = filter(x -> x ∉ [n.id], firedNeurons) # exclude this neuron id from the id list
|
||||
filter!(x -> x ∉ n.subscriptionList, nFiredPool) # exclude this neuron's subscriptionList from the list
|
||||
|
||||
# new synaptic connection must sample fron neuron that fires
|
||||
nFiredPool = filter(x -> x ∉ [n.id], firedNeurons) # exclude this neuron id from the id list
|
||||
filter!(x -> x ∉ n.subscriptionList, nFiredPool) # exclude this neuron's subscriptionList from the list
|
||||
nNonFiredPool = setdiff!([1:length(nExInTypeList)...], nFiredPool)
|
||||
|
||||
nNonFiredPool = setdiff!([1:length(nExInTypeList)...], nFiredPool)
|
||||
filter!(x -> x ∉ [n.id], nNonFiredPool) # exclude this neuron id from the id list
|
||||
filter!(x -> x ∉ n.subscriptionList, nNonFiredPool) # exclude this neuron's subscriptionList from the list
|
||||
filter!(x -> x ∉ [n.id], nNonFiredPool) # exclude this neuron id from the id list
|
||||
filter!(x -> x ∉ n.subscriptionList, nNonFiredPool) # exclude this neuron's subscriptionList from the list
|
||||
|
||||
w = rand(0.01:0.01:0.2, length(zeroWeightConnIndex))
|
||||
synapticStrength = rand(-5:0.01:-4, length(zeroWeightConnIndex))
|
||||
w = randn(length(zeroWeightConnIndex)) / 100
|
||||
synapticStrength = rand(-4.5:0.1:-3.5, length(zeroWeightConnIndex))
|
||||
|
||||
shuffle!(nFiredPool)
|
||||
shuffle!(nNonFiredPool)
|
||||
shuffle!(nFiredPool)
|
||||
shuffle!(nNonFiredPool)
|
||||
|
||||
# add new synaptic connection to neuron
|
||||
for (i, connIndex) in enumerate(zeroWeightConnIndex)
|
||||
if length(nFiredPool) != 0
|
||||
newConn = popfirst!(nFiredPool)
|
||||
else
|
||||
newConn = popfirst!(nNonFiredPool)
|
||||
end
|
||||
|
||||
""" conn that is being replaced has to go into nNonFiredPool so nNonFiredPool isn't empty
|
||||
"""
|
||||
push!(nNonFiredPool, n.subscriptionList[connIndex])
|
||||
n.subscriptionList[connIndex] = newConn
|
||||
n.wRec[connIndex] = w[i] * nExInTypeList[newConn]
|
||||
n.synapticStrength[connIndex] = synapticStrength[i]
|
||||
end
|
||||
end
|
||||
|
||||
function neuroplasticity!(n::outputNeuron, firedNeurons::Vector,
|
||||
nExInTypeList::Vector, totalInputNeuron::Integer)
|
||||
# if there is 0-weight then replace it with new connection
|
||||
zeroWeightConnIndex = findall(iszero.(n.wRec)) # connection that has 0 weight
|
||||
|
||||
# new synaptic connection must sample fron neuron that fires
|
||||
nFiredPool = filter(x -> x ∉ [n.id], firedNeurons) # exclude this neuron id from the id list
|
||||
filter!(x -> x ∉ n.subscriptionList, nFiredPool) # exclude this neuron's subscriptionList from the list
|
||||
filter!(x -> x ∉ [1:totalInputNeuron...], nFiredPool) # exclude input neuron
|
||||
|
||||
nNonFiredPool = setdiff!([1:length(nExInTypeList)...], nFiredPool)
|
||||
filter!(x -> x ∉ [n.id], nNonFiredPool) # exclude this neuron id from the id list
|
||||
filter!(x -> x ∉ n.subscriptionList, nNonFiredPool) # exclude this neuron's subscriptionList from the list
|
||||
filter!(x -> x ∉ [1:totalInputNeuron...], nNonFiredPool) # exclude input neuron
|
||||
|
||||
w = rand(0.01:0.01:0.2, length(zeroWeightConnIndex))
|
||||
synapticStrength = rand(-5:0.01:-4, length(zeroWeightConnIndex))
|
||||
|
||||
shuffle!(nFiredPool)
|
||||
shuffle!(nNonFiredPool)
|
||||
|
||||
# add new synaptic connection to neuron
|
||||
for (i, connIndex) in enumerate(zeroWeightConnIndex)
|
||||
newConn::Int64 = 0
|
||||
if length(nFiredPool) != 0
|
||||
newConn = popfirst!(nFiredPool)
|
||||
elseif length(nNonFiredPool) != 0
|
||||
newConn = popfirst!(nNonFiredPool)
|
||||
else
|
||||
# skip
|
||||
end
|
||||
|
||||
if newConn != 0
|
||||
""" conn that is being replaced has to go into nNonFiredPool so nNonFiredPool isn't empty
|
||||
"""
|
||||
# add new synaptic connection to neuron
|
||||
for (i, connIndex) in enumerate(zeroWeightConnIndex)
|
||||
""" conn that is being replaced has to go into nNonFiredPool so
|
||||
nNonFiredPool isn't empty """
|
||||
push!(nNonFiredPool, n.subscriptionList[connIndex])
|
||||
|
||||
if length(nFiredPool) != 0
|
||||
newConn = popfirst!(nFiredPool)
|
||||
else
|
||||
newConn = popfirst!(nNonFiredPool)
|
||||
end
|
||||
n.subscriptionList[connIndex] = newConn
|
||||
n.wRec[connIndex] = w[i] * nExInTypeList[newConn]
|
||||
n.wRec[connIndex] = abs(w[i]) * nExInTypeList[newConn]
|
||||
n.synapticStrength[connIndex] = synapticStrength[i]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# function neuroplasticity!(n::outputNeuron, firedNeurons::Vector,
|
||||
# nExInTypeList::Vector, totalInputNeuron::Integer)
|
||||
# # if there is 0-weight then replace it with new connection
|
||||
# zeroWeightConnIndex = findall(iszero.(n.wRec)) # connection that has 0 weight
|
||||
# if length(zeroWeightConnIndex) != 0
|
||||
# # new synaptic connection must sample fron neuron that fires
|
||||
# nFiredPool = filter(x -> x ∉ [n.id], firedNeurons) # exclude this neuron id from the id list
|
||||
# filter!(x -> x ∉ n.subscriptionList, nFiredPool) # exclude this neuron's subscriptionList from the list
|
||||
# filter!(x -> x ∉ [1:totalInputNeuron...], nFiredPool) # exclude input neuron
|
||||
|
||||
# nNonFiredPool = setdiff!([1:length(nExInTypeList)...], nFiredPool)
|
||||
# unique!(append!(nNonFiredPool, zeroWeightConnIndex))
|
||||
# filter!(x -> x ∉ [n.id], nNonFiredPool) # exclude this neuron id from the id list
|
||||
# filter!(x -> x ∉ n.subscriptionList, nNonFiredPool) # exclude this neuron's subscriptionList from the list
|
||||
# filter!(x -> x ∉ [1:totalInputNeuron...], nNonFiredPool) # exclude input neuron
|
||||
|
||||
# w = randn(length(zeroWeightConnIndex)) / 100
|
||||
# synapticStrength = rand(-4.5:0.1:-3.5, length(zeroWeightConnIndex))
|
||||
|
||||
# shuffle!(nFiredPool)
|
||||
# shuffle!(nNonFiredPool)
|
||||
|
||||
# # add new synaptic connection to neuron
|
||||
# for (i, connIndex) in enumerate(zeroWeightConnIndex)
|
||||
# """ conn that is being replaced has to go into nNonFiredPool so
|
||||
# nNonFiredPool isn't empty """
|
||||
# push!(nNonFiredPool, n.subscriptionList[connIndex])
|
||||
|
||||
# if length(nFiredPool) != 0
|
||||
# newConn = popfirst!(nFiredPool)
|
||||
# else
|
||||
# newConn = popfirst!(nNonFiredPool)
|
||||
# end
|
||||
# n.subscriptionList[connIndex] = newConn
|
||||
# n.wRec[connIndex] = w[i] * nExInTypeList[newConn]
|
||||
# n.synapticStrength[connIndex] = synapticStrength[i]
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
|
||||
""" Cap maximum weight of each neuron connection
|
||||
"""
|
||||
function capMaxWeight!(v::Vector{Float64}, max=1.0)
|
||||
|
||||
153
src/types.jl
153
src/types.jl
@@ -4,6 +4,7 @@ export
|
||||
# struct
|
||||
IronpenStruct, model, knowledgeFn, lifNeuron, alifNeuron, linearNeuron,
|
||||
kfn_1, inputNeuron, computeNeuron, neuron, outputNeuron, passthroughNeuron,
|
||||
integrateNeuron,
|
||||
|
||||
# function
|
||||
instantiate_custom_types, init_neuron, populate_neuron,
|
||||
@@ -22,6 +23,8 @@ abstract type computeNeuron <: neuron end
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
rng = MersenneTwister(1234)
|
||||
|
||||
""" Model struct
|
||||
"""
|
||||
Base.@kwdef mutable struct model <: Ironpen
|
||||
@@ -262,16 +265,16 @@ function kfn_1(kfnParams::Dict)
|
||||
end
|
||||
end
|
||||
|
||||
# add ExInType into each output neuron subExInType
|
||||
for n in kfn.outputNeuronsArray
|
||||
try # input neuron doest have n.subscriptionList
|
||||
for (i, sub_id) in enumerate(n.subscriptionList)
|
||||
n_ExInType = kfn.neuronsArray[sub_id].ExInType
|
||||
n.wRec[i] *= n_ExInType
|
||||
end
|
||||
catch
|
||||
end
|
||||
end
|
||||
# # add ExInType into each output neuron subExInType
|
||||
# for n in kfn.outputNeuronsArray
|
||||
# try # input neuron doest have n.subscriptionList
|
||||
# for (i, sub_id) in enumerate(n.subscriptionList)
|
||||
# n_ExInType = kfn.neuronsArray[sub_id].ExInType
|
||||
# n.wRec[i] *= n_ExInType
|
||||
# end
|
||||
# catch
|
||||
# end
|
||||
# end
|
||||
|
||||
for n in kfn.neuronsArray
|
||||
push!(kfn.nExInType, n.ExInType)
|
||||
@@ -339,6 +342,7 @@ Base.@kwdef mutable struct lifNeuron <: computeNeuron
|
||||
|
||||
gammaPd::Float64 = 0.3 # γ_pd, discount factor, value from paper
|
||||
alpha::Float64 = 0.0 # α, neuron membrane potential decay factor
|
||||
alphaChange::Float64 = 0.0
|
||||
phi::Float64 = 0.0 # ϕ, psuedo derivative
|
||||
epsilonRec::Array{Float64} = Float64[] # ϵ_rec, eligibility vector for neuron spike
|
||||
decayedEpsilonRec::Array{Float64} = Float64[] # α * epsilonRec
|
||||
@@ -347,7 +351,7 @@ Base.@kwdef mutable struct lifNeuron <: computeNeuron
|
||||
refractoryDuration::Int64 = 3 # neuron's refratory period in millisecond
|
||||
refractoryCounter::Int64 = 0
|
||||
tau_m::Float64 = 100.0 # τ_m, membrane time constant in millisecond
|
||||
eta::Float64 = 0.01 # η, learning rate
|
||||
eta::Float64 = 1e-3 # η, learning rate
|
||||
wRecChange::Array{Float64} = Float64[] # Δw_rec, cumulated wRec change
|
||||
recSignal::Float64 = 0.0 # incoming recurrent signal
|
||||
alpha_v_t::Float64 = 0.0 # alpha * v_t
|
||||
@@ -428,6 +432,7 @@ Base.@kwdef mutable struct alifNeuron <: computeNeuron
|
||||
synapticStrengthLimit::NamedTuple = (lowerlimit=(-5=>0), upperlimit=(5=>5))
|
||||
|
||||
alpha::Float64 = 0.0 # α, neuron membrane potential decay factor
|
||||
alphaChange::Float64 = 0.0
|
||||
delta::Float64 = 1.0 # δ, discreate timestep size in millisecond
|
||||
epsilonRec::Array{Float64} = Float64[] # ϵ_rec(v), eligibility vector for neuron i spike
|
||||
epsilonRecA::Array{Float64} = Float64[] # ϵ_rec(a)
|
||||
@@ -435,7 +440,7 @@ Base.@kwdef mutable struct alifNeuron <: computeNeuron
|
||||
eRec_v::Array{Float64} = Float64[] # a component of neuron's eligibility trace resulted from v_t
|
||||
eRec_a::Array{Float64} = Float64[] # a component of neuron's eligibility trace resulted from av_th
|
||||
eRec::Array{Float64} = Float64[] # neuron's eligibility trace
|
||||
eta::Float64 = 0.01 # eta, learning rate
|
||||
eta::Float64 = 1e-3 # eta, learning rate
|
||||
gammaPd::Float64 = 0.3 # γ_pd, discount factor, value from paper
|
||||
phi::Float64 = 0.0 # ϕ, psuedo derivative
|
||||
refractoryDuration::Int64 = 3 # neuron's refractory period in millisecond
|
||||
@@ -510,7 +515,7 @@ end
|
||||
""" linearNeuron struct
|
||||
"""
|
||||
Base.@kwdef mutable struct linearNeuron <: outputNeuron
|
||||
id::Float64 = 0.0 # ID of this neuron which is it position in knowledgeFn array
|
||||
id::Int64 = 0 # ID of this neuron which is it position in knowledgeFn array
|
||||
type::String = "linearNeuron"
|
||||
knowledgeFnName::String = "not defined" # knowledgeFn that this neuron belongs to
|
||||
subscriptionList::Array{Int64} = Int64[] # list of other neuron that this neuron synapse subscribed to
|
||||
@@ -545,7 +550,7 @@ Base.@kwdef mutable struct linearNeuron <: outputNeuron
|
||||
refractoryDuration::Int64 = 3 # neuron's refratory period in millisecond
|
||||
refractoryCounter::Int64 = 0
|
||||
tau_out::Float64 = 50.0 # τ_out, membrane time constant in millisecond
|
||||
eta::Float64 = 0.01 # η, learning rate
|
||||
eta::Float64 = 1e-3 # η, learning rate
|
||||
wRecChange::Array{Float64} = Float64[] # Δw_rec, cumulated wRec change
|
||||
recSignal::Float64 = 0.0 # incoming recurrent signal
|
||||
alpha_v_t::Float64 = 0.0 # alpha * v_t
|
||||
@@ -584,6 +589,87 @@ function linearNeuron(params::Dict)
|
||||
return n
|
||||
end
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
""" integrateNeuron struct
|
||||
"""
|
||||
Base.@kwdef mutable struct integrateNeuron <: outputNeuron
|
||||
id::Int64 = 0 # ID of this neuron which is it position in knowledgeFn array
|
||||
type::String = "integrateNeuron"
|
||||
knowledgeFnName::String = "not defined" # knowledgeFn that this neuron belongs to
|
||||
subscriptionList::Array{Int64} = Int64[] # list of other neuron that this neuron synapse subscribed to
|
||||
timeStep::Int64 = 0 # current time
|
||||
wRec::Array{Float64} = Float64[] # synaptic weight (for receiving signal from other neuron)
|
||||
v_t::Float64 = randn() # vᵗ, postsynaptic neuron membrane potential of previous timestep
|
||||
v_t1::Float64 = 0.0 # vᵗ⁺¹, postsynaptic neuron membrane potential at current timestep
|
||||
v_th::Float64 = 1.0 # vᵗʰ, neuron firing threshold
|
||||
vRest::Float64 = 0.0 # resting potential after neuron fired
|
||||
vError::Float64 = 0.0 # used to compute model error
|
||||
z_t::Bool = false # zᵗ, neuron postsynaptic firing of previous timestep
|
||||
# zᵗ⁺¹, neuron firing status at time = t+1. I need this because the way I calculate all
|
||||
# neurons forward function at each timestep-by-timestep is to do every neuron
|
||||
# forward calculation. Each neuron requires access to other neuron's firing status
|
||||
# during v_t1 calculation hence I need a variable to hold z_t1 so that I'm not replacing z_t
|
||||
z_t1::Bool = false # neuron postsynaptic firing at current timestep (after neuron's calculation)
|
||||
b::Float64 = 0.0
|
||||
bChange::Float64 = 0.0
|
||||
|
||||
# neuron presynaptic firing at current timestep (which is other neuron postsynaptic firing of
|
||||
# previous timestep)
|
||||
z_i_t::Array{Bool} = Bool[]
|
||||
z_i_t_commulative::Array{Int64} = Int64[] # used to compute connection strength
|
||||
synapticStrength::Array{Float64} = Float64[]
|
||||
synapticStrengthLimit::NamedTuple = (lowerlimit=(-5=>-5), upperlimit=(5=>5))
|
||||
|
||||
gammaPd::Float64 = 0.3 # γ_pd, discount factor, value from paper
|
||||
alpha::Float64 = 0.0 # α, neuron membrane potential decay factor
|
||||
alphaChange::Float64 = 0.0
|
||||
phi::Float64 = 0.0 # ϕ, psuedo derivative
|
||||
epsilonRec::Array{Float64} = Float64[] # ϵ_rec, eligibility vector for neuron spike
|
||||
decayedEpsilonRec::Array{Float64} = Float64[] # α * epsilonRec
|
||||
eRec::Array{Float64} = Float64[] # eligibility trace for neuron spike
|
||||
delta::Float64 = 1.0 # δ, discreate timestep size in millisecond
|
||||
refractoryDuration::Int64 = 3 # neuron's refratory period in millisecond
|
||||
refractoryCounter::Int64 = 0
|
||||
tau_out::Float64 = 50.0 # τ_out, membrane time constant in millisecond
|
||||
eta::Float64 = 1e-3 # η, learning rate
|
||||
wRecChange::Array{Float64} = Float64[] # Δw_rec, cumulated wRec change
|
||||
recSignal::Float64 = 0.0 # incoming recurrent signal
|
||||
alpha_v_t::Float64 = 0.0 # alpha * v_t
|
||||
|
||||
firingCounter::Int64 = 0 # store how many times neuron fires
|
||||
ExInSignalSum::Float64 = 0.0
|
||||
end
|
||||
|
||||
""" linear neuron outer constructor
|
||||
|
||||
# Example
|
||||
|
||||
linear_neuron_params = Dict(
|
||||
:type => "linearNeuron",
|
||||
:k => 0.9, # output leakink coefficient
|
||||
:tau_out => 5.0, # output time constant in millisecond. It should equals to time use for 1 sequence
|
||||
:out => 0.0, # neuron's output value store here
|
||||
)
|
||||
|
||||
neuron1 = linearNeuron(linear_neuron_params)
|
||||
"""
|
||||
function integrateNeuron(params::Dict)
|
||||
n = integrateNeuron()
|
||||
field_names = fieldnames(typeof(n))
|
||||
for i in field_names
|
||||
if i in keys(params)
|
||||
if i == :optimiser
|
||||
opt_type = string(split(params[i], ".")[end])
|
||||
n.:($i) = load_optimiser(opt_type)
|
||||
else
|
||||
n.:($i) = params[i] # assign params to n struct fields
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return n
|
||||
end
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
# function load_optimiser(optimiser_name::String; params::Union{Dict,Nothing} = nothing)
|
||||
@@ -634,10 +720,11 @@ function init_neuron!(id::Int64, n::lifNeuron, n_params::Dict, kfnParams::Dict)
|
||||
|
||||
# prevent subscription to itself by removing this neuron id
|
||||
filter!(x -> x != n.id, n.subscriptionList)
|
||||
n.synapticStrength = rand(-5:0.01:-4, length(n.subscriptionList))
|
||||
n.synapticStrength = rand(-4.5:0.01:-4, length(n.subscriptionList))
|
||||
|
||||
n.epsilonRec = zeros(length(n.subscriptionList))
|
||||
n.wRec = randn(length(n.subscriptionList))
|
||||
# n.wRec = randn(length(n.subscriptionList))
|
||||
n.wRec = randn(rng, length(n.subscriptionList)) / 100
|
||||
n.wRecChange = zeros(length(n.subscriptionList))
|
||||
n.alpha = calculate_α(n)
|
||||
n.z_i_t_commulative = zeros(length(n.subscriptionList))
|
||||
@@ -654,10 +741,10 @@ function init_neuron!(id::Int64, n::alifNeuron, n_params::Dict,
|
||||
|
||||
# prevent subscription to itself by removing this neuron id
|
||||
filter!(x -> x != n.id, n.subscriptionList)
|
||||
n.synapticStrength = rand(-5:0.01:-4, length(n.subscriptionList))
|
||||
n.synapticStrength = rand(-4.5:0.01:-4, length(n.subscriptionList))
|
||||
|
||||
n.epsilonRec = zeros(length(n.subscriptionList))
|
||||
n.wRec = randn(length(n.subscriptionList))
|
||||
n.wRec = randn(rng, length(n.subscriptionList)) / 100 # TODO use abs()
|
||||
n.wRecChange = zeros(length(n.subscriptionList))
|
||||
|
||||
# the more time has passed from the last time neuron was activated, the more
|
||||
@@ -668,8 +755,7 @@ function init_neuron!(id::Int64, n::alifNeuron, n_params::Dict,
|
||||
n.z_i_t_commulative = zeros(length(n.subscriptionList))
|
||||
end
|
||||
|
||||
|
||||
function init_neuron!(id::Int64, n::linearNeuron, n_params::Dict, kfnParams::Dict)
|
||||
function init_neuron!(id::Int64, n::integrateNeuron, n_params::Dict, kfnParams::Dict)
|
||||
n.id = id
|
||||
n.knowledgeFnName = kfnParams[:knowledgeFnName]
|
||||
|
||||
@@ -677,15 +763,33 @@ function init_neuron!(id::Int64, n::linearNeuron, n_params::Dict, kfnParams::Dic
|
||||
subscription_numbers = Int(floor((n_params[:synapticConnectionPercent] / 100.0) *
|
||||
kfnParams[:totalNeurons] - kfnParams[:totalInputPort]))
|
||||
n.subscriptionList = [pop!(subscription_options) for i = 1:subscription_numbers]
|
||||
n.synapticStrength = rand(-5:0.01:-4, length(n.subscriptionList))
|
||||
n.synapticStrength = rand(-4.5:0.01:-4, length(n.subscriptionList))
|
||||
|
||||
n.epsilonRec = zeros(length(n.subscriptionList))
|
||||
n.wRec = randn(length(n.subscriptionList))
|
||||
n.wRec = randn(rng, length(n.subscriptionList)) / 100
|
||||
n.wRecChange = zeros(length(n.subscriptionList))
|
||||
n.alpha = calculate_k(n)
|
||||
n.z_i_t_commulative = zeros(length(n.subscriptionList))
|
||||
n.b = randn(rng) / 100
|
||||
end
|
||||
|
||||
# function init_neuron!(id::Int64, n::linearNeuron, n_params::Dict, kfnParams::Dict)
|
||||
# n.id = id
|
||||
# n.knowledgeFnName = kfnParams[:knowledgeFnName]
|
||||
|
||||
# subscription_options = shuffle!([kfnParams[:totalInputPort]+1 : kfnParams[:totalNeurons]...])
|
||||
# subscription_numbers = Int(floor((n_params[:synapticConnectionPercent] / 100.0) *
|
||||
# kfnParams[:totalNeurons] - kfnParams[:totalInputPort]))
|
||||
# n.subscriptionList = [pop!(subscription_options) for i = 1:subscription_numbers]
|
||||
# n.synapticStrength = rand(-4.5:0.01:-4, length(n.subscriptionList))
|
||||
|
||||
# n.epsilonRec = zeros(length(n.subscriptionList))
|
||||
# n.wRec = randn(rng, length(n.subscriptionList)) / 100
|
||||
# n.wRecChange = zeros(length(n.subscriptionList))
|
||||
# n.alpha = calculate_k(n)
|
||||
# n.z_i_t_commulative = zeros(length(n.subscriptionList))
|
||||
# end
|
||||
|
||||
""" Make a neuron intended for use with knowledgeFn
|
||||
"""
|
||||
function init_neuron(id::Int64, n_params::Dict, kfnParams::Dict)
|
||||
@@ -715,7 +819,9 @@ function instantiate_custom_types(params::Union{Dict,Nothing} = nothing)
|
||||
elseif type == "alifNeuron"
|
||||
return alifNeuron(params)
|
||||
elseif type == "linearNeuron"
|
||||
return linearNeuron(params)
|
||||
return linearNeuron(params)
|
||||
elseif type == "integrateNeuron"
|
||||
return integrateNeuron(params)
|
||||
else
|
||||
return nothing
|
||||
end
|
||||
@@ -740,6 +846,7 @@ calculate_α(neuron::lifNeuron) = exp(-neuron.delta / neuron.tau_m)
|
||||
calculate_α(neuron::alifNeuron) = exp(-neuron.delta / neuron.tau_m)
|
||||
calculate_ρ(neuron::alifNeuron) = exp(-neuron.delta / neuron.tau_a)
|
||||
calculate_k(neuron::linearNeuron) = exp(-neuron.delta / neuron.tau_out)
|
||||
calculate_k(neuron::integrateNeuron) = exp(-neuron.delta / neuron.tau_out)
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
|
||||
Reference in New Issue
Block a user