From abd69c9dae2ff9623bb9e25a67685349d18e54d5 Mon Sep 17 00:00:00 2001 From: tonaerospace Date: Wed, 17 May 2023 13:20:03 +0700 Subject: [PATCH] add normalizePeak! --- src/learn.jl | 3 +++ src/snn_utils.jl | 15 +++++++++++++-- src/types.jl | 6 +++--- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/learn.jl b/src/learn.jl index e7fe6fb..35fa74d 100644 --- a/src/learn.jl +++ b/src/learn.jl @@ -86,6 +86,9 @@ function learn!(kfn::kfn_1, correctAnswer::AbstractVector) nonFlipedSign = isequal.(n.subExInType, wSign) # 1 not fliped, 0 fliped LinearAlgebra.normalize!(n.wRec, 1) n.wRec .*= nonFlipedSign # set weight that fliped sign to 0 for random new connection + # normalize wRec peak to prevent input signal overwhelming neuron + normalizePeak!(n.wRec, 2) + synapticConnStrength!(n) #TODO neuroplasticity diff --git a/src/snn_utils.jl b/src/snn_utils.jl index 5ac5c41..23757dd 100644 --- a/src/snn_utils.jl +++ b/src/snn_utils.jl @@ -4,7 +4,7 @@ using Flux.Optimise: apply! export calculate_α, calculate_ρ, calculate_k, timestep_forward!, init_neuron, no_negative!, precision, calculate_w_change!, store_knowledgefn_error!, interneurons_adjustment!, reset_z_t!, resetLearningParams!, reset_learning_history_params!, reset_epsilonRec!, - reset_epsilonRecA!, synapticConnStrength!, + reset_epsilonRecA!, synapticConnStrength!, normalizePeak!, firing_rate_error!, firing_rate_regulator!, update_Bn!, cal_firing_reg!, neuroplasticity!, shakeup!, reset_learning_no_wchange!, adjust_internal_learning_rate!, gradient_withloss @@ -317,7 +317,18 @@ function neuroplasticity!(n::compute_neuron, firedNeurons::Vector) end end - +""" normalize a part of a vector centering at a vector's maximum value along with nearby value + within its radius. radius must be odd number +""" +function normalizePeak!(v::Vector, radius::Integer=2) + peak = findall(isequal.(n.wRec, maximum(n.wRec)))[1] + upindex = peak - radius + upindex = upindex < 1 ? 1 : upindex + downindex = peak + radius + downindex = downindex > length(v) ? length(v) : downindex + subvector = view(v, upindex:downindex) + normalize!(subvector, 1) +end diff --git a/src/types.jl b/src/types.jl index 473e7a9..98f9d19 100644 --- a/src/types.jl +++ b/src/types.jl @@ -630,7 +630,7 @@ function init_neuron!(id::Int64, n::lif_neuron, n_params::Dict, kfnParams::Dict) n.synapticStrength = rand(-5:0.1:-3, length(n.subscriptionList)) n.epsilonRec = zeros(length(n.subscriptionList)) - n.wRec = LinearAlgebra.normalize!(rand(length(n.subscriptionList)), 1) + n.wRec = rand(length(n.subscriptionList)) n.wRecChange = zeros(length(n.subscriptionList)) n.alpha = calculate_α(n) end @@ -649,7 +649,7 @@ function init_neuron!(id::Int64, n::alif_neuron, n_params::Dict, n.synapticStrength = rand(-5:0.1:-3, length(n.subscriptionList)) n.epsilonRec = zeros(length(n.subscriptionList)) - n.wRec = LinearAlgebra.normalize!(rand(length(n.subscriptionList)), 1) + n.wRec = rand(length(n.subscriptionList)) n.wRecChange = zeros(length(n.subscriptionList)) # the more time has passed from the last time neuron was activated, the more @@ -671,7 +671,7 @@ function init_neuron!(id::Int64, n::linear_neuron, n_params::Dict, kfnParams::Di n.synapticStrength = rand(-5:0.1:-3, length(n.subscriptionList)) n.epsilonRec = zeros(length(n.subscriptionList)) - n.wRec = LinearAlgebra.normalize!(rand(length(n.subscriptionList)), 1) + n.wRec = rand(length(n.subscriptionList)) n.wRecChange = zeros(length(n.subscriptionList)) n.alpha = calculate_k(n) end