From 56ec3757c9c1ca54d8a910fcc7958edf0be4b1c4 Mon Sep 17 00:00:00 2001 From: ton Date: Sun, 6 Aug 2023 06:02:23 +0700 Subject: [PATCH] minor fix --- src/forward.jl | 9 --- src/learn.jl | 170 +++++++++++++++++++++++++++++-------------------- 2 files changed, 100 insertions(+), 79 deletions(-) diff --git a/src/forward.jl b/src/forward.jl index 63dbdc1..7d1c451 100644 --- a/src/forward.jl +++ b/src/forward.jl @@ -21,15 +21,6 @@ function (kfn::kfn_1)(input::AbstractArray) # kfn.learningStage = [2] end - # println(">>> input ", size(input)) - # println(">>> lif_zit ", size(kfn.lif_zit)) - # println(">>> lif_recSignal ", size(kfn.lif_recSignal)) - # println(">>> lif_wRec ", size(kfn.lif_wRec)) - # println(">>> lif_refractoryCounter ", size(kfn.lif_refractoryCounter)) - # println(">>> lif_alpha ", size(kfn.lif_alpha)) - # println(">>> lif_vt0 ", size(kfn.lif_vt0)) - # println(">>> lif_vt0 sum ", sum(kfn.lif_vt0)) - # update activation matrix with "lif_zt1" and "alif_zt1" by concatenating # (input, lif_zt1, alif_zt1) to form activation matrix _zit = cat(reshape(input, (size(input, 1), size(input, 2), 1, size(input, 3))), diff --git a/src/learn.jl b/src/learn.jl index c836d71..4b68796 100644 --- a/src/learn.jl +++ b/src/learn.jl @@ -42,6 +42,72 @@ function compute_paramsChange!(kfn::kfn_1, modelError, outputError) # error("DEBUG -> kfn compute_paramsChange! $(Dates.now())") end + + +function lifComputeParamsChange!( phi::CuArray, + epsilonRec::CuArray, + eta::CuArray, + eRec::CuArray, + wRec::CuArray, + wRecChange::CuArray, + wOut::CuArray, + arrayProjection4d::CuArray, + nError::CuArray, + modelError::CuArray) + wOutSum = sum(wOut, dims=3) .* arrayProjection4d + + # nError a.k.a. learning signal use dopamine concept, + # this neuron receive summed error signal (modelError) + nError .= (modelError .* arrayProjection4d) .* wOutSum + eRec .= phi .* epsilonRec + # GeneralUtils.isNotEqual(wRec, 0) is a subscribe filter use to filter out non-subscribed wRecChange + wRecChange .+= ((-1 .* eta) .* nError .* eRec) .* GeneralUtils.isNotEqual.(wRec, 0) + # error("DEBUG -> lifComputeParamsChange! $(Dates.now())") +end + +function alifComputeParamsChange!( phi::CuArray, + epsilonRec::CuArray, + eta::CuArray, + eRec::CuArray, + wRec::CuArray, + wRecChange::CuArray, + wOut::CuArray, + arrayProjection4d::CuArray, + nError::CuArray, + modelError::CuArray, + beta::CuArray) + + wOutSum = sum(wOut, dims=3) .* arrayProjection4d + + # nError a.k.a. learning signal use dopamine concept, + # this neuron receive summed error signal (modelError) + nError .= (modelError .* arrayProjection4d) .* wOutSum + eRec .= (phi .* epsilonRec) .+ (phi .* epsilonRec .* beta) + + # GeneralUtils.isNotEqual(wRec, 0) is a subscribe filter use to filter out non-subscribed wRecChange + wRecChange .+= ((-1 .* eta) .* nError .* eRec) .* GeneralUtils.isNotEqual.(wRec, 0) + # error("DEBUG -> alifComputeParamsChange! $(Dates.now())") +end + +function onComputeParamsChange!(phi::CuArray, + epsilonRec::CuArray, + eta::CuArray, + eRec::CuArray, + wOut::CuArray, + wOutChange::CuArray, + outputError::CuArray # outputError is output neuron's error + ) + + # nError a.k.a. learning signal use dopamine concept, + # this neuron receive summed error signal (modelError) + eRec .= (phi .* epsilonRec) .* reshape(outputError, (1, 1, :, size(epsilonRec, 4))) + + # GeneralUtils.isNotEqual(wRec, 0) is a subscribe filter use to filter out non-subscribed wRecChange + wOutChange .+= ((-1 .* eta) .* eRec) .* GeneralUtils.isNotEqual.(wOut, 0) + + # error("DEBUG -> onComputeParamsChange! $(Dates.now())") +end + function lifComputeParamsChange!( phi::AbstractArray, epsilonRec::AbstractArray, eta::AbstractArray, @@ -71,27 +137,6 @@ function lifComputeParamsChange!( phi::AbstractArray, end end -function lifComputeParamsChange!( phi::CuArray, - epsilonRec::CuArray, - eta::CuArray, - eRec::CuArray, - wRec::CuArray, - wRecChange::CuArray, - wOut::CuArray, - arrayProjection4d::CuArray, - nError::CuArray, - modelError::CuArray) - wOutSum = sum(wOut, dims=3) .* arrayProjection4d - - # nError a.k.a. learning signal use dopamine concept, - # this neuron receive summed error signal (modelError) - nError .= (modelError .* arrayProjection4d) .* wOutSum - eRec .= phi .* epsilonRec - # GeneralUtils.isNotEqual(wRec, 0) is a subscribe filter use to filter out non-subscribed wRecChange - wRecChange .+= ((-1 .* eta) .* nError .* eRec) .* GeneralUtils.isNotEqual.(wRec, 0) - # error("DEBUG -> lifComputeParamsChange! $(Dates.now())") -end - function alifComputeParamsChange!( phi::AbstractArray, epsilonRec::AbstractArray, epsilonRecA::AbstractArray, @@ -129,30 +174,6 @@ function alifComputeParamsChange!( phi::AbstractArray, end end -function alifComputeParamsChange!( phi::CuArray, - epsilonRec::CuArray, - eta::CuArray, - eRec::CuArray, - wRec::CuArray, - wRecChange::CuArray, - wOut::CuArray, - arrayProjection4d::CuArray, - nError::CuArray, - modelError::CuArray, - beta::CuArray) - - wOutSum = sum(wOut, dims=3) .* arrayProjection4d - - # nError a.k.a. learning signal use dopamine concept, - # this neuron receive summed error signal (modelError) - nError .= (modelError .* arrayProjection4d) .* wOutSum - eRec .= (phi .* epsilonRec) .+ (phi .* epsilonRec .* beta) - - # GeneralUtils.isNotEqual(wRec, 0) is a subscribe filter use to filter out non-subscribed wRecChange - wRecChange .+= ((-1 .* eta) .* nError .* eRec) .* GeneralUtils.isNotEqual.(wRec, 0) - # error("DEBUG -> alifComputeParamsChange! $(Dates.now())") -end - function onComputeParamsChange!(phi::AbstractArray, epsilonRec::AbstractArray, eta::AbstractArray, @@ -173,42 +194,27 @@ function onComputeParamsChange!(phi::AbstractArray, end end -function onComputeParamsChange!(phi::CuArray, - epsilonRec::CuArray, - eta::CuArray, - eRec::CuArray, - wOut::CuArray, - wOutChange::CuArray, - outputError::CuArray # outputError is output neuron's error - ) - - # nError a.k.a. learning signal use dopamine concept, - # this neuron receive summed error signal (modelError) - eRec .= (phi .* epsilonRec) .* reshape(outputError, (1, 1, :, size(epsilonRec, 4))) - - # GeneralUtils.isNotEqual(wRec, 0) is a subscribe filter use to filter out non-subscribed wRecChange - wOutChange .+= ((-1 .* eta) .* eRec) .* GeneralUtils.isNotEqual.(wOut, 0) - - # error("DEBUG -> onComputeParamsChange! $(Dates.now())") -end - function learn!(kfn::kfn_1) - #WORKING lif learn + # lif learn lifLearn!(kfn.lif_wRec, kfn.lif_wRecChange) - error("DEBUG -> kfn learn! $(Dates.now())") - #TODO alif learn + # alif learn + alifLearn!(kfn.alif_wRec, + kfn.alif_wRecChange) + # on learn + onLearn!(kfn.on_wOut, + kfn.on_wOutChange) - #TODO on learn - - #TODO wOut decay + # wOut decay + kfn.on_wOut .*= 0.0001 # wrap up learning session if kfn.learningStage == [3] kfn.learningStage = [0] end + # error("DEBUG -> kfn learn! $(Dates.now())") end function lifLearn!(wRec, @@ -222,7 +228,31 @@ function lifLearn!(wRec, end +function alifLearn!(wRec, + wRecChange) + # merge learning weight + wRec .+= wRecChange + #TODO synaptic strength + + #TODO neuroplasticity + +end + +function onLearn!(wOut, + wOutChange) + # merge learning weight + wOut .+= wOutChange + + #TODO synaptic strength + + #TODO neuroplasticity + +end + +#TODO voltage regulator + +#TODO frequency regulator