readout forward()

This commit is contained in:
ton
2023-07-30 10:12:24 +07:00
parent 50df913106
commit 28f9fb4bdc
2 changed files with 209 additions and 207 deletions

View File

@@ -117,36 +117,45 @@ function (kfn::kfn_1)(input::AbstractArray)
kfn.on_refractoryCounter, kfn.on_refractoryCounter,
kfn.on_refractoryDuration, kfn.on_refractoryDuration,
kfn.on_gammaPd, kfn.on_gammaPd,
kfn.on_firingCounter) kfn.on_firingCounter,
kfn.on_arrayProjection3DTo4D,
kfn.on_recSignal,
kfn.on_decayed_vt0,
kfn.on_decayed_epsilonRec,
kfn.on_vt1_diff_vth,
kfn.on_vt1_diff_vth_div_vth,
kfn.on_gammaPd_div_vth,
kfn.on_phiActivation)
return reshape(kfn.on_zt1, (size(input, 1), :)), return reshape(kfn.on_zt1, (size(input, 1), :)),
kfn.zit kfn.zit
end end
function lifForward(kfn_zit, function lifForward(kfn_zit::Array{T},
zit, zit::Array{T},
wRec, wRec::Array{T},
vt0, vt0::Array{T},
vt1, vt1::Array{T},
vth, vth::Array{T},
vRest, vRest::Array{T},
zt1, zt1::Array{T},
alpha, alpha::Array{T},
phi, phi::Array{T},
epsilonRec, epsilonRec::Array{T},
refractoryCounter, refractoryCounter::Array{T},
refractoryDuration, refractoryDuration::Array{T},
gammaPd, gammaPd::Array{T},
firingCounter, firingCounter::Array{T},
arrayProjection3DTo4D, arrayProjection3DTo4D::Array{T},
recSignal, recSignal::Array{T},
decayed_vt0, decayed_vt0::Array{T},
decayed_epsilonRec, decayed_epsilonRec::Array{T},
vt1_diff_vth, vt1_diff_vth::Array{T},
vt1_diff_vth_div_vth, vt1_diff_vth_div_vth::Array{T},
gammaPd_div_vth, gammaPd_div_vth::Array{T},
phiActivation) phiActivation::Array{T},
) where T<:Number
# project 3D kfn zit into 4D lif zit # project 3D kfn zit into 4D lif zit
zit .= reshape(kfn_zit, zit .= reshape(kfn_zit,
@@ -190,40 +199,41 @@ function lifForward(kfn_zit,
end end
end end
function alifForward(kfn_zit, function alifForward(kfn_zit::Array{T},
zit, zit::Array{T},
wRec, wRec::Array{T},
vt0, vt0::Array{T},
vt1, vt1::Array{T},
vth, vth::Array{T},
vRest, vRest::Array{T},
zt1, zt1::Array{T},
alpha, alpha::Array{T},
phi, phi::Array{T},
epsilonRec, epsilonRec::Array{T},
refractoryCounter, refractoryCounter::Array{T},
refractoryDuration, refractoryDuration::Array{T},
gammaPd, gammaPd::Array{T},
firingCounter, firingCounter::Array{T},
arrayProjection3DTo4D, arrayProjection3DTo4D::Array{T},
recSignal, recSignal::Array{T},
decayed_vt0, decayed_vt0::Array{T},
decayed_epsilonRec, decayed_epsilonRec::Array{T},
vt1_diff_vth, vt1_diff_vth::Array{T},
vt1_diff_vth_div_vth, vt1_diff_vth_div_vth::Array{T},
gammaPd_div_vth, gammaPd_div_vth::Array{T},
phiActivation, phiActivation::Array{T},
epsilonRecA, epsilonRecA::Array{T},
avth, avth::Array{T},
a, a::Array{T},
beta, beta::Array{T},
rho, rho::Array{T},
phi_x_epsilonRec, phi_x_epsilonRec::Array{T},
phi_x_beta, phi_x_beta::Array{T},
rho_diff_phi_x_beta, rho_diff_phi_x_beta::Array{T},
rho_div_phi_x_beta_x_epsilonRecA, rho_div_phi_x_beta_x_epsilonRecA::Array{T},
beta_x_a) beta_x_a::Array{T},
) where T<:Number
# project 3D kfn zit into 4D lif zit # project 3D kfn zit into 4D lif zit
@@ -295,125 +305,69 @@ function alifForward(kfn_zit,
end end
end end
# function alifForward(kfn_zit, function onForward(kfn_zit::Array{T},
# zit, zit::Array{T},
# wRec, wOut::Array{T},
# vt0, vt0::Array{T},
# vt1, vt1::Array{T},
# vth, vth::Array{T},
# avth, vRest::Array{T},
# vRest, zt1::Array{T},
# zt1, alpha::Array{T},
# alpha, phi::Array{T},
# phi, epsilonRec::Array{T},
# epsilonRec, refractoryCounter::Array{T},
# epsilonRecA, refractoryDuration::Array{T},
# refractoryCounter, gammaPd::Array{T},
# refractoryDuration, firingCounter::Array{T},
# a, arrayProjection3DTo4D::Array{T},
# beta, recSignal::Array{T},
# rho, decayed_vt0::Array{T},
# gammaPd, decayed_epsilonRec::Array{T},
# firingCounter) vt1_diff_vth::Array{T},
# d1, d2, d3, d4 = size(wRec) vt1_diff_vth_div_vth::Array{T},
# zit .= reshape(kfn_zit, (d1, d2, 1, d4)) .* ones(size(wRec)...) # project zit into zit gammaPd_div_vth::Array{T},
phiActivation::Array{T},
) where T<:Number
# for j in 1:d4, i in 1:d3 # compute along neurons axis of every batch # project 3D kfn zit into 4D lif zit
# if view(refractoryCounter, :, :, i, j)[1] > 0 # refractory period is active zit .= reshape(kfn_zit,
# view(refractoryCounter, :, :, i, j)[1] -= 1 (size(wOut, 1), size(wOut, 2), 1, size(wOut, 4))) .* arrayProjection3DTo4D
# view(zt1, :, :, i, j)[1] = 0
# view(vt1, :, :, i, j)[1] = view(alpha, :, :, i, j)[1] *
# view(vt0, :, :, i, j)[1]
# view(phi, :, :, i, j)[1] = 0.0
# view(epsilonRec, :, :, i, j) .= view(alpha, :, :, i, j)[1] .*
# view(epsilonRec, :, :, i, j)
# view(a, :, :, i, j)[1] =
# (view(rho, :, :, i, j)[1] * view(a, :, :, i, j)[1]) + 0
# else # refractory period is inactive
# view(vt1, :, :, i, j)[1] =
# (view(alpha, :, :, i, j)[1] * view(vt0,:, :, i, j)[1]) +
# sum(view(zit, :, :, i, j) .* view(wRec, :, :, i, j))
# view(avth, :, :, i, j)[1] = view(vth, :, :, i, j)[1] +
# (view(beta, :, :, i, j)[1] * view(a, :, :, i, j)[1])
# if view(vt1, :, :, i, j)[1] > view(avth, :, :, i, j)[1]
# view(zt1, :, :, i, j)[1] = 1
# view(refractoryCounter, :, :, i, j)[1] =
# view(refractoryDuration, :, :, i, j)[1]
# view(firingCounter, :, :, i, j)[1] += 1
# view(vt1, :, :, i, j)[1] = view(vRest, :, :, i, j)[1]
# view(a, :, :, i, j)[1] = (view(rho, :, :, i, j)[1] *
# view(a, :, :, i, j)[1]) + 1
# else
# view(zt1, :, :, i, j)[1] = 0
# view(a, :, :, i, j)[1] =
# (view(rho, :, :, i, j)[1] * view(a, :, :, i, j)[1]) + 0
# end
# # there is a difference from alif formula for j in 1:size(wOut, 4), i in 1:size(wOut, 3) # compute along neurons axis of every batch
# view(phi, :, :, i, j)[1] = if sum(@view(refractoryCounter[:,:,i,j])) > 0 # refractory period is active
# (view(gammaPd, :, :, i, j)[1] / view(vth, :, :, i, j)[1]) * @. @views refractoryCounter[:,:,i,j] -= 1
# max(0, 1 - ((view(vt1, :, :, i, j)[1] - view(avth, :, :, i, j)[1]) / @. @views zt1[:,:,i,j] = 0
# view(vth, :, :, i, j)[1])) @. @views vt1[:,:,i,j] = alpha[:,:,i,j] * vt0[:,:,i,j]
# view(epsilonRec, :, :, i, j) .= @. @views phi[:,:,i,j] = 0
# (view(alpha, :, :, i, j) .* view(epsilonRec, :, :, i, j)) +
# view(zit, :, :, i, j)
# view(epsilonRecA, :, :, i, j) .=
# (view(phi, :, :, i, j)[1] .* view(epsilonRec, :, :, i, j)) +
# ((view(rho, :, :, i, j)[1] -
# (view(phi, :, :, i, j)[1] * view(beta, :, :, i, j)[1])) .*
# view(epsilonRecA, :, :, i, j))
# end
# end
# end
function onForward(kfn_zit, # compute epsilonRec
zit, @. @views decayed_epsilonRec[:,:,i,j] = alpha[:,:,i,j] * epsilonRec[:,:,i,j]
wOut, @. @views epsilonRec[:,:,i,j] = decayed_epsilonRec[:,:,i,j]
vt0, else # refractory period is inactive
vt1, @. @views recSignal[:,:,i,j] = zit[:,:,i,j] * wOut[:,:,i,j]
vth, @. @views decayed_vt0[:,:,i,j] = alpha[:,:,i,j] * vt0[:,:,i,j]
vRest, @view(vt1[:,:,i,j]) .= @view(decayed_vt0[:,:,i,j]) .+ sum(@view(recSignal[:,:,i,j]))
zt1,
alpha,
phi,
epsilonRec,
refractoryCounter,
refractoryDuration,
gammaPd,
firingCounter)
d1, d2, d3, d4 = size(wOut)
zit .= reshape(kfn_zit, (d1, d2, 1, d4)) .* ones(size(wOut)...) # project zit into zit
for j in 1:d4, i in 1:d3 # compute along neurons axis of every batch if sum(@view(vt1[:,:,i,j])) > sum(@view(vth[:,:,i,j]))
if view(refractoryCounter, :, :, i, j)[1] > 0 # neuron is inactive (in refractory period) @. @views zt1[:,:,i,j] = 1
view(refractoryCounter, :, :, i, j)[1] -= 1 @. @views refractoryCounter[:,:,i,j] = refractoryDuration[:,:,i,j]
view(zt1, :, :, i, j)[1] = 0 @. @views firingCounter[:,:,i,j] += 1
view(vt1, :, :, i, j)[1] = @. @views vt1[:,:,i,j] = vRest[:,:,i,j]
view(alpha, :, :, i, j)[1] * view(vt0, :, :, i, j)[1]
view(phi, :, :, i, j)[1] = 0.0
view(epsilonRec, :, :, i, j) .= view(alpha, :, :, i, j)[1] .*
view(epsilonRec, :, :, i, j)
else # neuron is active
view(vt1, :, :, i, j)[1] =
(view(alpha, :, :, i, j)[1] * view(vt0,:, :, i, j)[1]) +
sum(view(zit, :, :, i, j) .* view(wOut, :, :, i, j))
if view(vt1, :, :, i, j)[1] > view(vth, :, :, i, j)[1]
view(zt1, :, :, i, j)[1] = 1
view(refractoryCounter, :, :, i, j)[1] =
view(refractoryDuration, :, :, i, j)[1]
view(firingCounter, :, :, i, j)[1] += 1
view(vt1, :, :, i, j)[1] = view(vRest, :, :, i, j)[1]
else else
view(zt1, :, :, i, j)[1] = 0 @. @views zt1[:,:,i,j] = 0
end end
# there is a difference from alif formula
view(phi, :, :, i, j)[1] = # compute phi, there is a difference from alif formula
(view(gammaPd, :, :, i, j)[1] / view(vth, :, :, i, j)[1]) * @. @views gammaPd_div_vth[:,:,i,j] = gammaPd[:,:,i,j] / vth[:,:,i,j]
max(0, 1 - ((view(vt1, :, :, i, j)[1] - view(vth, :, :, i, j)[1]) / @. @views vt1_diff_vth[:,:,i,j] = vt1[:,:,i,j] - vth[:,:,i,j]
view(vth, :, :, i, j)[1])) @. @views vt1_diff_vth_div_vth[:,:,i,j] = vt1_diff_vth[:,:,i,j] / vth[:,:,i,j]
view(epsilonRec, :, :, i, j) .= @view(phiActivation[:,:,i,j]) .= max(0, 1 - sum(@view(vt1_diff_vth_div_vth[:,:,i,j])))
(view(alpha, :, :, i, j)[1] .* view(epsilonRec, :, :, i, j)) + @. @views phi[:,:,i,j] = gammaPd_div_vth[:,:,i,j] * phiActivation[:,:,i,j]
view(zit, :, :, i, j)
# compute epsilonRec
@. @views decayed_epsilonRec[:,:,i,j] = alpha[:,:,i,j] * epsilonRec[:,:,i,j]
@. @views epsilonRec[:,:,i,j] = decayed_epsilonRec[:,:,i,j] + zit[:,:,i,j]
end end
end end
end end

View File

@@ -118,7 +118,8 @@ Base.@kwdef mutable struct kfn_1 <: knowledgeFn
# output neuron is based on LIF # output neuron is based on LIF
on_zit::Union{AbstractArray, Nothing} = nothing on_zit::Union{AbstractArray, Nothing} = nothing
on_wOut::Union{AbstractArray, Nothing} = nothing # same as lif_wRec # main variables according to papers
on_wOut::Union{AbstractArray, Nothing} = nothing # wOut is wRec, just use the name from paper
on_vt0::Union{AbstractArray, Nothing} = nothing on_vt0::Union{AbstractArray, Nothing} = nothing
on_vt1::Union{AbstractArray, Nothing} = nothing on_vt1::Union{AbstractArray, Nothing} = nothing
on_vth::Union{AbstractArray, Nothing} = nothing on_vth::Union{AbstractArray, Nothing} = nothing
@@ -135,14 +136,19 @@ Base.@kwdef mutable struct kfn_1 <: knowledgeFn
on_eRec::Union{AbstractArray, Nothing} = nothing on_eRec::Union{AbstractArray, Nothing} = nothing
on_eta::Union{AbstractArray, Nothing} = nothing on_eta::Union{AbstractArray, Nothing} = nothing
on_gammaPd::Union{AbstractArray, Nothing} = nothing on_gammaPd::Union{AbstractArray, Nothing} = nothing
on_wOutChange::Union{AbstractArray, Nothing} = nothing on_wOutChange::Union{AbstractArray, Nothing} = nothing
on_b::Union{AbstractArray, Nothing} = nothing
on_bChange::Union{AbstractArray, Nothing} = nothing
on_firingCounter::Union{AbstractArray, Nothing} = nothing on_firingCounter::Union{AbstractArray, Nothing} = nothing
on_arraySize::Union{AbstractArray, Nothing} = nothing
# pre-allocation array
on_arrayProjection3DTo4D::Union{AbstractArray, Nothing} = nothing # use to project 3d array to 4d on_arrayProjection3DTo4D::Union{AbstractArray, Nothing} = nothing # use to project 3d array to 4d
on_recSignal::Union{AbstractArray, Nothing} = nothing
on_decayed_vt0::Union{AbstractArray, Nothing} = nothing
on_decayed_epsilonRec::Union{AbstractArray, Nothing} = nothing
on_vt1_diff_vth::Union{AbstractArray, Nothing} = nothing
on_vt1_diff_vth_div_vth::Union{AbstractArray, Nothing} = nothing
on_gammaPd_div_vth::Union{AbstractArray, Nothing} = nothing
on_phiActivation::Union{AbstractArray, Nothing} = nothing
end end
# outer constructor # outer constructor
@@ -214,7 +220,6 @@ function kfn_1(params::Dict; device=cpu)
kfn.lif_gammaPd_div_vth = similar(kfn.lif_vt0) .= 0 |> device kfn.lif_gammaPd_div_vth = similar(kfn.lif_vt0) .= 0 |> device
kfn.lif_phiActivation = similar(kfn.lif_vt0) .= 0 |> device kfn.lif_phiActivation = similar(kfn.lif_vt0) .= 0 |> device
# ---------------------------------------------------------------------------- # # ---------------------------------------------------------------------------- #
# ALIF config # # ALIF config #
# ---------------------------------------------------------------------------- # # ---------------------------------------------------------------------------- #
@@ -279,34 +284,10 @@ function kfn_1(params::Dict; device=cpu)
# output config # # output config #
# ---------------------------------------------------------------------------- # # ---------------------------------------------------------------------------- #
n = kfn.params[:outputPort][:numbers][1] * kfn.params[:outputPort][:numbers][2] n = kfn.params[:outputPort][:numbers][1] * kfn.params[:outputPort][:numbers][2]
kfn.on_zit = zeros(row, col, n, batch) |> device
kfn.on_vt0 = zeros(1, 1, n, batch) |> device
kfn.on_vt1 = zeros(1, 1, n, batch) |> device
kfn.on_vth = ones(1, 1, n, batch) |> device
kfn.on_vRest = zeros(1, 1, n, batch) |> device
# kfn.on_zt0 = zeros(1, 1, n, batch) |> device
kfn.on_zt1 = zeros(1, 1, n, batch) |> device
kfn.on_refractoryCounter = zeros(1, 1, n, batch) |> device
kfn.on_refractoryDuration = ones(1, 1, n, batch) .* 0 |> device
kfn.on_delta = 1.0
kfn.on_tau_m = 20.0
kfn.on_alpha = ones(1, 1, n, batch) .* (exp(-kfn.on_delta / kfn.on_tau_m)) |> device
kfn.on_phi = zeros(1, 1, n, batch) |> device
kfn.on_epsilonRec = zeros(row, col, n, batch) |> device
# kfn.on_eRec = zeros(row, col, n, batch)
kfn.on_eta = zeros(1, 1, n, batch) |> device
kfn.on_gammaPd = zeros(1, 1, n, batch) .* 0.3 |> device
kfn.on_wOutChange = zeros(row, col, n, batch) |> device
# kfn.on_b = randn(1, 1, n, batch) |> device
# kfn.on_bChange = randn(1, 1, n, batch) |> device
kfn.on_firingCounter = zeros(1, 1, n, batch) |> device
kfn.on_arraySize = [row, col, n, batch] |> device
kfn.on_arrayProjection3DTo4D = ones(row, col, n, batch) |> device
# subscription # subscription
w = zeros(row, col, n) w = zeros(row, col, n)
synapticConnectionPercent = kfn.params[:outputPort][:params][:synapticConnectionPercent] synapticConnectionPercent = kfn.params[:computeNeuron][:lif][:params][:synapticConnectionPercent]
synapticConnection = Int(floor(row*col * synapticConnectionPercent/100)) synapticConnection = Int(floor(row*col * synapticConnectionPercent/100))
for slice in eachslice(w, dims=3) for slice in eachslice(w, dims=3)
pool = shuffle!([1:row*col...])[1:synapticConnection] pool = shuffle!([1:row*col...])[1:synapticConnection]
@@ -314,8 +295,75 @@ function kfn_1(params::Dict; device=cpu)
slice[i] = randn()/10 # assign weight to synaptic connection slice[i] = randn()/10 # assign weight to synaptic connection
end end
end end
# project 3D w into 4D kfn.on_wOut # project 3D w into 4D kfn.lif_wOut (row, col, n, batch)
kfn.on_wOut = reshape(w, (row, col, n, 1)) .* ones(row, col, n, batch) |> device kfn.on_wOut = reshape(w, (row, col, n, 1)) .* ones(row, col, n, batch) |> device
kfn.on_zit = similar(kfn.on_wOut) .= 0 |> device
kfn.on_vt0 = zeros(1, 1, n, batch) |> device
kfn.on_vt1 = similar(kfn.on_vt0) .= 0 |> device
kfn.on_vth = similar(kfn.on_vt0) .= 1 |> device
kfn.on_vRest = similar(kfn.on_vt0) .= 0 |> device
kfn.on_zt0 = similar(kfn.on_vt0) .= 0 |> device
kfn.on_zt1 = similar(kfn.on_vt0) .= 0 |> device
kfn.on_refractoryCounter = similar(kfn.on_vt0) .= 0 |> device
kfn.on_refractoryDuration = similar(kfn.on_vt0) .= 0 |> device
kfn.on_delta = 1.0
kfn.on_tau_m = 20.0
kfn.on_alpha = similar(kfn.on_vt0) .= (exp(-kfn.on_delta / kfn.on_tau_m)) |> device
kfn.on_phi = similar(kfn.on_vt0) .= 0 |> device
kfn.on_epsilonRec = similar(kfn.on_wOut) .= 0 |> device
kfn.on_eRec = similar(kfn.on_wOut) .= 0 |> device
kfn.on_eta = similar(kfn.on_vt0) .= 0 |> device
kfn.on_gammaPd = similar(kfn.on_vt0) .= 0.3 |> device
kfn.on_wOutChange = similar(kfn.on_wOut) .= 0 |> device
kfn.on_firingCounter = similar(kfn.on_vt0) .= 0 |> device
kfn.on_arrayProjection3DTo4D = similar(kfn.on_wOut) .= 1 |> device
kfn.on_recSignal = similar(kfn.on_wOut) .= 0 |> device
kfn.on_decayed_vt0 = similar(kfn.on_vt0) .= 0 |> device
kfn.on_decayed_epsilonRec = similar(kfn.on_wOut) .= 0 |> device
kfn.on_vt1_diff_vth = similar(kfn.on_vt0) .= 0 |> device
kfn.on_vt1_diff_vth_div_vth = similar(kfn.on_vt0) .= 0 |> device
kfn.on_gammaPd_div_vth = similar(kfn.on_vt0) .= 0 |> device
kfn.on_phiActivation = similar(kfn.on_vt0) .= 0 |> device
# kfn.on_zit = zeros(row, col, n, batch) |> device
# kfn.on_vt0 = zeros(1, 1, n, batch) |> device
# kfn.on_vt1 = zeros(1, 1, n, batch) |> device
# kfn.on_vth = ones(1, 1, n, batch) |> device
# kfn.on_vRest = zeros(1, 1, n, batch) |> device
# # kfn.on_zt0 = zeros(1, 1, n, batch) |> device
# kfn.on_zt1 = zeros(1, 1, n, batch) |> device
# kfn.on_refractoryCounter = zeros(1, 1, n, batch) |> device
# kfn.on_refractoryDuration = ones(1, 1, n, batch) .* 0 |> device
# kfn.on_delta = 1.0
# kfn.on_tau_m = 20.0
# kfn.on_alpha = ones(1, 1, n, batch) .* (exp(-kfn.on_delta / kfn.on_tau_m)) |> device
# kfn.on_phi = zeros(1, 1, n, batch) |> device
# kfn.on_epsilonRec = zeros(row, col, n, batch) |> device
# # kfn.on_eRec = zeros(row, col, n, batch)
# kfn.on_eta = zeros(1, 1, n, batch) |> device
# kfn.on_gammaPd = zeros(1, 1, n, batch) .* 0.3 |> device
# kfn.on_wOutChange = zeros(row, col, n, batch) |> device
# # kfn.on_b = randn(1, 1, n, batch) |> device
# # kfn.on_bChange = randn(1, 1, n, batch) |> device
# kfn.on_firingCounter = zeros(1, 1, n, batch) |> device
# kfn.on_arraySize = [row, col, n, batch] |> device
# kfn.on_arrayProjection3DTo4D = ones(row, col, n, batch) |> device
# # subscription
# w = zeros(row, col, n)
# 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_wOut
# kfn.on_wOut = reshape(w, (row, col, n, 1)) .* ones(row, col, n, batch) |> device