refractoring

This commit is contained in:
2023-05-17 15:08:14 +07:00
parent ab3caad413
commit 95cad151dc
4 changed files with 104 additions and 104 deletions

View File

@@ -99,18 +99,18 @@ end
#------------------------------------------------------------------------------------------------100 #------------------------------------------------------------------------------------------------100
""" passthrough_neuron forward() """ passthroughNeuron forward()
""" """
function (n::passthrough_neuron)(kfn::knowledgeFn) function (n::passthroughNeuron)(kfn::knowledgeFn)
n.timeStep = kfn.timeStep n.timeStep = kfn.timeStep
# n.global_tick = kfn.global_tick # n.global_tick = kfn.global_tick
end end
#------------------------------------------------------------------------------------------------100 #------------------------------------------------------------------------------------------------100
""" lif_neuron forward() """ lifNeuron forward()
""" """
function (n::lif_neuron)(kfn::knowledgeFn) function (n::lifNeuron)(kfn::knowledgeFn)
n.timeStep = kfn.timeStep n.timeStep = kfn.timeStep
# pulling other neuron's firing status at time t # pulling other neuron's firing status at time t
@@ -151,9 +151,9 @@ end
#------------------------------------------------------------------------------------------------100 #------------------------------------------------------------------------------------------------100
""" alif_neuron forward() """ alifNeuron forward()
""" """
function (n::alif_neuron)(kfn::knowledgeFn) function (n::alifNeuron)(kfn::knowledgeFn)
n.timeStep = kfn.timeStep n.timeStep = kfn.timeStep
n.z_i_t = getindex(kfn.firedNeurons_t0, n.subscriptionList) n.z_i_t = getindex(kfn.firedNeurons_t0, n.subscriptionList)
@@ -197,10 +197,10 @@ end
#------------------------------------------------------------------------------------------------100 #------------------------------------------------------------------------------------------------100
""" linear_neuron forward() """ linearNeuron forward()
In this implementation, each output neuron is fully connected to every lif and alif neuron. In this implementation, each output neuron is fully connected to every lif and alif neuron.
""" """
function (n::linear_neuron)(kfn::T) where T<:knowledgeFn function (n::linearNeuron)(kfn::T) where T<:knowledgeFn
n.timeStep = kfn.timeStep n.timeStep = kfn.timeStep
# pulling other neuron's firing status at time t # pulling other neuron's firing status at time t

View File

@@ -82,15 +82,15 @@ function learn!(kfn::kfn_1, correctAnswer::AbstractVector)
end end
end end
""" passthrough_neuron learn() """ passthroughNeuron learn()
""" """
function learn!(n::passthrough_neuron, kfn::knowledgeFn) function learn!(n::passthroughNeuron, kfn::knowledgeFn)
# skip # skip
end end
""" lif learn() """ lif learn()
""" """
function learn!(n::lif_neuron, error::Number) function learn!(n::lifNeuron, error::Number)
n.eRec = n.phi * n.epsilonRec n.eRec = n.phi * n.epsilonRec
ΔwRecChange = n.eta * error * n.eRec ΔwRecChange = n.eta * error * n.eRec
@@ -98,9 +98,9 @@ function learn!(n::lif_neuron, error::Number)
reset_epsilonRec!(n) reset_epsilonRec!(n)
end end
""" alif_neuron learn() """ alifNeuron learn()
""" """
function learn!(n::alif_neuron, error::Number) function learn!(n::alifNeuron, error::Number)
n.eRec_v = n.phi * n.epsilonRec n.eRec_v = n.phi * n.epsilonRec
n.eRec_a = -n.phi * n.beta * n.epsilonRecA n.eRec_a = -n.phi * n.beta * n.epsilonRecA
n.eRec = n.eRec_v + n.eRec_a n.eRec = n.eRec_v + n.eRec_a
@@ -111,9 +111,9 @@ function learn!(n::alif_neuron, error::Number)
reset_epsilonRecA!(n) reset_epsilonRecA!(n)
end end
""" linear_neuron learn() """ linearNeuron learn()
""" """
function learn!(n::linear_neuron, error::Number) function learn!(n::linearNeuron, error::Number)
n.eRec = n.phi * n.epsilonRec n.eRec = n.phi * n.epsilonRec
ΔwRecChange = n.eta * error * n.eRec ΔwRecChange = n.eta * error * n.eRec

View File

