lif forward

This commit is contained in:
ton
2023-07-23 11:03:08 +07:00
parent 6b71450055
commit b7c87bd0fa
3 changed files with 114 additions and 84 deletions

View File

@@ -21,40 +21,44 @@ Base.@kwdef mutable struct kfn_1 <: knowledgeFn
timeStep::AbstractArray = [0]
learningStage::AbstractArray = [0] # 0 inference, 1 start, 2 during, 3 end learning
z_i_t::Union{AbstractArray, Nothing} = nothing # 3D activation matrix
zit::Union{AbstractArray, Nothing} = nothing # 3D activation matrix
# ---------------------------------------------------------------------------- #
# LIF #
# ---------------------------------------------------------------------------- #
# a projection of kfn.z_i_t into lif dimension for broadcasting later)
lif_z_i_t::Union{AbstractArray, Nothing} = nothing
# a projection of kfn.zit into lif dimension for broadcasting later)
lif_zit::Union{AbstractArray, Nothing} = nothing
lif_w::Union{AbstractArray, Nothing} = nothing
lif_recSignal::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
lif_vRest::Union{AbstractArray, Nothing} = nothing
lif_zt0::Union{AbstractArray, Nothing} = nothing
lif_zt1::Union{AbstractArray, Nothing} = nothing
lif_refractoryCounter::Union{AbstractArray, Nothing} = nothing
lif_refractoryActive::Union{AbstractArray, Nothing} = nothing
lif_refractoryInactive::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_firingCounter::Union{AbstractArray, Nothing} = nothing
# ---------------------------------------------------------------------------- #
# ALIF #
# ---------------------------------------------------------------------------- #
alif_w::Union{AbstractArray, Nothing} = nothing
alif_wRec::Union{AbstractArray, Nothing} = nothing
alif_recSignal::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
end
# outer constructor
@@ -70,23 +74,27 @@ function kfn_1(params::Dict)
col += kfn.params[:computeNeuron][:alif][:numbers][2]
# activation matrix
kfn.z_i_t = zeros(row, col, batch)
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_recSignal = zeros(1, 1, z, batch)
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)
kfn.lif_vRest = zeros(1, 1, z, batch)
kfn.lif_zt0 = zeros(1, 1, z, batch)
kfn.lif_zt1 = zeros(1, 1, z, batch)
kfn.lif_refractoryCounter = zeros(1, 1, z, batch)
kfn.lif_refractoryActive = zeros(1, 1, z, batch)
kfn.lif_refractoryInactive = 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)
# subscription
w = zeros(row, col, z)
@@ -98,14 +106,13 @@ function kfn_1(params::Dict)
slice[i] = randn()/10 # assign weight to synaptic connection
end
end
#WORKING project 3D w into 4D kfn.lif_w
kfn.lif_w = reshape(w, (row, col, z, 1)) .* ones(row, col, z, batch)
println(">>> lif_w ", size(kfn.lif_w))
error("end WORKING")
# project 3D w into 4D kfn.lif_wRec
kfn.lif_wRec = reshape(w, (row, col, z, 1)) .* ones(row, col, z, batch)
# ALIF
kfn.lif_firingCounter = zeros(1, 1, z, batch)
# -------------------------------- ALIF config ------------------------------- #
z = kfn.params[:computeNeuron][:alif][:numbers][1] * kfn.params[:computeNeuron][:alif][:numbers][2]
kfn.alif_w = zeros(row, col, z) # matrix z-axis represent each neurons
kfn.alif_recSignal = zeros(1, 1, z, batch)
kfn.alif_zt0 = zeros(1, 1, z, batch)
kfn.alif_zt1 = zeros(1, 1, z, batch)
@@ -114,16 +121,17 @@ function kfn_1(params::Dict)
kfn.alif_refractoryInactive = zeros(1, 1, z, batch)
# subscription
row, col, _ = size(kfn.alif_w) # row*col is synaptic subscribe weight for each neuron in z-axis
w = zeros(row, col, z)
synapticConnectionPercent = kfn.params[:computeNeuron][:alif][:params][:synapticConnectionPercent]
synapticConnection = Int(floor(row*col * synapticConnectionPercent/100))
for slice in eachslice(kfn.alif_w, dims=3)
for slice in eachslice(w, dims=3)
pool = shuffle!([1:row*col...])[1:synapticConnection]
for i in pool
slice[i] = randn()/10
end
end
# project 3D w into 4D kfn.lif_wRec
kfn.alif_wRec = reshape(w, (row, col, z, 1)) .* ones(row, col, z, batch)