working milestone with 25% accuracy

This commit is contained in:
2023-06-20 09:16:34 +07:00
parent 08297ccd00
commit 57efefc8e3
6 changed files with 1280 additions and 262 deletions

View File

@@ -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