building knowledgeFn in GPU format
This commit is contained in:
@@ -17,14 +17,17 @@ function (kfn::kfn_1)(input::AbstractArray)
|
|||||||
end
|
end
|
||||||
|
|
||||||
println(">>> input ", size(input))
|
println(">>> input ", size(input))
|
||||||
println(">>> z_i_t1 ", size(kfn.z_i_t1))
|
|
||||||
|
|
||||||
# pass input_data into input neuron.
|
# pass input_data into input neuron.
|
||||||
GeneralUtils.cartesianAssign!(kfn.z_i_t1, input)
|
GeneralUtils.cartesianAssign!(kfn.z_i_t, input)
|
||||||
println(">>> z_i_t1 ", size(kfn.z_i_t1))
|
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_recSignal ", size(kfn.lif_recSignal))
|
||||||
println(">>> lif_w ", size(kfn.lif_w))
|
println(">>> lif_w ", size(kfn.lif_w))
|
||||||
println(">>> lif_refractoryActive ", size(kfn.lif_refractoryActive))
|
println(">>> lif_refractoryActive ", size(kfn.lif_refractoryCounter))
|
||||||
println(">>> lif_alpha ", size(kfn.lif_alpha))
|
println(">>> lif_alpha ", size(kfn.lif_alpha))
|
||||||
println(">>> lif_vt0 ", size(kfn.lif_vt0))
|
println(">>> lif_vt0 ", size(kfn.lif_vt0))
|
||||||
println(">>> lif_vt0 sum ", sum(kfn.lif_vt0))
|
println(">>> lif_vt0 sum ", sum(kfn.lif_vt0))
|
||||||
@@ -33,13 +36,14 @@ function (kfn::kfn_1)(input::AbstractArray)
|
|||||||
refractoryStatus!(kfn.lif_refractoryCounter, kfn.lif_refractoryActive, kfn.lif_refractoryInactive)
|
refractoryStatus!(kfn.lif_refractoryCounter, kfn.lif_refractoryActive, kfn.lif_refractoryInactive)
|
||||||
refractoryStatus!(kfn.alif_refractoryCounter, kfn.alif_refractoryActive, kfn.alif_refractoryInactive)
|
refractoryStatus!(kfn.alif_refractoryCounter, kfn.alif_refractoryActive, kfn.alif_refractoryInactive)
|
||||||
|
|
||||||
# LIF forward active neurons
|
#WORKING LIF forward active neurons
|
||||||
kfn.lif_recSignal .= GeneralUtils.sumAlongDim3(
|
# a = kfn.lif_refractoryActive .* kfn.lif_w
|
||||||
GeneralUtils.matMul_3Dto4D_batchwise(kfn.z_i_t1, kfn.lif_refractoryActive .* kfn.lif_w))
|
# lifForward.(kfn.lif_refractoryCounter, kfn.z_i_t0, kfn.z_i_t1,
|
||||||
kfn.lif_vt1 = (kfn.lif_alpha .* kfn.lif_vt0) .+ kfn.lif_recSignal
|
# kfn.lif_vt0, kfn.lif_vt1, kfn.lif_alpha, kfn.lif_recSignal)
|
||||||
# for (i, v) in enumerate(kfn.lif_vt1)
|
|
||||||
# if v <
|
# kfn.lif_recSignal .= GeneralUtils.sumAlongDim3(
|
||||||
# LIF forward inactive neurons
|
# GeneralUtils.matMul_3Dto4D_batchwise(kfn.z_i_t1, kfn.lif_refractoryActive .* kfn.lif_w))
|
||||||
|
# kfn.lif_vt1 = (kfn.lif_alpha .* kfn.lif_vt0) .+ kfn.lif_recSignal
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -52,19 +56,53 @@ function (kfn::kfn_1)(input::AbstractArray)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
error("debug end kfn forward")
|
error("debug end kfn forward")
|
||||||
end
|
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
|
||||||
|
|
||||||
|
# # 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
|
||||||
|
|
||||||
|
# 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
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ function refractoryStatus!(refractoryCounter, refractoryActive, refractoryInacti
|
|||||||
if refractoryCounter[1, 1, i, j] > 0 # inactive
|
if refractoryCounter[1, 1, i, j] > 0 # inactive
|
||||||
view(refractoryActive, 1, 1, i, j) .= 0
|
view(refractoryActive, 1, 1, i, j) .= 0
|
||||||
view(refractoryInactive, 1, 1, i, j) .= 1
|
view(refractoryInactive, 1, 1, i, j) .= 1
|
||||||
else
|
else # active
|
||||||
view(refractoryActive, 1, 1, i, j) .= 1
|
view(refractoryActive, 1, 1, i, j) .= 1
|
||||||
view(refractoryInactive, 1, 1, i, j) .= 0
|
view(refractoryInactive, 1, 1, i, j) .= 0
|
||||||
end
|
end
|
||||||
|
|||||||
38
src/type.jl
38
src/type.jl
@@ -21,10 +21,14 @@ Base.@kwdef mutable struct kfn_1 <: knowledgeFn
|
|||||||
|
|
||||||
timeStep::AbstractArray = [0]
|
timeStep::AbstractArray = [0]
|
||||||
learningStage::AbstractArray = [0] # 0 inference, 1 start, 2 during, 3 end learning
|
learningStage::AbstractArray = [0] # 0 inference, 1 start, 2 during, 3 end learning
|
||||||
z_i_t1::Union{AbstractArray, Nothing} = nothing # 2D activation matrix
|
z_i_t::Union{AbstractArray, Nothing} = nothing # 3D activation matrix
|
||||||
z_i_t0::Union{AbstractArray, Nothing} = nothing
|
|
||||||
|
# ---------------------------------------------------------------------------- #
|
||||||
|
# LIF #
|
||||||
|
# ---------------------------------------------------------------------------- #
|
||||||
|
# a projection of kfn.z_i_t into lif dimension for broadcasting later)
|
||||||
|
lif_z_i_t::Union{AbstractArray, Nothing} = nothing
|
||||||
|
|
||||||
# LIF
|
|
||||||
lif_w::Union{AbstractArray, Nothing} = nothing
|
lif_w::Union{AbstractArray, Nothing} = nothing
|
||||||
lif_recSignal::Union{AbstractArray, Nothing} = nothing
|
lif_recSignal::Union{AbstractArray, Nothing} = nothing
|
||||||
lif_vt0::Union{AbstractArray, Nothing} = nothing
|
lif_vt0::Union{AbstractArray, Nothing} = nothing
|
||||||
@@ -39,7 +43,9 @@ Base.@kwdef mutable struct kfn_1 <: knowledgeFn
|
|||||||
lif_delta::AbstractFloat = 1.0
|
lif_delta::AbstractFloat = 1.0
|
||||||
lif_tau_m::AbstractFloat = 20.0
|
lif_tau_m::AbstractFloat = 20.0
|
||||||
|
|
||||||
# ALIF
|
# ---------------------------------------------------------------------------- #
|
||||||
|
# ALIF #
|
||||||
|
# ---------------------------------------------------------------------------- #
|
||||||
alif_w::Union{AbstractArray, Nothing} = nothing
|
alif_w::Union{AbstractArray, Nothing} = nothing
|
||||||
alif_recSignal::Union{AbstractArray, Nothing} = nothing
|
alif_recSignal::Union{AbstractArray, Nothing} = nothing
|
||||||
alif_zt0::Union{AbstractArray, Nothing} = nothing
|
alif_zt0::Union{AbstractArray, Nothing} = nothing
|
||||||
@@ -55,7 +61,8 @@ end
|
|||||||
function kfn_1(params::Dict)
|
function kfn_1(params::Dict)
|
||||||
kfn = kfn_1()
|
kfn = kfn_1()
|
||||||
kfn.params = params
|
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, col, batch = kfn.params[:inputPort][:signal][:numbers] # z-axis represent signal batch number
|
||||||
row += kfn.params[:inputPort][:noise][:numbers][1]
|
row += kfn.params[:inputPort][:noise][:numbers][1]
|
||||||
col += kfn.params[:inputPort][:signal][:numbers][2]
|
col += kfn.params[:inputPort][:signal][:numbers][2]
|
||||||
@@ -63,12 +70,13 @@ function kfn_1(params::Dict)
|
|||||||
col += kfn.params[:computeNeuron][:alif][:numbers][2]
|
col += kfn.params[:computeNeuron][:alif][:numbers][2]
|
||||||
|
|
||||||
# activation matrix
|
# activation matrix
|
||||||
kfn.z_i_t0 = zeros(row, col, batch)
|
kfn.z_i_t = zeros(row, col, batch)
|
||||||
kfn.z_i_t1 = zeros(row, col, batch)
|
|
||||||
|
|
||||||
# LIF
|
# -------------------------------- 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]
|
z = kfn.params[:computeNeuron][:lif][:numbers][1] * kfn.params[:computeNeuron][:lif][:numbers][2]
|
||||||
kfn.lif_w = zeros(row, col, z) # matrix z-axis represent each neurons
|
|
||||||
kfn.lif_recSignal = zeros(1, 1, z, batch)
|
kfn.lif_recSignal = zeros(1, 1, z, batch)
|
||||||
kfn.lif_vt0 = zeros(1, 1, z, batch)
|
kfn.lif_vt0 = zeros(1, 1, z, batch)
|
||||||
kfn.lif_vt1 = zeros(1, 1, z, batch)
|
kfn.lif_vt1 = zeros(1, 1, z, batch)
|
||||||
@@ -81,19 +89,23 @@ function kfn_1(params::Dict)
|
|||||||
kfn.lif_alpha = ones(1, 1, z, batch) .* (exp(-kfn.lif_delta / kfn.lif_tau_m))
|
kfn.lif_alpha = ones(1, 1, z, batch) .* (exp(-kfn.lif_delta / kfn.lif_tau_m))
|
||||||
|
|
||||||
# subscription
|
# subscription
|
||||||
row, col, _ = size(kfn.lif_w) # row*col is synaptic subscribe weight for each neuron in z-axis
|
w = zeros(row, col, z)
|
||||||
synapticConnectionPercent = kfn.params[:computeNeuron][:lif][: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(kfn.lif_w, dims=3)
|
for slice in eachslice(w, dims=3)
|
||||||
pool = shuffle!([1:row*col...])[1:synapticConnection]
|
pool = shuffle!([1:row*col...])[1:synapticConnection]
|
||||||
for i in pool
|
for i in pool
|
||||||
slice[i] = randn()/10
|
slice[i] = randn()/10 # assign weight to synaptic connection
|
||||||
end
|
end
|
||||||
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")
|
||||||
|
|
||||||
# ALIF
|
# ALIF
|
||||||
z = kfn.params[:computeNeuron][:alif][:numbers][1] * kfn.params[:computeNeuron][:alif][:numbers][2]
|
z = kfn.params[:computeNeuron][:alif][:numbers][1] * kfn.params[:computeNeuron][:alif][:numbers][2]
|
||||||
kfn.alif_w = zeros(row, col, z)
|
kfn.alif_w = zeros(row, col, z) # matrix z-axis represent each neurons
|
||||||
kfn.alif_recSignal = zeros(1, 1, z, batch)
|
kfn.alif_recSignal = zeros(1, 1, z, batch)
|
||||||
kfn.alif_zt0 = zeros(1, 1, z, batch)
|
kfn.alif_zt0 = zeros(1, 1, z, batch)
|
||||||
kfn.alif_zt1 = zeros(1, 1, z, batch)
|
kfn.alif_zt1 = zeros(1, 1, z, batch)
|
||||||
|
|||||||
Reference in New Issue
Block a user