alif forward

This commit is contained in:
ton
2023-07-23 15:39:48 +07:00
parent b7c87bd0fa
commit bdec057886
2 changed files with 162 additions and 61 deletions

View File

@@ -30,7 +30,6 @@ Base.@kwdef mutable struct kfn_1 <: knowledgeFn
lif_zit::Union{AbstractArray, Nothing} = nothing
lif_wRec::Union{AbstractArray, Nothing} = nothing
# lif_recSignal::Union{AbstractArray, Nothing} = nothing
lif_vt0::Union{AbstractArray, Nothing} = nothing
lif_vt1::Union{AbstractArray, Nothing} = nothing
lif_vth::Union{AbstractArray, Nothing} = nothing
@@ -39,33 +38,55 @@ Base.@kwdef mutable struct kfn_1 <: knowledgeFn
lif_zt1::Union{AbstractArray, Nothing} = nothing
lif_refractoryCounter::Union{AbstractArray, Nothing} = nothing
lif_refractoryDuration::Union{AbstractArray, Nothing} = nothing
# lif_refractoryActive::Union{AbstractArray, Nothing} = nothing
# lif_refractoryInactive::Union{AbstractArray, Nothing} = nothing
lif_alpha::Union{AbstractArray, Nothing} = nothing
lif_delta::AbstractFloat = 1.0
lif_tau_m::AbstractFloat = 20.0
lif_phi::Union{AbstractArray, Nothing} = nothing
lif_epsilonRec::Union{AbstractArray, Nothing} = nothing
lif_eta::Union{AbstractArray, Nothing} = nothing
lif_gammaPd::Union{AbstractArray, Nothing} = nothing
lif_firingCounter::Union{AbstractArray, Nothing} = nothing
# ---------------------------------------------------------------------------- #
# ALIF #
# ---------------------------------------------------------------------------- #
alif_zit::Union{AbstractArray, Nothing} = nothing
alif_wRec::Union{AbstractArray, Nothing} = nothing
alif_recSignal::Union{AbstractArray, Nothing} = nothing
alif_vt0::Union{AbstractArray, Nothing} = nothing
alif_vt1::Union{AbstractArray, Nothing} = nothing
alif_vth::Union{AbstractArray, Nothing} = nothing
alif_avth::Union{AbstractArray, Nothing} = nothing
alif_vRest::Union{AbstractArray, Nothing} = nothing
alif_zt0::Union{AbstractArray, Nothing} = nothing
alif_zt1::Union{AbstractArray, Nothing} = nothing
alif_refractoryCounter::Union{AbstractArray, Nothing} = nothing
alif_refractoryActive::Union{AbstractArray, Nothing} = nothing
alif_refractoryInactive::Union{AbstractArray, Nothing} = nothing
alif_refractoryDuration::Union{AbstractArray, Nothing} = nothing
alif_alpha::Union{AbstractArray, Nothing} = nothing
alif_delta::AbstractFloat = 1.0
alif_tau_m::AbstractFloat = 20.0
alif_phi::Union{AbstractArray, Nothing} = nothing
alif_epsilonRec::Union{AbstractArray, Nothing} = nothing
alif_epsilonRecA::Union{AbstractArray, Nothing} = nothing
alif_eta::Union{AbstractArray, Nothing} = nothing
alif_gammaPd::Union{AbstractArray, Nothing} = nothing
alif_firingCounter::Union{AbstractArray, Nothing} = nothing
alif_a::Union{AbstractArray, Nothing} = nothing # threshold adaptation
alif_beta::Union{AbstractArray, Nothing} = nothing # β, constant, value from paper
alif_rho::Union{AbstractArray, Nothing} = nothing # ρ, threshold adaptation decay factor
alif_tau_a::AbstractFloat = 100.0 # τ_a, adaption time constant in millisecond
end
# outer constructor
function kfn_1(params::Dict)
kfn = kfn_1()
kfn.params = params
# ----------------------- initialize activation matrix ----------------------- #
# ---------------------------------------------------------------------------- #
# initialize activation matrix #
# ---------------------------------------------------------------------------- #
# row*col is a 2D matrix represent all RSNN activation
row, col, batch = kfn.params[:inputPort][:signal][:numbers] # z-axis represent signal batch number
row += kfn.params[:inputPort][:noise][:numbers][1]
@@ -74,14 +95,14 @@ function kfn_1(params::Dict)
col += kfn.params[:computeNeuron][:alif][:numbers][2]
# activation matrix
kfn.zit = zeros(row, col, 1, batch)
# -------------------------------- LIF config -------------------------------- #
kfn.zit = zeros(row, col, 1, batch)
# ---------------------------------------------------------------------------- #
# LIF config #
# ---------------------------------------------------------------------------- #
# In 3D LIF matrix, z-axis represent each neuron while each 2D slice represent that neuron's
# synaptic subscription to other neurons (via activation matrix)
z = kfn.params[:computeNeuron][:lif][:numbers][1] * kfn.params[:computeNeuron][:lif][:numbers][2]
kfn.lif_zit = zeros(row, col, z, batch)
# kfn.lif_recSignal = zeros(1, 1, z, batch)
kfn.lif_vt0 = zeros(1, 1, z, batch)
kfn.lif_vt1 = zeros(1, 1, z, batch)
kfn.lif_vth = ones(1, 1, z, batch)
@@ -90,11 +111,11 @@ function kfn_1(params::Dict)
kfn.lif_zt1 = zeros(1, 1, z, batch)
kfn.lif_refractoryCounter = zeros(1, 1, z, batch)
kfn.lif_refractoryDuration = ones(1, 1, z, batch) .* 3
# kfn.lif_refractoryActive = zeros(1, 1, z, batch)
# kfn.lif_refractoryInactive = zeros(1, 1, z, batch)
kfn.lif_alpha = ones(1, 1, z, batch) .* (exp(-kfn.lif_delta / kfn.lif_tau_m))
kfn.lif_phi = zeros(1, 1, z, batch)
kfn.lif_epsilonRec = zeros(row, col, z, batch)
kfn.lif_eta = zeros(1, 1, z, batch)
kfn.lif_gammaPd = zeros(1, 1, z, batch) .* 0.3
# subscription
w = zeros(row, col, z)
@@ -110,15 +131,30 @@ function kfn_1(params::Dict)
kfn.lif_wRec = reshape(w, (row, col, z, 1)) .* ones(row, col, z, batch)
kfn.lif_firingCounter = zeros(1, 1, z, batch)
# -------------------------------- ALIF config ------------------------------- #
# ---------------------------------------------------------------------------- #
# ALIF config #
# ---------------------------------------------------------------------------- #
z = kfn.params[:computeNeuron][:alif][:numbers][1] * kfn.params[:computeNeuron][:alif][:numbers][2]
kfn.alif_recSignal = zeros(1, 1, z, batch)
kfn.alif_zit = zeros(row, col, z, batch)
kfn.alif_vt0 = zeros(1, 1, z, batch)
kfn.alif_vt1 = zeros(1, 1, z, batch)
kfn.alif_vth = ones(1, 1, z, batch)
kfn.alif_avth = ones(1, 1, z, batch)
kfn.alif_vRest = zeros(1, 1, z, batch)
kfn.alif_zt0 = zeros(1, 1, z, batch)
kfn.alif_zt1 = zeros(1, 1, z, batch)
kfn.alif_refractoryCounter = zeros(1, 1, z, batch)
kfn.alif_refractoryActive = zeros(1, 1, z, batch)
kfn.alif_refractoryInactive = zeros(1, 1, z, batch)
kfn.alif_refractoryDuration = ones(1, 1, z, batch) .* 3
kfn.alif_alpha = ones(1, 1, z, batch) .* (exp(-kfn.alif_delta / kfn.alif_tau_m))
kfn.alif_phi = zeros(1, 1, z, batch)
kfn.alif_epsilonRec = zeros(row, col, z, batch)
kfn.alif_epsilonRecA = zeros(row, col, z, batch)
kfn.alif_eta = zeros(1, 1, z, batch)
kfn.alif_gammaPd = zeros(1, 1, z, batch) .* 0.3
kfn.alif_a = zeros(1, 1, z, batch)
kfn.alif_beta = zeros(1, 1, z, batch) .* 0.15
kfn.alif_rho = zeros(1, 1, z, batch) .* (exp(-kfn.alif_delta / kfn.alif_tau_a))
# subscription
w = zeros(row, col, z)
@@ -127,12 +163,14 @@ function kfn_1(params::Dict)
for slice in eachslice(w, dims=3)
pool = shuffle!([1:row*col...])[1:synapticConnection]
for i in pool
slice[i] = randn()/10
slice[i] = randn()/10 # assign weight to synaptic connection
end
end
# project 3D w into 4D kfn.lif_wRec
kfn.alif_wRec = reshape(w, (row, col, z, 1)) .* ones(row, col, z, batch)
kfn.alif_firingCounter = zeros(1, 1, z, batch)