lif forward
This commit is contained in:
141
src/forward.jl
141
src/forward.jl
@@ -17,91 +17,114 @@ function (kfn::kfn_1)(input::AbstractArray)
|
||||
end
|
||||
|
||||
println(">>> input ", size(input))
|
||||
|
||||
# pass input_data into input neuron.
|
||||
GeneralUtils.cartesianAssign!(kfn.z_i_t, input)
|
||||
kfn.lif_z_i_t = GeneralUtils.matMul_3Dto4D_batchwise(kfn.z_i_t,
|
||||
ones(size(kfn.z_i_t)[1], size(kfn.z_i_t)[2], size(kfn.lif_w)[3], size(kfn.z_i_t)[3]))
|
||||
|
||||
println(">>> z_i_t ", size(kfn.z_i_t))
|
||||
println(">>> lif_z_i_t ", size(kfn.lif_z_i_t))
|
||||
println(">>> lif_recSignal ", size(kfn.lif_recSignal))
|
||||
println(">>> lif_w ", size(kfn.lif_w))
|
||||
println(">>> lif_refractoryActive ", size(kfn.lif_refractoryCounter))
|
||||
println(">>> zit ", size(kfn.zit))
|
||||
# println(">>> lif_zit ", size(kfn.lif_zit))
|
||||
# println(">>> lif_recSignal ", size(kfn.lif_recSignal))
|
||||
println(">>> lif_wRec ", size(kfn.lif_wRec))
|
||||
println(">>> lif_refractoryCounter ", size(kfn.lif_refractoryCounter))
|
||||
println(">>> lif_alpha ", size(kfn.lif_alpha))
|
||||
println(">>> lif_vt0 ", size(kfn.lif_vt0))
|
||||
println(">>> lif_vt0 sum ", sum(kfn.lif_vt0))
|
||||
|
||||
# check active/inactive neurons
|
||||
refractoryStatus!(kfn.lif_refractoryCounter, kfn.lif_refractoryActive, kfn.lif_refractoryInactive)
|
||||
refractoryStatus!(kfn.alif_refractoryCounter, kfn.alif_refractoryActive, kfn.alif_refractoryInactive)
|
||||
|
||||
# pass input_data into input neuron.
|
||||
s1, s2, s3 = size(input)
|
||||
GeneralUtils.cartesianAssign!(kfn.zit, reshape(input, (s1, s2, 1, s3)))
|
||||
|
||||
#WORKING LIF forward active neurons
|
||||
# a = kfn.lif_refractoryActive .* kfn.lif_w
|
||||
# lifForward.(kfn.lif_refractoryCounter, kfn.z_i_t0, kfn.z_i_t1,
|
||||
lifForward( kfn.zit,
|
||||
kfn.lif_zit,
|
||||
kfn.lif_wRec,
|
||||
kfn.lif_vt0,
|
||||
kfn.lif_vt1,
|
||||
kfn.lif_vth,
|
||||
kfn.lif_vRest,
|
||||
kfn.lif_zt1,
|
||||
kfn.lif_alpha,
|
||||
kfn.lif_phi,
|
||||
kfn.lif_epsilonRec,
|
||||
kfn.lif_refractoryCounter,
|
||||
kfn.lif_refractoryDuration,)
|
||||
|
||||
|
||||
|
||||
|
||||
error("debug end kfn forward")
|
||||
|
||||
|
||||
# kfn.lif_zit = GeneralUtils.matMul_3Dto4D_batchwise(kfn.zit,
|
||||
# ones(size(kfn.zit)[1], size(kfn.zit)[2], size(kfn.lif_wRec)[3], size(kfn.zit)[3]))
|
||||
|
||||
|
||||
|
||||
# check active/inactive neurons
|
||||
# refractoryStatus!(kfn.lif_refractoryCounter, kfn.lif_refractoryActive, kfn.lif_refractoryInactive)
|
||||
# refractoryStatus!(kfn.alif_refractoryCounter, kfn.alif_refractoryActive, kfn.alif_refractoryInactive)
|
||||
|
||||
|
||||
# a = kfn.lif_refractoryActive .* kfn.lif_wRec
|
||||
# lifForward.(kfn.lif_refractoryCounter, kfn.zit0, kfn.zit1,
|
||||
# kfn.lif_vt0, kfn.lif_vt1, kfn.lif_alpha, kfn.lif_recSignal)
|
||||
|
||||
# kfn.lif_recSignal .= GeneralUtils.sumAlongDim3(
|
||||
# GeneralUtils.matMul_3Dto4D_batchwise(kfn.z_i_t1, kfn.lif_refractoryActive .* kfn.lif_w))
|
||||
# GeneralUtils.matMul_3Dto4D_batchwise(kfn.zit1, kfn.lif_refractoryActive .* kfn.lif_wRec))
|
||||
# kfn.lif_vt1 = (kfn.lif_alpha .* kfn.lif_vt0) .+ kfn.lif_recSignal
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# GeneralUtils.batchMatEleMul(kfn.z_i_t1, kfn.alif_w, resultStorage=kfn.alif_recSignal)
|
||||
# GeneralUtils.batchMatEleMul(kfn.zit1, kfn.alif_wRec, resultStorage=kfn.alif_recSignal)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
error("debug end kfn forward")
|
||||
|
||||
end
|
||||
|
||||
|
||||
function lifForward(lif_refractoryCounter, z_i_t0, z_i_t1, lif_w, lif_vt0, lif_vt1, lif_alpha,
|
||||
lif_recSignal)
|
||||
error("debug end LIF forward")
|
||||
|
||||
|
||||
|
||||
# if n.refractoryCounter != 0
|
||||
# n.refractoryCounter -= 1
|
||||
function lifForward(zit,
|
||||
lif_zit,
|
||||
lif_wRec,
|
||||
lif_vt0,
|
||||
lif_vt1,
|
||||
lif_vth,
|
||||
lif_vRest,
|
||||
lif_zt1,
|
||||
lif_alpha,
|
||||
lif_phi,
|
||||
lif_epsilonRec,
|
||||
lif_refractoryCounter,
|
||||
lif_refractoryDuration,)
|
||||
_, _, d3, d4 = size(lif_wRec)
|
||||
lif_zit .= zit .* ones(size(lif_wRec)...) # project zit into lif_zit
|
||||
|
||||
# # neuron is in refractory state, skip all calculation
|
||||
# n.z_t1 = false # used by timestep_forward() in kfn. Set to zero because neuron spike
|
||||
# # last only 1 timestep follow by a period of refractory.
|
||||
# n.recSignal = n.recSignal * 0.0
|
||||
|
||||
# # decay of v_t1
|
||||
# n.v_t1 = n.alpha * n.v_t
|
||||
for j in 1:d4, i in 1:d3 # compute along neurons axis of every batch
|
||||
if view(lif_refractoryCounter, :, :, i, j)[1] > 0 # refractory period is active
|
||||
view(lif_refractoryCounter, :, :, i, j)[1] -= 1
|
||||
view(lif_zt1, :, :, i, j)[1] = 0
|
||||
view(lif_vt1, :, :, i, j)[1] = view(lif_alpha, :, :, i, j)[1] * view(lif_vt0, :, :, i, j)[1]
|
||||
view(lif_phi, :, :, i, j)[1] = 0.0
|
||||
view(lif_epsilonRec, :, :, i, j) .= view(lif_alpha, :, :, i, j)[1] .*
|
||||
view(lif_epsilonRec, :, :, i, j)
|
||||
else # refractory period is inactive
|
||||
view(lif_vt1, :, :, i, j)[1] =
|
||||
(view(lif_alpha, :, :, i, j)[1] * view(lif_vt0,:, :, i, j)[1]) +
|
||||
sum(view(lif_zit, :, :, i, j) .* view(lif_wRec, :, :, i, j))
|
||||
if view(lif_vt1, :, :, i, j)[1] > view(lif_vth, :, :, i, j)[1]
|
||||
view(lif_zt1, :, :, i, j)[1] = 1
|
||||
view(lif_refractoryCounter, :, :, i, j)[1] = view(lif_refractoryDuration, :, :, i, j)[1]
|
||||
view(lif_firingCounter, :, :, i, j)[1] += 1
|
||||
view(lif_vt1, :, :, i, j)[1] = view(lif_vRest, :, :, i, j)[1]
|
||||
else
|
||||
view(lif_zt1, :, :, i, j)[1] = 0
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# n.phi = 0.0
|
||||
# n.decayedEpsilonRec = n.alpha * n.epsilonRec
|
||||
# n.epsilonRec = n.decayedEpsilonRec
|
||||
# else
|
||||
# n.recSignal = sum(n.wRec .* n.z_i_t) # signal from other neuron that this neuron subscribed
|
||||
|
||||
# # computeAlpha!(n)
|
||||
# n.alpha_v_t = n.alpha * n.v_t
|
||||
# n.v_t1 = n.alpha_v_t + n.recSignal
|
||||
# # n.v_t1 = no_negative!(n.v_t1)
|
||||
|
||||
# if n.v_t1 > n.v_th
|
||||
# n.z_t1 = true
|
||||
# n.refractoryCounter = n.refractoryDuration
|
||||
# n.firingCounter += 1
|
||||
# n.v_t1 = n.vRest
|
||||
# else
|
||||
# n.z_t1 = false
|
||||
# end
|
||||
|
||||
# # there is a difference from alif formula
|
||||
# n.phi = (n.gammaPd / n.v_th) * max(0, 1 - (n.v_t1 - n.v_th) / n.v_th)
|
||||
# n.decayedEpsilonRec = n.alpha * n.epsilonRec
|
||||
# n.epsilonRec = n.decayedEpsilonRec + n.z_i_t
|
||||
# end
|
||||
error("debug end -> LIF forward")
|
||||
end
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user