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

@@ -2,8 +2,8 @@ module types
export
# struct
IronpenStruct, model, knowledgeFn, lif_neuron, alif_neuron, linear_neuron,
kfn_1, inputNeuron, computeNeuron, neuron, outputNeuron, passthrough_neuron,
IronpenStruct, model, knowledgeFn, lifNeuron, alifNeuron, linearNeuron,
kfn_1, inputNeuron, computeNeuron, neuron, outputNeuron, passthroughNeuron,
# function
instantiate_custom_types, init_neuron, populate_neuron,
@@ -42,7 +42,7 @@ end
# Example
I_kfnparams = Dict(
:type => "lif_neuron",
:type => "lifNeuron",
: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)
:z_t => false, # neuron firing status at time = t
@@ -120,7 +120,7 @@ end
# Example
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)
:z_t => false, # neuron firing status at time = t
:gammaPd => 0.3, # discount factor. The value is from the paper
@@ -130,7 +130,7 @@ end
)
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)
:z_t => false, # neuron firing status at time = t
:gammaPd => 0.3, # discount factor. The value is from the paper
@@ -146,7 +146,7 @@ end
)
linear_neuron_params = Dict(
:type => "linear_neuron",
: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
@@ -166,7 +166,7 @@ end
:neuron_w_rec_generation_pattern => "random",
:neuron_v_t_default => 0.5,
:neuron_voltage_drop_percentage => "100%",
:neuron_firing_rate_target => 50.0,
:neuronFiringRateTarget => 50.0,
:neuron_learning_rate => 0.01,
:neuron_c_reg => 0.0001,
:neuron_c_reg_v => 0.0001,
@@ -182,23 +182,23 @@ function kfn_1(kfnParams::Dict)
kfn.kfnParams = kfnParams
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"))
end
# Bn
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
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
# assign neurons ID by their position in kfn.neurons array because I think it is
# straight forward way
# add input port
for (k, v) in kfn.kfnParams[:input_port]
current_type = kfn.kfnParams[:input_port][k]
for (k, v) in kfn.kfnParams[:inputPort]
current_type = kfn.kfnParams[:inputPort][k]
for i = 1:current_type[:numbers]
n_id = length(kfn.neuronsArray) + 1
neuron = init_neuron(n_id, current_type[:params], kfn.kfnParams)
@@ -216,22 +216,22 @@ function kfn_1(kfnParams::Dict)
end
end
for i = 1:kfn.kfnParams[:output_port][:numbers]
neuron = init_neuron(i, kfn.kfnParams[:output_port][:params],
for i = 1:kfn.kfnParams[:outputPort][:numbers]
neuron = init_neuron(i, kfn.kfnParams[:outputPort][:params],
kfn.kfnParams)
push!(kfn.outputNeuronsArray, neuron)
end
for n in kfn.neuronsArray
if typeof(n) <: computeNeuron
n.firingRateTarget = kfn.kfnParams[:neuron_firing_rate_target]
n.firingRateTarget = kfn.kfnParams[:neuronFiringRateTarget]
end
end
# 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]
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]
ex_in = shuffle!([ex_n; in_n])
@@ -268,11 +268,11 @@ end
#------------------------------------------------------------------------------------------------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
type::String = "passthrough_neuron"
type::String = "passthroughNeuron"
knowledgeFnName::Union{String,Nothing} = nothing # knowledgeFn that this neuron belongs to
z_t::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
end
function passthrough_neuron(params::Dict)
n = passthrough_neuron()
function passthroughNeuron(params::Dict)
n = passthroughNeuron()
field_names = fieldnames(typeof(n))
for i in field_names
if i in keys(params)
@@ -298,11 +298,11 @@ end
#------------------------------------------------------------------------------------------------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
type::String = "lif_neuron"
type::String = "lifNeuron"
ExInType::Integer = 1 # 1 excitatory, -1 inhabitory
# Bn::Union{Float64,Nothing} = Random.rand() # Bias for neuron error
knowledgeFnName::Union{String,Nothing} = nothing # knowledgeFn that this neuron belongs to
@@ -361,7 +361,7 @@ end
# Example
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)
:z_t => false, # neuron firing status at time = t
: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
)
neuron1 = lif_neuron(lif_neuron_params)
neuron1 = lifNeuron(lif_neuron_params)
"""
function lif_neuron(params::Dict)
n = lif_neuron()
function lifNeuron(params::Dict)
n = lifNeuron()
field_names = fieldnames(typeof(n))
for i in field_names
if i in keys(params)
@@ -390,11 +390,11 @@ end
#------------------------------------------------------------------------------------------------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
type::String = "alif_neuron"
type::String = "alifNeuron"
ExInType::Integer = -1 # 1 excitatory, -1 inhabitory
# Bn::Union{Float64,Nothing} = Random.rand() # Bias for neuron error
knowledgeFnName::Union{String,Nothing} = nothing # knowledgeFn that this neuron belongs to
@@ -462,7 +462,7 @@ end
# Example
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)
:z_t => false, # neuron firing status at time = t
@@ -479,10 +479,10 @@ end
:a => 0.0,
)
neuron1 = alif_neuron(alif_neuron_params)
neuron1 = alifNeuron(alif_neuron_params)
"""
function alif_neuron(params::Dict)
n = alif_neuron()
function alifNeuron(params::Dict)
n = alifNeuron()
field_names = fieldnames(typeof(n))
for i in field_names
if i in keys(params)
@@ -498,11 +498,11 @@ function alif_neuron(params::Dict)
end
#------------------------------------------------------------------------------------------------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
type::String = "linear_neuron"
type::String = "linearNeuron"
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
timeStep::Union{Number,Nothing} = nothing # current time
@@ -549,16 +549,16 @@ end
# Example
linear_neuron_params = Dict(
:type => "linear_neuron",
: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 = linear_neuron(linear_neuron_params)
neuron1 = linearNeuron(linear_neuron_params)
"""
function linear_neuron(params::Dict)
n = linear_neuron()
function linearNeuron(params::Dict)
n = linearNeuron()
field_names = fieldnames(typeof(n))
for i in field_names
if i in keys(params)
@@ -592,15 +592,15 @@ function load_optimiser(optimiser_name::String; params::Union{Dict,Nothing} = no
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.knowledgeFnName = kfnParams[:knowledgeFnName]
end
# function init_neuron!(id::Int64, n::lif_neuron, kfnParams::Dict)
# function init_neuron!(id::Int64, n::lifNeuron, kfnParams::Dict)
# n.id = id
# 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
# percent = parse(Int, kfnParams[:synaptic_connection_number][1:end-1]) / 100
# synaptic_connection_number = floor(length(subscription_options) * percent)
@@ -614,12 +614,12 @@ end
# n.alpha = calculate_α(n)
# 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.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] *
kfnParams[:total_neurons] / 100.0))
kfnParams[:totalNeurons] / 100.0))
n.subscriptionList = [pop!(subscription_options) for i = 1:subscription_numbers]
# 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)
end
function init_neuron!(id::Int64, n::alif_neuron, n_params::Dict,
function init_neuron!(id::Int64, n::alifNeuron, n_params::Dict,
kfnParams::Dict)
n.id = id
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] *
kfnParams[:total_neurons] / 100.0))
kfnParams[:totalNeurons] / 100.0))
n.subscriptionList = [pop!(subscription_options) for i = 1:subscription_numbers]
# 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
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.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] *
kfnParams[:total_compute_neuron] / 100.0))
kfnParams[:totalComputeNeuron] / 100.0))
n.subscriptionList = [pop!(subscription_options) for i = 1:subscription_numbers]
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()
elseif type == "knowledgeFn"
return knowledgeFn()
elseif type == "passthrough_neuron"
return passthrough_neuron(params)
elseif type == "lif_neuron"
return lif_neuron(params)
elseif type == "alif_neuron"
return alif_neuron(params)
elseif type == "linear_neuron"
return linear_neuron(params)
elseif type == "passthroughNeuron"
return passthroughNeuron(params)
elseif type == "lifNeuron"
return lifNeuron(params)
elseif type == "alifNeuron"
return alifNeuron(params)
elseif type == "linearNeuron"
return linearNeuron(params)
else
return nothing
end
@@ -716,17 +716,17 @@ end
# function add_neuron!(neuron_Dict::Dict, kfn::knowledgeFn)
# id = length(kfn.neuronsArray) + 1
# neuron = init_neuron(id, neuron_Dict, kfn.kfnParams,
# total_neurons = (length(kfn.neuronsArray) + 1))
# totalNeurons = (length(kfn.neuronsArray) + 1))
# push!(kfn.neuronsArray, neuron)
# # Randomly select an output neuron to add a new neuron to
# add_n_output_n!(Random.rand(kfn.outputNeuronsArray), id)
# end
calculate_α(neuron::lif_neuron) = exp(-neuron.delta / neuron.tau_m)
calculate_α(neuron::alif_neuron) = exp(-neuron.delta / neuron.tau_m)
calculate_ρ(neuron::alif_neuron) = exp(-neuron.delta / neuron.tau_a)
calculate_k(neuron::linear_neuron) = exp(-neuron.delta / neuron.tau_out)
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)
#------------------------------------------------------------------------------------------------100