building forwared()
This commit is contained in:
@@ -10,8 +10,14 @@ files and each file can only depend on the file included before it.
|
|||||||
include("type.jl")
|
include("type.jl")
|
||||||
using .type # bring model into this module namespace (this module is a parent module)
|
using .type # bring model into this module namespace (this module is a parent module)
|
||||||
|
|
||||||
include("snnUtils.jl")
|
include("snnUtil.jl")
|
||||||
using .snnUtils
|
using .snnUtil
|
||||||
|
|
||||||
|
include("forward.jl")
|
||||||
|
using .forward
|
||||||
|
|
||||||
|
include("learn.jl")
|
||||||
|
using .learn
|
||||||
|
|
||||||
include("interface.jl")
|
include("interface.jl")
|
||||||
using .interface
|
using .interface
|
||||||
@@ -19,7 +25,7 @@ using .interface
|
|||||||
|
|
||||||
#------------------------------------------------------------------------------------------------100
|
#------------------------------------------------------------------------------------------------100
|
||||||
|
|
||||||
"""
|
""" version 0.0.1
|
||||||
Todo:
|
Todo:
|
||||||
[*1] knowledgeFn in GPU format
|
[*1] knowledgeFn in GPU format
|
||||||
[] use partial error update for computeNeuron
|
[] use partial error update for computeNeuron
|
||||||
|
|||||||
@@ -2,10 +2,68 @@ module forward
|
|||||||
|
|
||||||
# export
|
# export
|
||||||
|
|
||||||
# using
|
using GeneralUtils
|
||||||
|
using ..type, ..snnUtil
|
||||||
|
|
||||||
#------------------------------------------------------------------------------------------------100
|
#------------------------------------------------------------------------------------------------100
|
||||||
|
|
||||||
|
# kfn forward
|
||||||
|
function (kfn::kfn_1)(input::AbstractArray)
|
||||||
|
kfn.timeStep .+= 1
|
||||||
|
|
||||||
|
#TODO time step forward
|
||||||
|
if kfn.learningStage == [1]
|
||||||
|
# reset learning params
|
||||||
|
end
|
||||||
|
|
||||||
|
println(">>> input ", size(input))
|
||||||
|
println(">>> z_i_t1 ", size(kfn.z_i_t1))
|
||||||
|
|
||||||
|
# pass input_data into input neuron.
|
||||||
|
GeneralUtils.cartesianAssign!(kfn.z_i_t1, input)
|
||||||
|
println(">>> z_i_t1 ", size(kfn.z_i_t1))
|
||||||
|
println(">>> lif_recSignal ", size(kfn.lif_recSignal))
|
||||||
|
println(">>> lif_w ", size(kfn.lif_w))
|
||||||
|
println(">>> lif_refractoryActive ", size(kfn.lif_refractoryActive))
|
||||||
|
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)
|
||||||
|
|
||||||
|
# LIF forward active neurons
|
||||||
|
kfn.lif_recSignal .= GeneralUtils.sumAlongDim3(
|
||||||
|
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
|
||||||
|
# for (i, v) in enumerate(kfn.lif_vt1)
|
||||||
|
# if v <
|
||||||
|
# LIF forward inactive neurons
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# GeneralUtils.batchMatEleMul(kfn.z_i_t1, kfn.alif_w, resultStorage=kfn.alif_recSignal)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
error("debug end kfn forward")
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
78
src/snnUtil.jl
Normal file
78
src/snnUtil.jl
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
module snnUtil
|
||||||
|
|
||||||
|
export refractoryStatus!
|
||||||
|
|
||||||
|
# using
|
||||||
|
|
||||||
|
#------------------------------------------------------------------------------------------------100
|
||||||
|
|
||||||
|
function refractoryStatus!(refractoryCounter, refractoryActive, refractoryInactive)
|
||||||
|
d1, d2, d3, d4 = size(refractoryCounter)
|
||||||
|
for j in 1:d4
|
||||||
|
for i in 1:d3
|
||||||
|
if refractoryCounter[1, 1, i, j] > 0 # inactive
|
||||||
|
view(refractoryActive, 1, 1, i, j) .= 0
|
||||||
|
view(refractoryInactive, 1, 1, i, j) .= 1
|
||||||
|
else
|
||||||
|
view(refractoryActive, 1, 1, i, j) .= 1
|
||||||
|
view(refractoryInactive, 1, 1, i, j) .= 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
end # module
|
||||||
@@ -1,73 +0,0 @@
|
|||||||
module snnUtils
|
|
||||||
|
|
||||||
# export
|
|
||||||
|
|
||||||
# using
|
|
||||||
|
|
||||||
#------------------------------------------------------------------------------------------------100
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
end # module
|
|
||||||
103
src/type.jl
103
src/type.jl
@@ -20,16 +20,35 @@ Base.@kwdef mutable struct kfn_1 <: knowledgeFn
|
|||||||
params::Dict = Dict() # store params of knowledgeFn itself for later use
|
params::Dict = Dict() # store params of knowledgeFn itself for later use
|
||||||
|
|
||||||
timeStep::AbstractArray = [0]
|
timeStep::AbstractArray = [0]
|
||||||
refractory::Union{AbstractArray, Nothing} = nothing
|
|
||||||
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_t1::Union{AbstractArray, Nothing} = nothing # 2D activation matrix
|
||||||
z_i_t0::Union{AbstractArray, Nothing} = nothing
|
z_i_t0::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_vt1::Union{AbstractArray, Nothing} = nothing
|
||||||
|
lif_vth::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_alpha::Union{AbstractArray, Nothing} = nothing
|
||||||
|
lif_delta::AbstractFloat = 1.0
|
||||||
|
lif_tau_m::AbstractFloat = 20.0
|
||||||
|
|
||||||
|
# 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_zt1::Union{AbstractArray, Nothing} = nothing
|
||||||
|
alif_refractoryCounter::Union{AbstractArray, Nothing} = nothing
|
||||||
|
alif_refractoryActive::Union{AbstractArray, Nothing} = nothing
|
||||||
|
alif_refractoryInactive::Union{AbstractArray, Nothing} = nothing
|
||||||
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# outer constructor
|
# outer constructor
|
||||||
@@ -43,20 +62,25 @@ function kfn_1(params::Dict)
|
|||||||
col += kfn.params[:computeNeuron][:lif][:numbers][2]
|
col += kfn.params[:computeNeuron][:lif][:numbers][2]
|
||||||
col += kfn.params[:computeNeuron][:alif][:numbers][2]
|
col += kfn.params[:computeNeuron][:alif][:numbers][2]
|
||||||
|
|
||||||
kfn.z_i_t1 = zeros(row, col, batch)
|
# activation matrix
|
||||||
kfn.z_i_t0 = zeros(row, col, batch)
|
kfn.z_i_t0 = zeros(row, col, batch)
|
||||||
|
kfn.z_i_t1 = zeros(row, col, batch)
|
||||||
|
|
||||||
# LIF
|
# LIF
|
||||||
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_w = zeros(row, col, z) # matrix z-axis represent each neurons
|
||||||
kfn.lif_recSignal = 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_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_alpha = ones(1, 1, z, batch) .* (exp(-kfn.lif_delta / kfn.lif_tau_m))
|
||||||
|
|
||||||
# ALIF
|
# subscription
|
||||||
z = kfn.params[:computeNeuron][:alif][:numbers][1] * kfn.params[:computeNeuron][:alif][:numbers][2]
|
|
||||||
kfn.alif_w = zeros(row, col, z)
|
|
||||||
kfn.alif_recSignal = zeros(row, col, z, batch)
|
|
||||||
|
|
||||||
# lif subscription
|
|
||||||
row, col, _ = size(kfn.lif_w) # row*col is synaptic subscribe weight for each neuron in z-axis
|
row, col, _ = size(kfn.lif_w) # row*col is synaptic subscribe weight for each neuron in z-axis
|
||||||
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))
|
||||||
@@ -67,7 +91,17 @@ function kfn_1(params::Dict)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# alif subscription
|
# ALIF
|
||||||
|
z = kfn.params[:computeNeuron][:alif][:numbers][1] * kfn.params[:computeNeuron][:alif][:numbers][2]
|
||||||
|
kfn.alif_w = zeros(row, col, z)
|
||||||
|
kfn.alif_recSignal = zeros(1, 1, z, batch)
|
||||||
|
kfn.alif_zt0 = zeros(1, 1, z, batch)
|
||||||
|
kfn.alif_zt1 = zeros(1, 1, z, batch)
|
||||||
|
kfn.alif_refractoryCounter = zeros(1, 1, z, batch)
|
||||||
|
kfn.alif_refractoryActive = zeros(1, 1, z, batch)
|
||||||
|
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
|
row, col, _ = size(kfn.alif_w) # row*col is synaptic subscribe weight for each neuron in z-axis
|
||||||
synapticConnectionPercent = kfn.params[:computeNeuron][:alif][:params][:synapticConnectionPercent]
|
synapticConnectionPercent = kfn.params[:computeNeuron][:alif][:params][:synapticConnectionPercent]
|
||||||
synapticConnection = Int(floor(row*col * synapticConnectionPercent/100))
|
synapticConnection = Int(floor(row*col * synapticConnectionPercent/100))
|
||||||
@@ -92,53 +126,6 @@ function kfn_1(params::Dict)
|
|||||||
return kfn
|
return kfn
|
||||||
end
|
end
|
||||||
|
|
||||||
# kfn forward
|
|
||||||
function (kfn::kfn_1)(input::AbstractArray)
|
|
||||||
kfn.timeStep .+= 1
|
|
||||||
|
|
||||||
# time step forward
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# row, col = size(input) # if input is a 2D matrix
|
|
||||||
println(">>> 1 ", size(input))
|
|
||||||
println(">>> 2 ", size(kfn.z_i_t1))
|
|
||||||
|
|
||||||
# multiply input with kfn.z_i_t1 may be using cartesian coordinates
|
|
||||||
GeneralUtils.cartesianAssign!(kfn.z_i_t1, input)
|
|
||||||
println(">>> 3 ", sum(kfn.z_i_t1))
|
|
||||||
println(">>> 4 ", size(kfn.lif_recSignal))
|
|
||||||
println(">>> 5 ", size(kfn.lif_w))
|
|
||||||
kfn.lif_recSignal .= GeneralUtils.batchMatEleMul(kfn.z_i_t1, kfn.lif_w)
|
|
||||||
kfn.alif_recSignal .= GeneralUtils.batchMatEleMul(kfn.z_i_t1, kfn.alif_w)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
error("debug end kfn forward")
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user