kfn forward()

This commit is contained in:
ton
2023-07-24 14:21:28 +07:00
parent bdec057886
commit 1e08a4d750
2 changed files with 264 additions and 144 deletions

View File

@@ -24,7 +24,7 @@ Base.@kwdef mutable struct kfn_1 <: knowledgeFn
zit::Union{AbstractArray, Nothing} = nothing # 3D activation matrix
# ---------------------------------------------------------------------------- #
# LIF #
# LIF Neurons #
# ---------------------------------------------------------------------------- #
# a projection of kfn.zit into lif dimension for broadcasting later)
lif_zit::Union{AbstractArray, Nothing} = nothing
@@ -49,7 +49,7 @@ Base.@kwdef mutable struct kfn_1 <: knowledgeFn
lif_firingCounter::Union{AbstractArray, Nothing} = nothing
# ---------------------------------------------------------------------------- #
# ALIF #
# ALIF Neurons #
# ---------------------------------------------------------------------------- #
alif_zit::Union{AbstractArray, Nothing} = nothing
@@ -78,24 +78,51 @@ Base.@kwdef mutable struct kfn_1 <: knowledgeFn
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
# ---------------------------------------------------------------------------- #
# Output Neurons #
# ---------------------------------------------------------------------------- #
# output neuron is based on LIF
on_zit::Union{AbstractArray, Nothing} = nothing
on_wRec::Union{AbstractArray, Nothing} = nothing
on_vt0::Union{AbstractArray, Nothing} = nothing
on_vt1::Union{AbstractArray, Nothing} = nothing
on_vth::Union{AbstractArray, Nothing} = nothing
on_vRest::Union{AbstractArray, Nothing} = nothing
on_zt0::Union{AbstractArray, Nothing} = nothing
on_zt1::Union{AbstractArray, Nothing} = nothing
on_refractoryCounter::Union{AbstractArray, Nothing} = nothing
on_refractoryDuration::Union{AbstractArray, Nothing} = nothing
on_alpha::Union{AbstractArray, Nothing} = nothing
on_delta::AbstractFloat = 1.0
on_tau_m::AbstractFloat = 20.0
on_phi::Union{AbstractArray, Nothing} = nothing
on_epsilonRec::Union{AbstractArray, Nothing} = nothing
on_eta::Union{AbstractArray, Nothing} = nothing
on_gammaPd::Union{AbstractArray, Nothing} = nothing
on_firingCounter::Union{AbstractArray, Nothing} = nothing
end
# outer constructor
function kfn_1(params::Dict)
kfn = kfn_1()
kfn.params = params
# ---------------------------------------------------------------------------- #
# 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]
col += kfn.params[:inputPort][:signal][:numbers][2]
# row += kfn.params[:inputPort][:noise][:numbers][1]
col += kfn.params[:inputPort][:noise][:numbers][2]
col += kfn.params[:computeNeuron][:lif][:numbers][2]
col += kfn.params[:computeNeuron][:alif][:numbers][2]
# activation matrix
kfn.zit = zeros(row, col, 1, batch)
kfn.zit = zeros(row, col, batch)
# ---------------------------------------------------------------------------- #
# LIF config #
# ---------------------------------------------------------------------------- #
@@ -129,8 +156,8 @@ function kfn_1(params::Dict)
end
# project 3D w into 4D kfn.lif_wRec
kfn.lif_wRec = reshape(w, (row, col, z, 1)) .* ones(row, col, z, batch)
kfn.lif_firingCounter = zeros(1, 1, z, batch)
# ---------------------------------------------------------------------------- #
# ALIF config #
# ---------------------------------------------------------------------------- #
@@ -166,21 +193,48 @@ function kfn_1(params::Dict)
slice[i] = randn()/10 # assign weight to synaptic connection
end
end
# project 3D w into 4D kfn.lif_wRec
# project 3D w into 4D kfn.alif_wRec
kfn.alif_wRec = reshape(w, (row, col, z, 1)) .* ones(row, col, z, batch)
kfn.alif_firingCounter = zeros(1, 1, z, batch)
# ---------------------------------------------------------------------------- #
# output config #
# ---------------------------------------------------------------------------- #
#WORKING
z = kfn.params[:outputPort][:numbers][1] * kfn.params[:outputPort][:numbers][2]
kfn.on_zit = zeros(row, col, z, batch)
kfn.on_vt0 = zeros(1, 1, z, batch)
kfn.on_vt1 = zeros(1, 1, z, batch)
kfn.on_vth = ones(1, 1, z, batch)
kfn.on_vRest = zeros(1, 1, z, batch)
kfn.on_zt0 = zeros(1, 1, z, batch)
kfn.on_zt1 = zeros(1, 1, z, batch)
kfn.on_refractoryCounter = zeros(1, 1, z, batch)
kfn.on_refractoryDuration = ones(1, 1, z, batch) .* 1
kfn.on_alpha = ones(1, 1, z, batch) .* (exp(-kfn.on_delta / kfn.on_tau_m))
kfn.on_phi = zeros(1, 1, z, batch)
kfn.on_epsilonRec = zeros(row, col, z, batch)
kfn.on_eta = zeros(1, 1, z, batch)
kfn.on_gammaPd = zeros(1, 1, z, batch) .* 0.3
# subscription
w = zeros(row, col, z)
synapticConnectionPercent = kfn.params[:outputPort][:params][:synapticConnectionPercent]
synapticConnection = Int(floor(row*col * synapticConnectionPercent/100))
for slice in eachslice(w, dims=3)
pool = shuffle!([1:row*col...])[1:synapticConnection]
for i in pool
slice[i] = randn()/10 # assign weight to synaptic connection
end
end
# project 3D w into 4D kfn.on_wRec
kfn.on_wRec = reshape(w, (row, col, z, 1)) .* ones(row, col, z, batch)
kfn.on_firingCounter = zeros(1, 1, z, batch)
# error("debug end outer constructor")
return kfn
end