refractoring
This commit is contained in:
@@ -40,7 +40,7 @@ function (kfn::kfn_1)(m::model, input_data::AbstractVector)
|
|||||||
resetLearningParams!(n)
|
resetLearningParams!(n)
|
||||||
end
|
end
|
||||||
|
|
||||||
for n in kfn.output_neuron
|
for n in kfn.outputNeuronsArray
|
||||||
# epsilonRec need to be reset because it counting how many each synaptic fires and
|
# epsilonRec need to be reset because it counting how many each synaptic fires and
|
||||||
# use this info to calculate how much synaptic weight should be adjust
|
# use this info to calculate how much synaptic weight should be adjust
|
||||||
resetLearningParams!(n)
|
resetLearningParams!(n)
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ function timestep_forward!(x::passthrough_neuron)
|
|||||||
x.z_t = x.z_t1
|
x.z_t = x.z_t1
|
||||||
end
|
end
|
||||||
|
|
||||||
function timestep_forward!(x::Union{compute_neuron, output_neuron})
|
function timestep_forward!(x::Union{computeNeuron, outputNeuron})
|
||||||
x.z_t = x.z_t1
|
x.z_t = x.z_t1
|
||||||
x.v_t = x.v_t1
|
x.v_t = x.v_t1
|
||||||
end
|
end
|
||||||
@@ -28,24 +28,24 @@ no_negative!(x) = x < 0.0 ? 0.0 : x
|
|||||||
precision(x::Array{<:Array}) = ( std(mean.(x)) / mean(mean.(x)) ) * 100
|
precision(x::Array{<:Array}) = ( std(mean.(x)) / mean(mean.(x)) ) * 100
|
||||||
|
|
||||||
# reset functions for LIF/ALIF neuron
|
# reset functions for LIF/ALIF neuron
|
||||||
reset_last_firing_time!(n::compute_neuron) = n.lastFiringTime = 0.0
|
reset_last_firing_time!(n::computeNeuron) = n.lastFiringTime = 0.0
|
||||||
reset_refractory_state_active!(n::compute_neuron) = n.refractory_state_active = false
|
reset_refractory_state_active!(n::computeNeuron) = n.refractory_state_active = false
|
||||||
reset_v_t!(n::neuron) = n.v_t = n.vRest
|
reset_v_t!(n::neuron) = n.v_t = n.vRest
|
||||||
reset_z_t!(n::compute_neuron) = n.z_t = false
|
reset_z_t!(n::computeNeuron) = n.z_t = false
|
||||||
reset_epsilonRec!(n::compute_neuron) = n.epsilonRec = n.epsilonRec * 0.0
|
reset_epsilonRec!(n::computeNeuron) = n.epsilonRec = n.epsilonRec * 0.0
|
||||||
reset_epsilonRec!(n::output_neuron) = 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::alif_neuron) = n.epsilonRecA = n.epsilonRecA * 0.0
|
||||||
reset_epsilon_in!(n::compute_neuron) = 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{compute_neuron, linear_neuron}) = n.error = nothing
|
reset_error!(n::Union{computeNeuron, linear_neuron}) = n.error = nothing
|
||||||
reset_w_in_change!(n::compute_neuron) = 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::compute_neuron) = 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::alif_neuron) = n.a = n.a * 0.0
|
||||||
reset_reg_voltage_a!(n::compute_neuron) = 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::compute_neuron) = 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::compute_neuron) = 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::compute_neuron) = n.firingCounter = n.firingCounter * 0.0
|
reset_firing_counter!(n::computeNeuron) = n.firingCounter = n.firingCounter * 0.0
|
||||||
reset_firing_diff!(n::Union{compute_neuron, linear_neuron}) = n.firingDiff = n.firingDiff * 0.0
|
reset_firing_diff!(n::Union{computeNeuron, linear_neuron}) = n.firingDiff = n.firingDiff * 0.0
|
||||||
reset_refractoryCounter!(n::Union{compute_neuron, linear_neuron}) = n.refractoryCounter = n.refractoryCounter * 0.0
|
reset_refractoryCounter!(n::Union{computeNeuron, linear_neuron}) = 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::linear_neuron) = n.epsilon_j = n.epsilon_j * 0.0
|
||||||
@@ -218,12 +218,12 @@ function cal_v_reg!(n::alif_neuron)
|
|||||||
n.reg_voltage_b = n.reg_voltage_b + (component_b * (n.epsilonRec - n.epsilonRecA))
|
n.reg_voltage_b = n.reg_voltage_b + (component_b * (n.epsilonRec - n.epsilonRecA))
|
||||||
end
|
end
|
||||||
|
|
||||||
function voltage_error!(n::compute_neuron)
|
function voltage_error!(n::computeNeuron)
|
||||||
n.reg_voltage_error = 0.5 * n.reg_voltage_a
|
n.reg_voltage_error = 0.5 * n.reg_voltage_a
|
||||||
return n.reg_voltage_error
|
return n.reg_voltage_error
|
||||||
end
|
end
|
||||||
|
|
||||||
function voltage_regulator!(n::compute_neuron) # running average
|
function voltage_regulator!(n::computeNeuron) # running average
|
||||||
Δw = n.optimiser.eta * n.c_reg_v * n.reg_voltage_b
|
Δw = n.optimiser.eta * n.c_reg_v * n.reg_voltage_b
|
||||||
return Δw
|
return Δw
|
||||||
end
|
end
|
||||||
@@ -233,7 +233,7 @@ function firingRateError(kfn::knowledgeFn)
|
|||||||
return 0.5 * sum([(n.firingDiff)^2 for n in kfn.neuronsArray[start_id:end]])
|
return 0.5 * sum([(n.firingDiff)^2 for n in kfn.neuronsArray[start_id:end]])
|
||||||
end
|
end
|
||||||
|
|
||||||
function firing_rate_regulator!(n::compute_neuron)
|
function firing_rate_regulator!(n::computeNeuron)
|
||||||
# n.firingRate NOT running average (average over learning batch)
|
# n.firingRate NOT running average (average over learning batch)
|
||||||
Δw = n.optimiser.eta * n.c_reg *
|
Δw = n.optimiser.eta * n.c_reg *
|
||||||
(n.firingRate - n.firingRateTarget) * n.eRec
|
(n.firingRate - n.firingRateTarget) * n.eRec
|
||||||
@@ -241,10 +241,10 @@ function firing_rate_regulator!(n::compute_neuron)
|
|||||||
return Δw
|
return Δw
|
||||||
end
|
end
|
||||||
|
|
||||||
firing_rate!(n::compute_neuron) = n.firingRate = (n.firingCounter / n.timeStep) * 1000
|
firing_rate!(n::computeNeuron) = n.firingRate = (n.firingCounter / n.timeStep) * 1000
|
||||||
firing_diff!(n::compute_neuron) = n.firingDiff = n.firingRate - n.firingRateTarget
|
firing_diff!(n::computeNeuron) = n.firingDiff = n.firingRate - n.firingRateTarget
|
||||||
|
|
||||||
function adjust_internal_learning_rate!(n::compute_neuron)
|
function adjust_internal_learning_rate!(n::computeNeuron)
|
||||||
n.internal_learning_rate = n.error_diff[end] < 0.0 ? n.internal_learning_rate * 0.99 :
|
n.internal_learning_rate = n.error_diff[end] < 0.0 ? n.internal_learning_rate * 0.99 :
|
||||||
n.internal_learning_rate * 1.005
|
n.internal_learning_rate * 1.005
|
||||||
end
|
end
|
||||||
@@ -276,7 +276,7 @@ end
|
|||||||
""" Compute all synaptic connection strength of a neuron. Also mark n.wRec to 0 if wRec goes
|
""" Compute all synaptic connection strength of a neuron. Also mark n.wRec to 0 if wRec goes
|
||||||
below lowerlimit.
|
below lowerlimit.
|
||||||
"""
|
"""
|
||||||
function synapticConnStrength!(n::Union{compute_neuron, output_neuron})
|
function synapticConnStrength!(n::Union{computeNeuron, outputNeuron})
|
||||||
for (i, connStrength) in enumerate(n.synapticStrength)
|
for (i, connStrength) in enumerate(n.synapticStrength)
|
||||||
# check whether connStrength increase or decrease based on usage from n.epsilonRec
|
# check whether connStrength increase or decrease based on usage from n.epsilonRec
|
||||||
updown = n.epsilonRec[i] == 0.0 ? "down" : "up"
|
updown = n.epsilonRec[i] == 0.0 ? "down" : "up"
|
||||||
@@ -290,9 +290,9 @@ function synapticConnStrength!(n::Union{compute_neuron, output_neuron})
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function synapticConnStrength!(n::input_neuron) end
|
function synapticConnStrength!(n::inputNeuron) end
|
||||||
|
|
||||||
function neuroplasticity!(n::compute_neuron, firedNeurons::Vector)
|
function neuroplasticity!(n::computeNeuron, firedNeurons::Vector)
|
||||||
# if there is 0-weight then replace it with new connection
|
# if there is 0-weight then replace it with new connection
|
||||||
zero_weight_index = findall(iszero.(n.wRec))
|
zero_weight_index = findall(iszero.(n.wRec))
|
||||||
if length(zero_weight_index) != 0
|
if length(zero_weight_index) != 0
|
||||||
|
|||||||
26
src/types.jl
26
src/types.jl
@@ -3,7 +3,7 @@ module types
|
|||||||
export
|
export
|
||||||
# struct
|
# struct
|
||||||
IronpenStruct, model, knowledgeFn, lif_neuron, alif_neuron, linear_neuron,
|
IronpenStruct, model, knowledgeFn, lif_neuron, alif_neuron, linear_neuron,
|
||||||
kfn_1, input_neuron, compute_neuron, neuron, output_neuron, passthrough_neuron,
|
kfn_1, inputNeuron, computeNeuron, neuron, outputNeuron, passthrough_neuron,
|
||||||
|
|
||||||
# function
|
# function
|
||||||
instantiate_custom_types, init_neuron, populate_neuron,
|
instantiate_custom_types, init_neuron, populate_neuron,
|
||||||
@@ -16,9 +16,9 @@ using Random, Flux, LinearAlgebra
|
|||||||
abstract type Ironpen end
|
abstract type Ironpen end
|
||||||
abstract type knowledgeFn <: Ironpen end
|
abstract type knowledgeFn <: Ironpen end
|
||||||
abstract type neuron <: Ironpen end
|
abstract type neuron <: Ironpen end
|
||||||
abstract type input_neuron <: neuron end
|
abstract type inputNeuron <: neuron end
|
||||||
abstract type output_neuron <: neuron end
|
abstract type outputNeuron <: neuron end
|
||||||
abstract type compute_neuron <: neuron end
|
abstract type computeNeuron <: neuron end
|
||||||
|
|
||||||
#------------------------------------------------------------------------------------------------100
|
#------------------------------------------------------------------------------------------------100
|
||||||
|
|
||||||
@@ -207,8 +207,8 @@ function kfn_1(kfnParams::Dict)
|
|||||||
end
|
end
|
||||||
|
|
||||||
# add compute neurons
|
# add compute neurons
|
||||||
for (k, v) in kfn.kfnParams[:compute_neuron]
|
for (k, v) in kfn.kfnParams[:computeNeuron]
|
||||||
current_type = kfn.kfnParams[:compute_neuron][k]
|
current_type = kfn.kfnParams[:computeNeuron][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)
|
||||||
@@ -223,12 +223,12 @@ function kfn_1(kfnParams::Dict)
|
|||||||
end
|
end
|
||||||
|
|
||||||
for n in kfn.neuronsArray
|
for n in kfn.neuronsArray
|
||||||
if typeof(n) <: compute_neuron
|
if typeof(n) <: computeNeuron
|
||||||
n.firingRateTarget = kfn.kfnParams[:neuron_firing_rate_target]
|
n.firingRateTarget = kfn.kfnParams[:neuron_firing_rate_target]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# excitatory neuron to inhabitory neuron = 60:40 % of compute_neuron
|
# 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[:compute_neuron_number]))
|
||||||
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[:compute_neuron_number] - ex_number
|
||||||
@@ -241,7 +241,7 @@ function kfn_1(kfnParams::Dict)
|
|||||||
try n.ExInType = pop!(ex_in) catch end
|
try n.ExInType = pop!(ex_in) catch end
|
||||||
end
|
end
|
||||||
|
|
||||||
# add ExInType into each compute_neuron subExInType
|
# add ExInType into each computeNeuron subExInType
|
||||||
for n in reverse(kfn.neuronsArray)
|
for n in reverse(kfn.neuronsArray)
|
||||||
try # input neuron doest have n.subscriptionList
|
try # input neuron doest have n.subscriptionList
|
||||||
for sub_id in n.subscriptionList
|
for sub_id in n.subscriptionList
|
||||||
@@ -270,7 +270,7 @@ end
|
|||||||
|
|
||||||
""" passthrough_neuron struct
|
""" passthrough_neuron struct
|
||||||
"""
|
"""
|
||||||
Base.@kwdef mutable struct passthrough_neuron <: input_neuron
|
Base.@kwdef mutable struct passthrough_neuron <: 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 = "passthrough_neuron"
|
||||||
knowledgeFnName::Union{String,Nothing} = nothing # knowledgeFn that this neuron belongs to
|
knowledgeFnName::Union{String,Nothing} = nothing # knowledgeFn that this neuron belongs to
|
||||||
@@ -300,7 +300,7 @@ end
|
|||||||
|
|
||||||
""" lif_neuron struct
|
""" lif_neuron struct
|
||||||
"""
|
"""
|
||||||
Base.@kwdef mutable struct lif_neuron <: compute_neuron
|
Base.@kwdef mutable struct lif_neuron <: 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 = "lif_neuron"
|
||||||
ExInType::Integer = 1 # 1 excitatory, -1 inhabitory
|
ExInType::Integer = 1 # 1 excitatory, -1 inhabitory
|
||||||
@@ -392,7 +392,7 @@ end
|
|||||||
|
|
||||||
""" alif_neuron struct
|
""" alif_neuron struct
|
||||||
"""
|
"""
|
||||||
Base.@kwdef mutable struct alif_neuron <: compute_neuron
|
Base.@kwdef mutable struct alif_neuron <: 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 = "alif_neuron"
|
||||||
ExInType::Integer = -1 # 1 excitatory, -1 inhabitory
|
ExInType::Integer = -1 # 1 excitatory, -1 inhabitory
|
||||||
@@ -500,7 +500,7 @@ end
|
|||||||
#------------------------------------------------------------------------------------------------100
|
#------------------------------------------------------------------------------------------------100
|
||||||
""" linear_neuron struct
|
""" linear_neuron struct
|
||||||
"""
|
"""
|
||||||
Base.@kwdef mutable struct linear_neuron <: output_neuron
|
Base.@kwdef mutable struct linear_neuron <: 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 = "linear_neuron"
|
||||||
knowledgeFnName::Union{String,Nothing} = nothing # knowledgeFn that this neuron belongs to
|
knowledgeFnName::Union{String,Nothing} = nothing # knowledgeFn that this neuron belongs to
|
||||||
|
|||||||
Reference in New Issue
Block a user