@@ -15,7 +15,7 @@ using ..types
#------------------------------------------------------------------------------------------------100 #------------------------------------------------------------------------------------------------100
function timestep_forward!(x::passthrough_neuron) function timestep_forward!(x::passthroughNeuron)
x.z_t = x.z_t1 x.z_t = x.z_t1
end end
@@ -34,30 +34,30 @@ reset_v_t!(n::neuron) = n.v_t = n.vRest
reset_z_t!(n::computeNeuron) = n.z_t = false reset_z_t!(n::computeNeuron) = n.z_t = false
reset_epsilonRec!(n::computeNeuron) = n.epsilonRec = n.epsilonRec * 0.0 reset_epsilonRec!(n::computeNeuron) = n.epsilonRec = n.epsilonRec * 0.0
reset_epsilonRec!(n::outputNeuron) = n.epsilonRec = n.epsilonRec * 0.0 reset_epsilonRec!(n::outputNeuron) = n.epsilonRec = n.epsilonRec * 0.0
reset_epsilonRecA!(n::alif_neuron) = n.epsilonRecA = n.epsilonRecA * 0.0 reset_epsilonRecA!(n::alifNeuron) = n.epsilonRecA = n.epsilonRecA * 0.0
reset_epsilon_in!(n::computeNeuron) = n.epsilon_in = isnothing(n.epsilon_in) ? nothing : n.epsilon_in * 0.0 reset_epsilon_in!(n::computeNeuron) = n.epsilon_in = isnothing(n.epsilon_in) ? nothing : n.epsilon_in * 0.0
reset_error!(n::Union{computeNeuron, linear_neuron}) = n.error = nothing reset_error!(n::Union{computeNeuron, linearNeuron}) = n.error = nothing
reset_w_in_change!(n::computeNeuron) = n.w_in_change = isnothing(n.w_in_change) ? nothing : n.w_in_change * 0.0 reset_w_in_change!(n::computeNeuron) = n.w_in_change = isnothing(n.w_in_change) ? nothing : n.w_in_change * 0.0
reset_wRecChange!(n::Union{computeNeuron, outputNeuron}) = n.wRecChange = n.wRecChange * 0.0 reset_wRecChange!(n::Union{computeNeuron, outputNeuron}) = n.wRecChange = n.wRecChange * 0.0
reset_a!(n::alif_neuron) = n.a = n.a * 0.0 reset_a!(n::alifNeuron) = n.a = n.a * 0.0
reset_reg_voltage_a!(n::computeNeuron) = n.reg_voltage_a = n.reg_voltage_a * 0.0 reset_reg_voltage_a!(n::computeNeuron) = n.reg_voltage_a = n.reg_voltage_a * 0.0
reset_reg_voltage_b!(n::computeNeuron) = n.reg_voltage_b = n.reg_voltage_b * 0.0 reset_reg_voltage_b!(n::computeNeuron) = n.reg_voltage_b = n.reg_voltage_b * 0.0
reset_reg_voltage_error!(n::computeNeuron) = n.reg_voltage_error = n.reg_voltage_error * 0.0 reset_reg_voltage_error!(n::computeNeuron) = n.reg_voltage_error = n.reg_voltage_error * 0.0
reset_firing_counter!(n::computeNeuron) = n.firingCounter = n.firingCounter * 0.0 reset_firing_counter!(n::Union{computeNeuron, outputNeuron}) = n.firingCounter = n.firingCounter * 0.0
reset_firing_diff!(n::Union{computeNeuron, linear_neuron}) = n.firingDiff = n.firingDiff * 0.0 reset_firing_diff!(n::Union{computeNeuron, linearNeuron}) = n.firingDiff = n.firingDiff * 0.0
reset_refractoryCounter!(n::Union{computeNeuron, linear_neuron}) = n.refractoryCounter = n.refractoryCounter * 0.0 reset_refractoryCounter!(n::Union{computeNeuron, outputNeuron}) = n.refractoryCounter = n.refractoryCounter * 0.0
# reset function for output neuron # reset function for output neuron
reset_epsilon_j!(n::linear_neuron) = n.epsilon_j = n.epsilon_j * 0.0 reset_epsilon_j!(n::linearNeuron) = n.epsilon_j = n.epsilon_j * 0.0
reset_out_t!(n::linear_neuron) = n.out_t = n.out_t * 0.0 reset_out_t!(n::linearNeuron) = n.out_t = n.out_t * 0.0
reset_w_out_change!(n::linear_neuron) = n.w_out_change = n.w_out_change * 0.0 reset_w_out_change!(n::linearNeuron) = n.w_out_change = n.w_out_change * 0.0
reset_b_change!(n::linear_neuron) = n.b_change = n.b_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 """ Reset a part of learning-related params that used to collect learning history during learning
session session
""" """
# function reset_learning_no_wchange!(n::lif_neuron) # function reset_learning_no_wchange!(n::lifNeuron)
# reset_epsilonRec!(n) # reset_epsilonRec!(n)
# # reset_v_t!(n) # # reset_v_t!(n)
# # reset_z_t!(n) # # reset_z_t!(n)
@@ -73,7 +73,7 @@ reset_b_change!(n::linear_neuron) = n.b_change = n.b_change * 0.0
# # # it will stay in refractory state forever # # # it will stay in refractory state forever
# # reset_refractory_state_active!(n) # # reset_refractory_state_active!(n)
# end # end
# function reset_learning_no_wchange!(n::Union{alif_neuron, elif_neuron}) # function reset_learning_no_wchange!(n::Union{alifNeuron, elif_neuron})
# reset_epsilonRec!(n) # reset_epsilonRec!(n)
# reset_epsilonRecA!(n) # reset_epsilonRecA!(n)
# reset_v_t!(n) # reset_v_t!(n)
@@ -91,7 +91,7 @@ reset_b_change!(n::linear_neuron) = n.b_change = n.b_change * 0.0
# # it will stay in refractory state forever # # it will stay in refractory state forever
# reset_refractory_state_active!(n) # reset_refractory_state_active!(n)
# end # end
# function reset_learning_no_wchange!(n::linear_neuron) # function reset_learning_no_wchange!(n::linearNeuron)
# reset_epsilon_j!(n) # reset_epsilon_j!(n)
# reset_out_t!(n) # reset_out_t!(n)
# reset_error!(n) # reset_error!(n)
@@ -99,7 +99,7 @@ reset_b_change!(n::linear_neuron) = n.b_change = n.b_change * 0.0
""" Reset all learning-related params at the END of learning session """ Reset all learning-related params at the END of learning session
""" """
function resetLearningParams!(n::lif_neuron) function resetLearningParams!(n::lifNeuron)
reset_epsilonRec!(n) reset_epsilonRec!(n)
reset_wRecChange!(n) reset_wRecChange!(n)
# reset_v_t!(n) # reset_v_t!(n)
@@ -111,7 +111,7 @@ function resetLearningParams!(n::lif_neuron)
# refractory state, it will stay in refractory state forever # refractory state, it will stay in refractory state forever
reset_refractoryCounter!(n) reset_refractoryCounter!(n)
end end
function resetLearningParams!(n::alif_neuron) function resetLearningParams!(n::alifNeuron)
reset_epsilonRec!(n) reset_epsilonRec!(n)
reset_epsilonRecA!(n) reset_epsilonRecA!(n)
reset_wRecChange!(n) reset_wRecChange!(n)
@@ -126,14 +126,14 @@ function resetLearningParams!(n::alif_neuron)
reset_refractoryCounter!(n) reset_refractoryCounter!(n)
end end
# function reset_learning_no_wchange!(n::passthrough_neuron) # function reset_learning_no_wchange!(n::passthroughNeuron)
# end # end
function resetLearningParams!(n::passthrough_neuron) function resetLearningParams!(n::passthroughNeuron)
# skip # skip
end end
function resetLearningParams!(n::linear_neuron) function resetLearningParams!(n::linearNeuron)
reset_epsilonRec!(n) reset_epsilonRec!(n)
reset_wRecChange!(n) reset_wRecChange!(n)
reset_v_t!(n) reset_v_t!(n)
@@ -196,7 +196,7 @@ end
""" Regulates membrane potential to stay under v_th, output is weight change """ Regulates membrane potential to stay under v_th, output is weight change
""" """
function cal_v_reg!(n::lif_neuron) function cal_v_reg!(n::lifNeuron)
# retified linear function # retified linear function
component_a1 = n.v_t1 - n.v_th < 0 ? 0 : (n.v_t1 - n.v_th)^2 component_a1 = n.v_t1 - n.v_th < 0 ? 0 : (n.v_t1 - n.v_th)^2
component_a2 = -n.v_t1 - n.v_th < 0 ? 0 : (-n.v_t1 - n.v_th)^2 component_a2 = -n.v_t1 - n.v_th < 0 ? 0 : (-n.v_t1 - n.v_th)^2
@@ -207,7 +207,7 @@ function cal_v_reg!(n::lif_neuron)
n.reg_voltage_b = n.reg_voltage_b + (component_b * n.epsilonRec) n.reg_voltage_b = n.reg_voltage_b + (component_b * n.epsilonRec)
end end
function cal_v_reg!(n::alif_neuron) function cal_v_reg!(n::alifNeuron)
# retified linear function # retified linear function
component_a1 = n.v_t1 - n.av_th < 0 ? 0 : (n.v_t1 - n.av_th)^2 component_a1 = n.v_t1 - n.av_th < 0 ? 0 : (n.v_t1 - n.av_th)^2
component_a2 = -n.v_t1 - n.av_th < 0 ? 0 : (-n.v_t1 - n.av_th)^2 component_a2 = -n.v_t1 - n.av_th < 0 ? 0 : (-n.v_t1 - n.av_th)^2

View File

@@ -2,8 +2,8 @@ module types
export export
# struct # struct
IronpenStruct, model, knowledgeFn, lif_neuron, alif_neuron, linear_neuron, IronpenStruct, model, knowledgeFn, lifNeuron, alifNeuron, linearNeuron,
kfn_1, inputNeuron, computeNeuron, neuron, outputNeuron, passthrough_neuron, kfn_1, inputNeuron, computeNeuron, neuron, outputNeuron, passthroughNeuron,
# function # function
instantiate_custom_types, init_neuron, populate_neuron, instantiate_custom_types, init_neuron, populate_neuron,
@@ -42,7 +42,7 @@ end
# Example # Example
I_kfnparams = Dict( I_kfnparams = Dict(
:type => "lif_neuron", :type => "lifNeuron",
:v_t1 => 0.0, # neuron membrane potential at time = t+1 :v_t1 => 0.0, # neuron membrane potential at time = t+1
:v_th => 2.0, # neuron firing threshold (this value is treated as maximum bound if I use auto generate) :v_th => 2.0, # neuron firing threshold (this value is treated as maximum bound if I use auto generate)
:z_t => false, # neuron firing status at time = t :z_t => false, # neuron firing status at time = t
@@ -120,7 +120,7 @@ end
# Example # Example
lif_neuron_params = Dict( lif_neuron_params = Dict(
:type => "lif_neuron", :type => "lifNeuron",
:v_th => 1.2, # neuron firing threshold (this value is treated as maximum bound if I use auto generate) :v_th => 1.2, # neuron firing threshold (this value is treated as maximum bound if I use auto generate)
:z_t => false, # neuron firing status at time = t :z_t => false, # neuron firing status at time = t
:gammaPd => 0.3, # discount factor. The value is from the paper :gammaPd => 0.3, # discount factor. The value is from the paper
@@ -130,7 +130,7 @@ end
) )
alif_neuron_params = Dict( alif_neuron_params = Dict(
:type => "alif_neuron", :type => "alifNeuron",
:v_th => 1.2, # neuron firing threshold (this value is treated as maximum bound if I use auto generate) :v_th => 1.2, # neuron firing threshold (this value is treated as maximum bound if I use auto generate)
:z_t => false, # neuron firing status at time = t :z_t => false, # neuron firing status at time = t
:gammaPd => 0.3, # discount factor. The value is from the paper :gammaPd => 0.3, # discount factor. The value is from the paper
@@ -146,7 +146,7 @@ end
) )
linear_neuron_params = Dict( linear_neuron_params = Dict(
:type => "linear_neuron", :type => "linearNeuron",
:k => 0.9, # output leakink coefficient :k => 0.9, # output leakink coefficient
:tau_out => 5.0, # output time constant in millisecond. It should equals to time use for 1 sequence :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 :out => 0.0, # neuron's output value store here
@@ -166,7 +166,7 @@ end
:neuron_w_rec_generation_pattern => "random", :neuron_w_rec_generation_pattern => "random",
:neuron_v_t_default => 0.5, :neuron_v_t_default => 0.5,
:neuron_voltage_drop_percentage => "100%", :neuron_voltage_drop_percentage => "100%",
:neuron_firing_rate_target => 50.0, :neuronFiringRateTarget => 50.0,
:neuron_learning_rate => 0.01, :neuron_learning_rate => 0.01,
:neuron_c_reg => 0.0001, :neuron_c_reg => 0.0001,
:neuron_c_reg_v => 0.0001, :neuron_c_reg_v => 0.0001,
@@ -182,23 +182,23 @@ function kfn_1(kfnParams::Dict)
kfn.kfnParams = kfnParams kfn.kfnParams = kfnParams
kfn.knowledgeFnName = kfn.kfnParams[:knowledgeFnName] kfn.knowledgeFnName = kfn.kfnParams[:knowledgeFnName]
if kfn.kfnParams[:compute_neuron_number] < kfn.kfnParams[:total_input_port] if kfn.kfnParams[:computeNeuronNumber] < kfn.kfnParams[:totalInputPort]
throw(error("number of compute neuron must be greater than input neuron")) throw(error("number of compute neuron must be greater than input neuron"))
end end
# Bn # Bn
if kfn.kfnParams[:Bn] == "random" if kfn.kfnParams[:Bn] == "random"
kfn.Bn = [Random.rand(0:0.001:1) for i in 1:kfn.kfnParams[:compute_neuron_number]] kfn.Bn = [Random.rand(0:0.001:1) for i in 1:kfn.kfnParams[:computeNeuronNumber]]
else # in case I want to specify manually else # in case I want to specify manually
kfn.Bn = [kfn.kfnParams[:Bn] for i in 1:kfn.kfnParams[:compute_neuron_number]] kfn.Bn = [kfn.kfnParams[:Bn] for i in 1:kfn.kfnParams[:computeNeuronNumber]]
end end
# assign neurons ID by their position in kfn.neurons array because I think it is # assign neurons ID by their position in kfn.neurons array because I think it is
# straight forward way # straight forward way
# add input port # add input port
for (k, v) in kfn.kfnParams[:input_port] for (k, v) in kfn.kfnParams[:inputPort]
current_type = kfn.kfnParams[:input_port][k] current_type = kfn.kfnParams[:inputPort][k]
for i = 1:current_type[:numbers] for i = 1:current_type[:numbers]
n_id = length(kfn.neuronsArray) + 1 n_id = length(kfn.neuronsArray) + 1
neuron = init_neuron(n_id, current_type[:params], kfn.kfnParams) neuron = init_neuron(n_id, current_type[:params], kfn.kfnParams)
@@ -216,22 +216,22 @@ function kfn_1(kfnParams::Dict)
end end
end end
for i = 1:kfn.kfnParams[:output_port][:numbers] for i = 1:kfn.kfnParams[:outputPort][:numbers]
neuron = init_neuron(i, kfn.kfnParams[:output_port][:params], neuron = init_neuron(i, kfn.kfnParams[:outputPort][:params],
kfn.kfnParams) kfn.kfnParams)
push!(kfn.outputNeuronsArray, neuron) push!(kfn.outputNeuronsArray, neuron)
end end
for n in kfn.neuronsArray for n in kfn.neuronsArray
if typeof(n) <: computeNeuron if typeof(n) <: computeNeuron
n.firingRateTarget = kfn.kfnParams[:neuron_firing_rate_target] n.firingRateTarget = kfn.kfnParams[:neuronFiringRateTarget]
end end
end end
# excitatory neuron to inhabitory neuron = 60:40 % of computeNeuron # excitatory neuron to inhabitory neuron = 60:40 % of computeNeuron
ex_number = Int(floor(0.6 * kfn.kfnParams[:compute_neuron_number])) ex_number = Int(floor(0.6 * kfn.kfnParams[:computeNeuronNumber]))
ex_n = [1 for i in 1:ex_number] ex_n = [1 for i in 1:ex_number]
in_number = kfn.kfnParams[:compute_neuron_number] - ex_number in_number = kfn.kfnParams[:computeNeuronNumber] - ex_number
in_n = [-1 for i in 1:in_number] in_n = [-1 for i in 1:in_number]
ex_in = shuffle!([ex_n; in_n]) ex_in = shuffle!([ex_n; in_n])
@@ -268,11 +268,11 @@ end
#------------------------------------------------------------------------------------------------100 #------------------------------------------------------------------------------------------------100
""" passthrough_neuron struct """ passthroughNeuron struct
""" """
Base.@kwdef mutable struct passthrough_neuron <: inputNeuron Base.@kwdef mutable struct passthroughNeuron <: inputNeuron
id::Union{Int64,Nothing} = nothing # ID of this neuron which is it position in knowledgeFn array id::Union{Int64,Nothing} = nothing # ID of this neuron which is it position in knowledgeFn array
type::String = "passthrough_neuron" type::String = "passthroughNeuron"
knowledgeFnName::Union{String,Nothing} = nothing # knowledgeFn that this neuron belongs to knowledgeFnName::Union{String,Nothing} = nothing # knowledgeFn that this neuron belongs to
z_t::Bool = false z_t::Bool = false
z_t1::Bool = false z_t1::Bool = false
@@ -280,8 +280,8 @@ Base.@kwdef mutable struct passthrough_neuron <: inputNeuron
ExInType::Integer = 1 # 1 excitatory, -1 inhabitory. input neuron is always excitatory ExInType::Integer = 1 # 1 excitatory, -1 inhabitory. input neuron is always excitatory
end end
function passthrough_neuron(params::Dict) function passthroughNeuron(params::Dict)
n = passthrough_neuron() n = passthroughNeuron()
field_names = fieldnames(typeof(n)) field_names = fieldnames(typeof(n))
for i in field_names for i in field_names
if i in keys(params) if i in keys(params)
@@ -298,11 +298,11 @@ end
#------------------------------------------------------------------------------------------------100 #------------------------------------------------------------------------------------------------100
""" lif_neuron struct """ lifNeuron struct
""" """
Base.@kwdef mutable struct lif_neuron <: computeNeuron Base.@kwdef mutable struct lifNeuron <: computeNeuron
id::Union{Int64,Nothing} = nothing # this neuron ID i.e. position of this neuron in knowledgeFn id::Union{Int64,Nothing} = nothing # this neuron ID i.e. position of this neuron in knowledgeFn
type::String = "lif_neuron" type::String = "lifNeuron"
ExInType::Integer = 1 # 1 excitatory, -1 inhabitory ExInType::Integer = 1 # 1 excitatory, -1 inhabitory
# Bn::Union{Float64,Nothing} = Random.rand() # Bias for neuron error # Bn::Union{Float64,Nothing} = Random.rand() # Bias for neuron error
knowledgeFnName::Union{String,Nothing} = nothing # knowledgeFn that this neuron belongs to knowledgeFnName::Union{String,Nothing} = nothing # knowledgeFn that this neuron belongs to
@@ -361,7 +361,7 @@ end
# Example # Example
lif_neuron_params = Dict( lif_neuron_params = Dict(
:type => "lif_neuron", :type => "lifNeuron",
:v_th => 1.2, # neuron firing threshold (this value is treated as maximum bound if I use auto generate) :v_th => 1.2, # neuron firing threshold (this value is treated as maximum bound if I use auto generate)
:z_t => false, # neuron firing status at time = t :z_t => false, # neuron firing status at time = t
:gammaPd => 0.3, # discount factor. The value is from the paper :gammaPd => 0.3, # discount factor. The value is from the paper
@@ -370,10 +370,10 @@ end
:tau_m => 5.0, # membrane time constant in millisecond. It should equals to time use for 1 sequence :tau_m => 5.0, # membrane time constant in millisecond. It should equals to time use for 1 sequence
) )
neuron1 = lif_neuron(lif_neuron_params) neuron1 = lifNeuron(lif_neuron_params)
""" """
function lif_neuron(params::Dict) function lifNeuron(params::Dict)
n = lif_neuron() n = lifNeuron()
field_names = fieldnames(typeof(n)) field_names = fieldnames(typeof(n))
for i in field_names for i in field_names
if i in keys(params) if i in keys(params)
@@ -390,11 +390,11 @@ end
#------------------------------------------------------------------------------------------------100 #------------------------------------------------------------------------------------------------100
""" alif_neuron struct """ alifNeuron struct
""" """
Base.@kwdef mutable struct alif_neuron <: computeNeuron Base.@kwdef mutable struct alifNeuron <: computeNeuron
id::Union{Int64,Nothing} = nothing # this neuron ID i.e. position of this neuron in knowledgeFn id::Union{Int64,Nothing} = nothing # this neuron ID i.e. position of this neuron in knowledgeFn
type::String = "alif_neuron" type::String = "alifNeuron"
ExInType::Integer = -1 # 1 excitatory, -1 inhabitory ExInType::Integer = -1 # 1 excitatory, -1 inhabitory
# Bn::Union{Float64,Nothing} = Random.rand() # Bias for neuron error # Bn::Union{Float64,Nothing} = Random.rand() # Bias for neuron error
knowledgeFnName::Union{String,Nothing} = nothing # knowledgeFn that this neuron belongs to knowledgeFnName::Union{String,Nothing} = nothing # knowledgeFn that this neuron belongs to
@@ -462,7 +462,7 @@ end
# Example # Example
alif_neuron_params = Dict( alif_neuron_params = Dict(
:type => "alif_neuron", :type => "alifNeuron",
:v_th => 1.2, # neuron firing threshold (this value is treated as maximum bound if I :v_th => 1.2, # neuron firing threshold (this value is treated as maximum bound if I
use auto generate) use auto generate)
:z_t => false, # neuron firing status at time = t :z_t => false, # neuron firing status at time = t
@@ -479,10 +479,10 @@ end
:a => 0.0, :a => 0.0,
) )
neuron1 = alif_neuron(alif_neuron_params) neuron1 = alifNeuron(alif_neuron_params)
""" """
function alif_neuron(params::Dict) function alifNeuron(params::Dict)
n = alif_neuron() n = alifNeuron()
field_names = fieldnames(typeof(n)) field_names = fieldnames(typeof(n))
for i in field_names for i in field_names
if i in keys(params) if i in keys(params)
@@ -498,11 +498,11 @@ function alif_neuron(params::Dict)
end end
#------------------------------------------------------------------------------------------------100 #------------------------------------------------------------------------------------------------100
""" linear_neuron struct """ linearNeuron struct
""" """
Base.@kwdef mutable struct linear_neuron <: outputNeuron Base.@kwdef mutable struct linearNeuron <: outputNeuron
id::Union{Int64,Nothing} = nothing # ID of this neuron which is it position in knowledgeFn array id::Union{Int64,Nothing} = nothing # ID of this neuron which is it position in knowledgeFn array
type::String = "linear_neuron" type::String = "linearNeuron"
knowledgeFnName::Union{String,Nothing} = nothing # knowledgeFn that this neuron belongs to knowledgeFnName::Union{String,Nothing} = nothing # knowledgeFn that this neuron belongs to
subscriptionList::Union{Array{Int64},Nothing} = nothing # list of other neuron that this neuron synapse subscribed to subscriptionList::Union{Array{Int64},Nothing} = nothing # list of other neuron that this neuron synapse subscribed to
timeStep::Union{Number,Nothing} = nothing # current time timeStep::Union{Number,Nothing} = nothing # current time
@@ -549,16 +549,16 @@ end
# Example # Example
linear_neuron_params = Dict( linear_neuron_params = Dict(
:type => "linear_neuron", :type => "linearNeuron",
:k => 0.9, # output leakink coefficient :k => 0.9, # output leakink coefficient
:tau_out => 5.0, # output time constant in millisecond. It should equals to time use for 1 sequence :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 :out => 0.0, # neuron's output value store here
) )
neuron1 = linear_neuron(linear_neuron_params) neuron1 = linearNeuron(linear_neuron_params)
""" """
function linear_neuron(params::Dict) function linearNeuron(params::Dict)
n = linear_neuron() n = linearNeuron()
field_names = fieldnames(typeof(n)) field_names = fieldnames(typeof(n))
for i in field_names for i in field_names
if i in keys(params) if i in keys(params)
@@ -592,15 +592,15 @@ function load_optimiser(optimiser_name::String; params::Union{Dict,Nothing} = no
end end
end end
function init_neuron!(id::Int64, n::passthrough_neuron, n_params::Dict, kfnParams::Dict) function init_neuron!(id::Int64, n::passthroughNeuron, n_params::Dict, kfnParams::Dict)
n.id = id n.id = id
n.knowledgeFnName = kfnParams[:knowledgeFnName] n.knowledgeFnName = kfnParams[:knowledgeFnName]
end end
# function init_neuron!(id::Int64, n::lif_neuron, kfnParams::Dict) # function init_neuron!(id::Int64, n::lifNeuron, kfnParams::Dict)
# n.id = id # n.id = id
# n.knowledgeFnName = kfnParams[:knowledgeFnName] # n.knowledgeFnName = kfnParams[:knowledgeFnName]
# subscription_options = shuffle!([1:(kfnParams[:input_neuron_number]+kfnParams[:compute_neuron_number])...]) # subscription_options = shuffle!([1:(kfnParams[:input_neuron_number]+kfnParams[:computeNeuronNumber])...])
# if typeof(kfnParams[:synaptic_connection_number]) == String # if typeof(kfnParams[:synaptic_connection_number]) == String
# percent = parse(Int, kfnParams[:synaptic_connection_number][1:end-1]) / 100 # percent = parse(Int, kfnParams[:synaptic_connection_number][1:end-1]) / 100
# synaptic_connection_number = floor(length(subscription_options) * percent) # synaptic_connection_number = floor(length(subscription_options) * percent)
@@ -614,12 +614,12 @@ end
# n.alpha = calculate_α(n) # n.alpha = calculate_α(n)
# end # end
function init_neuron!(id::Int64, n::lif_neuron, n_params::Dict, kfnParams::Dict) function init_neuron!(id::Int64, n::lifNeuron, n_params::Dict, kfnParams::Dict)
n.id = id n.id = id
n.knowledgeFnName = kfnParams[:knowledgeFnName] n.knowledgeFnName = kfnParams[:knowledgeFnName]
subscription_options = shuffle!([1:kfnParams[:total_neurons]...]) subscription_options = shuffle!([1:kfnParams[:totalNeurons]...])
subscription_numbers = Int(floor(n_params[:synaptic_connection_number] * subscription_numbers = Int(floor(n_params[:synaptic_connection_number] *
kfnParams[:total_neurons] / 100.0)) kfnParams[:totalNeurons] / 100.0))
n.subscriptionList = [pop!(subscription_options) for i = 1:subscription_numbers] n.subscriptionList = [pop!(subscription_options) for i = 1:subscription_numbers]
# prevent subscription to itself by removing this neuron id # prevent subscription to itself by removing this neuron id
@@ -632,13 +632,13 @@ function init_neuron!(id::Int64, n::lif_neuron, n_params::Dict, kfnParams::Dict)
n.alpha = calculate_α(n) n.alpha = calculate_α(n)
end end
function init_neuron!(id::Int64, n::alif_neuron, n_params::Dict, function init_neuron!(id::Int64, n::alifNeuron, n_params::Dict,
kfnParams::Dict) kfnParams::Dict)
n.id = id n.id = id
n.knowledgeFnName = kfnParams[:knowledgeFnName] n.knowledgeFnName = kfnParams[:knowledgeFnName]
subscription_options = shuffle!([1:kfnParams[:total_neurons]...]) subscription_options = shuffle!([1:kfnParams[:totalNeurons]...])
subscription_numbers = Int(floor(n_params[:synaptic_connection_number] * subscription_numbers = Int(floor(n_params[:synaptic_connection_number] *
kfnParams[:total_neurons] / 100.0)) kfnParams[:totalNeurons] / 100.0))
n.subscriptionList = [pop!(subscription_options) for i = 1:subscription_numbers] n.subscriptionList = [pop!(subscription_options) for i = 1:subscription_numbers]
# prevent subscription to itself by removing this neuron id # prevent subscription to itself by removing this neuron id
@@ -657,13 +657,13 @@ function init_neuron!(id::Int64, n::alif_neuron, n_params::Dict,
end end
function init_neuron!(id::Int64, n::linear_neuron, n_params::Dict, kfnParams::Dict) function init_neuron!(id::Int64, n::linearNeuron, n_params::Dict, kfnParams::Dict)
n.id = id n.id = id
n.knowledgeFnName = kfnParams[:knowledgeFnName] n.knowledgeFnName = kfnParams[:knowledgeFnName]
subscription_options = shuffle!([kfnParams[:total_input_port]+1 : kfnParams[:total_neurons]...]) subscription_options = shuffle!([kfnParams[:totalInputPort]+1 : kfnParams[:totalNeurons]...])
subscription_numbers = Int(floor(n_params[:synaptic_connection_number] * subscription_numbers = Int(floor(n_params[:synaptic_connection_number] *
kfnParams[:total_compute_neuron] / 100.0)) kfnParams[:totalComputeNeuron] / 100.0))
n.subscriptionList = [pop!(subscription_options) for i = 1:subscription_numbers] n.subscriptionList = [pop!(subscription_options) for i = 1:subscription_numbers]
n.synapticStrength = rand(-5:0.1:-3, length(n.subscriptionList)) n.synapticStrength = rand(-5:0.1:-3, length(n.subscriptionList))
@@ -695,14 +695,14 @@ function instantiate_custom_types(params::Union{Dict,Nothing} = nothing)
return model() return model()
elseif type == "knowledgeFn" elseif type == "knowledgeFn"
return knowledgeFn() return knowledgeFn()
elseif type == "passthrough_neuron" elseif type == "passthroughNeuron"
return passthrough_neuron(params) return passthroughNeuron(params)
elseif type == "lif_neuron" elseif type == "lifNeuron"
return lif_neuron(params) return lifNeuron(params)
elseif type == "alif_neuron" elseif type == "alifNeuron"
return alif_neuron(params) return alifNeuron(params)
elseif type == "linear_neuron" elseif type == "linearNeuron"
return linear_neuron(params) return linearNeuron(params)
else else
return nothing return nothing
end end
@@ -716,17 +716,17 @@ end
# function add_neuron!(neuron_Dict::Dict, kfn::knowledgeFn) # function add_neuron!(neuron_Dict::Dict, kfn::knowledgeFn)
# id = length(kfn.neuronsArray) + 1 # id = length(kfn.neuronsArray) + 1
# neuron = init_neuron(id, neuron_Dict, kfn.kfnParams, # neuron = init_neuron(id, neuron_Dict, kfn.kfnParams,
# total_neurons = (length(kfn.neuronsArray) + 1)) # totalNeurons = (length(kfn.neuronsArray) + 1))
# push!(kfn.neuronsArray, neuron) # push!(kfn.neuronsArray, neuron)
# # Randomly select an output neuron to add a new neuron to # # Randomly select an output neuron to add a new neuron to
# add_n_output_n!(Random.rand(kfn.outputNeuronsArray), id) # add_n_output_n!(Random.rand(kfn.outputNeuronsArray), id)
# end # end
calculate_α(neuron::lif_neuron) = exp(-neuron.delta / neuron.tau_m) calculate_α(neuron::lifNeuron) = exp(-neuron.delta / neuron.tau_m)
calculate_α(neuron::alif_neuron) = exp(-neuron.delta / neuron.tau_m) calculate_α(neuron::alifNeuron) = exp(-neuron.delta / neuron.tau_m)
calculate_ρ(neuron::alif_neuron) = exp(-neuron.delta / neuron.tau_a) calculate_ρ(neuron::alifNeuron) = exp(-neuron.delta / neuron.tau_a)
calculate_k(neuron::linear_neuron) = exp(-neuron.delta / neuron.tau_out) calculate_k(neuron::linearNeuron) = exp(-neuron.delta / neuron.tau_out)
#------------------------------------------------------------------------------------------------100 #------------------------------------------------------------------------------------------------100