version 0.01
This commit is contained in:
86
previousVersion/0.0.1/src/IronpenGPU.jl
Normal file
86
previousVersion/0.0.1/src/IronpenGPU.jl
Normal file
@@ -0,0 +1,86 @@
|
||||
module IronpenGPU # this is a parent module
|
||||
|
||||
# export
|
||||
|
||||
|
||||
""" Order by dependencies of each file. The 1st included file must not depend on any other
|
||||
files and each file can only depend on the file included before it.
|
||||
"""
|
||||
|
||||
include("type.jl")
|
||||
using .type # bring type into parent module namespace
|
||||
|
||||
include("snnUtil.jl")
|
||||
using .snnUtil
|
||||
|
||||
include("forward.jl")
|
||||
using .forward
|
||||
|
||||
include("learn.jl")
|
||||
using .learn
|
||||
|
||||
include("interface.jl")
|
||||
using .interface
|
||||
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
""" version 0.0.1
|
||||
Todo:
|
||||
[*1] knowledgeFn in GPU format
|
||||
[] use partial error update for computeNeuron
|
||||
[] use integrate_neuron_params synapticConnectionPercent LESS THAN 100%
|
||||
[2] implement dormant connection and pruning machanism. the longer the training the longer
|
||||
0 weight stay 0.
|
||||
[] using RL to control learning signal
|
||||
[] consider using Dates.now() instead of timestamp because time_stamp may overflow
|
||||
[] Liquid time constant. training should include adjusting α, neuron membrane potential decay factor
|
||||
which defined by neuron.tau_m formula in type.jl
|
||||
|
||||
Change from version:
|
||||
-
|
||||
|
||||
All features
|
||||
|
||||
"""
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
end # module IronpenGPU
|
||||
391
previousVersion/0.0.1/src/forward.jl
Normal file
391
previousVersion/0.0.1/src/forward.jl
Normal file
@@ -0,0 +1,391 @@
|
||||
module forward
|
||||
|
||||
# export
|
||||
|
||||
using Flux, CUDA
|
||||
using GeneralUtils
|
||||
using ..type, ..snnUtil
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
""" kfn forward
|
||||
input (row, col, batch)
|
||||
"""
|
||||
function (kfn::kfn_1)(input::AbstractArray)
|
||||
|
||||
kfn.timeStep .+= 1
|
||||
|
||||
#TODO time step forward
|
||||
if view(kfn.learningStage, 1)[1] == 1
|
||||
# reset learning params
|
||||
# kfn.learningStage = [2]
|
||||
end
|
||||
|
||||
# println(">>> input ", size(input))
|
||||
# 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))
|
||||
|
||||
# pass input_data into input neuron.
|
||||
GeneralUtils.cartesianAssign!(kfn.zit, input)
|
||||
|
||||
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,
|
||||
kfn.lif_gammaPd,
|
||||
kfn.lif_firingCounter,
|
||||
kfn.lif_arraySize,
|
||||
kfn.lif_arrayProjection3DTo4D)
|
||||
|
||||
alifForward( kfn.zit,
|
||||
kfn.alif_zit,
|
||||
kfn.alif_wRec,
|
||||
kfn.alif_vt0,
|
||||
kfn.alif_vt1,
|
||||
kfn.alif_vth,
|
||||
kfn.alif_avth,
|
||||
kfn.alif_vRest,
|
||||
kfn.alif_zt1,
|
||||
kfn.alif_alpha,
|
||||
kfn.alif_phi,
|
||||
kfn.alif_epsilonRec,
|
||||
kfn.alif_epsilonRecA,
|
||||
kfn.alif_refractoryCounter,
|
||||
kfn.alif_refractoryDuration,
|
||||
kfn.alif_a,
|
||||
kfn.alif_beta,
|
||||
kfn.alif_rho,
|
||||
kfn.alif_gammaPd,
|
||||
kfn.alif_firingCounter)
|
||||
# error("DEBUG -> kfn forward")
|
||||
|
||||
|
||||
|
||||
# update activation matrix by concatenate (input, lif_zt1, alif_zt1) to form activation matrix
|
||||
_zit = cat(reshape(input, (size(input, 1), size(input, 2), 1, size(input, 3))),
|
||||
reshape(kfn.lif_zt1, (size(input, 1), :, 1, size(input, 3))),
|
||||
reshape(kfn.alif_zt1, (size(input, 1), :, 1, size(input, 3))), dims=2)
|
||||
kfn.zit .= reshape(_zit, (size(input, 1), :, size(input, 3)))
|
||||
|
||||
# read out
|
||||
onForward( kfn.zit,
|
||||
kfn.on_zit,
|
||||
kfn.on_wOut,
|
||||
kfn.on_vt0,
|
||||
kfn.on_vt1,
|
||||
kfn.on_vth,
|
||||
kfn.on_vRest,
|
||||
kfn.on_zt1,
|
||||
kfn.on_alpha,
|
||||
kfn.on_phi,
|
||||
kfn.on_epsilonRec,
|
||||
kfn.on_refractoryCounter,
|
||||
kfn.on_refractoryDuration,
|
||||
kfn.on_gammaPd,
|
||||
kfn.on_firingCounter)
|
||||
|
||||
return reshape(kfn.on_zt1, (size(input, 1), :)),
|
||||
kfn.zit
|
||||
end
|
||||
|
||||
function lifForward(kfn_zit,
|
||||
zit,
|
||||
wRec,
|
||||
vt0,
|
||||
vt1,
|
||||
vth,
|
||||
vRest,
|
||||
zt1,
|
||||
alpha,
|
||||
phi,
|
||||
epsilonRec,
|
||||
refractoryCounter,
|
||||
refractoryDuration,
|
||||
gammaPd,
|
||||
firingCounter,
|
||||
arraySize,
|
||||
arrayProjection3DTo4D)
|
||||
|
||||
# project 3D kfn zit into 4D lif zit
|
||||
zit .= reshape(kfn_zit,
|
||||
(view(arraySize, 1)[1], view(arraySize, 2)[1], 1, view(arraySize, 4)[1])) .*
|
||||
arrayProjection3DTo4D
|
||||
# error("DEBUG -> lif forward") #WORKING
|
||||
for j in 1:size(wRec, 4), i in 1:size(wRec, 3) # compute along neurons axis of every batch
|
||||
if view(refractoryCounter, :, :, i, j)[1] > 0 # refractory period is active
|
||||
view(refractoryCounter, :, :, i, j)[1] -= 1
|
||||
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)
|
||||
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))
|
||||
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
|
||||
view(zt1, :, :, i, j)[1] = 0
|
||||
end
|
||||
# there is a difference from alif formula
|
||||
view(phi, :, :, i, j)[1] =
|
||||
(view(gammaPd, :, :, i, j)[1] / view(vth, :, :, i, j)[1]) *
|
||||
max(0, 1 - ((view(vt1, :, :, i, j)[1] - view(vth, :, :, i, j)[1]) /
|
||||
view(vth, :, :, i, j)[1]))
|
||||
view(epsilonRec, :, :, i, j) .=
|
||||
(view(alpha, :, :, i, j)[1] .* view(epsilonRec, :, :, i, j)) +
|
||||
view(zit, :, :, i, j)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function alifForward(kfn_zit,
|
||||
zit,
|
||||
wRec,
|
||||
vt0,
|
||||
vt1,
|
||||
vth,
|
||||
avth,
|
||||
vRest,
|
||||
zt1,
|
||||
alpha,
|
||||
phi,
|
||||
epsilonRec,
|
||||
epsilonRecA,
|
||||
refractoryCounter,
|
||||
refractoryDuration,
|
||||
a,
|
||||
beta,
|
||||
rho,
|
||||
gammaPd,
|
||||
firingCounter)
|
||||
d1, d2, d3, d4 = size(wRec)
|
||||
zit .= reshape(kfn_zit, (d1, d2, 1, d4)) .* ones(size(wRec)...) # project zit into zit
|
||||
|
||||
for j in 1:d4, i in 1:d3 # compute along neurons axis of every batch
|
||||
if view(refractoryCounter, :, :, i, j)[1] > 0 # refractory period is active
|
||||
view(refractoryCounter, :, :, i, j)[1] -= 1
|
||||
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
|
||||
view(phi, :, :, i, j)[1] =
|
||||
(view(gammaPd, :, :, i, j)[1] / view(vth, :, :, i, j)[1]) *
|
||||
max(0, 1 - ((view(vt1, :, :, i, j)[1] - view(avth, :, :, i, j)[1]) /
|
||||
view(vth, :, :, i, j)[1]))
|
||||
view(epsilonRec, :, :, i, j) .=
|
||||
(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,
|
||||
zit,
|
||||
wOut,
|
||||
vt0,
|
||||
vt1,
|
||||
vth,
|
||||
vRest,
|
||||
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 view(refractoryCounter, :, :, i, j)[1] > 0 # neuron is inactive (in refractory period)
|
||||
view(refractoryCounter, :, :, i, j)[1] -= 1
|
||||
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)
|
||||
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
|
||||
view(zt1, :, :, i, j)[1] = 0
|
||||
end
|
||||
# there is a difference from alif formula
|
||||
view(phi, :, :, i, j)[1] =
|
||||
(view(gammaPd, :, :, i, j)[1] / view(vth, :, :, i, j)[1]) *
|
||||
max(0, 1 - ((view(vt1, :, :, i, j)[1] - view(vth, :, :, i, j)[1]) /
|
||||
view(vth, :, :, i, j)[1]))
|
||||
view(epsilonRec, :, :, i, j) .=
|
||||
(view(alpha, :, :, i, j)[1] .* view(epsilonRec, :, :, i, j)) +
|
||||
view(zit, :, :, i, j)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# function onForward(kfn_zit,
|
||||
# zit,
|
||||
# wOut,
|
||||
# vt0,
|
||||
# vt1,
|
||||
# vth,
|
||||
# vRest,
|
||||
# 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 view(refractoryCounter, :, :, i, j)[1] > 0 # neuron is inactive (in refractory period)
|
||||
# view(refractoryCounter, :, :, i, j)[1] -= 1
|
||||
# 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)
|
||||
# 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
|
||||
# view(zt1, :, :, i, j)[1] = 0
|
||||
# end
|
||||
# # there is a difference from alif formula
|
||||
# view(phi, :, :, i, j)[1] =
|
||||
# (view(gammaPd, :, :, i, j)[1] / view(vth, :, :, i, j)[1]) *
|
||||
# max(0, 1 - ((view(vt1, :, :, i, j)[1] - view(vth, :, :, i, j)[1]) /
|
||||
# view(vth, :, :, i, j)[1]))
|
||||
# view(epsilonRec, :, :, i, j) .=
|
||||
# (view(alpha, :, :, i, j)[1] .* view(epsilonRec, :, :, i, j)) +
|
||||
# view(zit, :, :, i, j)
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
end # module
|
||||
87
previousVersion/0.0.1/src/interface.jl
Normal file
87
previousVersion/0.0.1/src/interface.jl
Normal file
@@ -0,0 +1,87 @@
|
||||
module interface
|
||||
|
||||
|
||||
# export
|
||||
|
||||
# using Flux, CUDA
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
end # module
|
||||
237
previousVersion/0.0.1/src/learn.jl
Normal file
237
previousVersion/0.0.1/src/learn.jl
Normal file
@@ -0,0 +1,237 @@
|
||||
module learn
|
||||
|
||||
export learn!, compute_paramsChange!
|
||||
|
||||
using Statistics, Random, LinearAlgebra, JSON3, Flux, Dates
|
||||
using GeneralUtils
|
||||
using ..type, ..snnUtil
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
function compute_paramsChange!(kfn::kfn_1, modelError, outputError)
|
||||
|
||||
|
||||
lifComputeParamsChange!(kfn.lif_phi,
|
||||
kfn.lif_epsilonRec,
|
||||
kfn.lif_eta,
|
||||
kfn.lif_wRec,
|
||||
kfn.lif_wRecChange,
|
||||
kfn.on_wOut,
|
||||
modelError)
|
||||
|
||||
alifComputeParamsChange!(kfn.alif_phi,
|
||||
kfn.alif_epsilonRec,
|
||||
kfn.alif_epsilonRecA,
|
||||
kfn.alif_eta,
|
||||
kfn.alif_wRec,
|
||||
kfn.alif_wRecChange,
|
||||
kfn.alif_beta,
|
||||
kfn.on_wOut,
|
||||
modelError)
|
||||
|
||||
onComputeParamsChange!(kfn.on_phi,
|
||||
kfn.on_epsilonRec,
|
||||
kfn.on_eta,
|
||||
kfn.on_wOutChange,
|
||||
outputError)
|
||||
|
||||
|
||||
error("debug end -> kfn compute_paramsChange! $(Dates.now())")
|
||||
end
|
||||
|
||||
function lifComputeParamsChange!( phi,
|
||||
epsilonRec,
|
||||
eta,
|
||||
wRec,
|
||||
wRecChange,
|
||||
wOut,
|
||||
modelError)
|
||||
d1, d2, d3, d4 = size(epsilonRec)
|
||||
|
||||
# Bₖⱼ in paper, sum() to get each neuron's total wOut weight
|
||||
wOutSum = reshape(sum(wOut, dims=3), (d1, :, d4))
|
||||
|
||||
for j in 1:d4, i in 1:d3 # compute along neurons axis of every batch
|
||||
# how much error of this neuron 1-spike causing each output neuron's error
|
||||
|
||||
view(wRecChange, :, :, i, j) .+= (-1 * view(eta, :, :, i, j)[1]) .*
|
||||
# eRec
|
||||
(
|
||||
(view(phi, :, :, i, j)[1] .* view(epsilonRec, :, :, i, j)) .*
|
||||
# nError a.k.a. learning signal
|
||||
(
|
||||
view(modelError, :, j)[1] * # dopamine concept, this neuron receive summed error signal
|
||||
# RSNN neuron's total wOut weight (neuron synaptic subscription .* wOutSum)
|
||||
view(wOutSum, :, :, j)[i]
|
||||
)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function alifComputeParamsChange!( phi,
|
||||
epsilonRec,
|
||||
epsilonRecA,
|
||||
eta,
|
||||
wRec,
|
||||
wRecChange,
|
||||
beta,
|
||||
wOut,
|
||||
modelError)
|
||||
d1, d2, d3, d4 = size(epsilonRec)
|
||||
|
||||
# Bₖⱼ in paper, sum() to get each neuron's total wOut weight
|
||||
wOutSum = reshape(sum(wOut, dims=3), (d1, :, d4))
|
||||
|
||||
for j in 1:d4, i in 1:d3 # compute along neurons axis of every batch
|
||||
# how much error of this neuron 1-spike causing each output neuron's error
|
||||
|
||||
view(wRecChange, :, :, i, j) .+= (-1 * view(eta, :, :, i, j)[1]) .*
|
||||
# eRec
|
||||
(
|
||||
# eRec_v
|
||||
(view(phi, :, :, i, j)[1] .* view(epsilonRec, :, :, i, j)) .+
|
||||
# eRec_a
|
||||
((view(phi, :, :, i, j)[1] * view(beta, :, :, i, j)[1]) .*
|
||||
view(epsilonRecA, :, :, i, j))
|
||||
) .*
|
||||
# nError a.k.a. learning signal
|
||||
(
|
||||
view(modelError, :, j)[1] *
|
||||
# RSNN neuron's total wOut weight (neuron synaptic subscription .* wOutSum)
|
||||
view(wOutSum, :, :, j)[i]
|
||||
# sum(GeneralUtils.isNotEqual.(view(wRec, :, :, i, j), 0) .*
|
||||
# view(wOutSum, :, :, j))
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function onComputeParamsChange!(phi,
|
||||
epsilonRec,
|
||||
eta,
|
||||
wOutChange,
|
||||
outputError)
|
||||
d1, d2, d3, d4 = size(epsilonRec)
|
||||
|
||||
for j in 1:d4, i in 1:d3 # compute along neurons axis of every batch
|
||||
# how much error of this neuron 1-spike causing each output neuron's error
|
||||
|
||||
view(wOutChange, :, :, i, j) .+= (-1 * view(eta, :, :, i, j)[1]) .*
|
||||
# eRec
|
||||
(
|
||||
(view(phi, :, :, i, j)[1] .* view(epsilonRec, :, :, i, j)) .*
|
||||
# nError a.k.a. learning signal, output neuron receives error of its own answer - correct answer.
|
||||
view(outputError, :, j)[i]
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
# function onComputeParamsChange!(wOut,
|
||||
# epsilonRec,
|
||||
# eta,
|
||||
# wOutChange,
|
||||
# bChange,
|
||||
# outputError)
|
||||
# d1, d2, d3, d4 = size(epsilonRec)
|
||||
# println(">>> epsilon ", size(epsilonRec))
|
||||
# println(">>> outputError ", size(outputError))
|
||||
|
||||
|
||||
# # Bₖⱼ in paper, sum() to get each neuron's total wOut weight
|
||||
|
||||
# for j in 1:d4, i in 1:d3 # compute along neurons axis of every batch
|
||||
# # how much error of this neuron 1-spike causing each output neuron's error
|
||||
|
||||
# view(wOutChange, :, :, i, j) .+=
|
||||
# (-1 * view(eta, :, :, i, j)[1] * view(outputError, :, j)[i]) .*
|
||||
# view(epsilonRec, :, :, i, j)
|
||||
# end
|
||||
# #TODO add b
|
||||
# error(">>> DEBUG -> onComputeParamsChange!")
|
||||
# end
|
||||
|
||||
|
||||
function learn!(kfn::kfn_1)
|
||||
#WORKING lif learn
|
||||
lifLearn!(kfn.lif_wRec,
|
||||
kfn.lif_wRecChange)
|
||||
|
||||
|
||||
#TODO alif learn
|
||||
|
||||
|
||||
#TODO on learn
|
||||
|
||||
#TODO wOut decay
|
||||
|
||||
# wrap up learning session
|
||||
if kfn.learningStage == [3]
|
||||
kfn.learningStage = [0]
|
||||
end
|
||||
end
|
||||
|
||||
function lifLearn!(wRec,
|
||||
wRecChange)
|
||||
# merge learning weight
|
||||
wRec .+= wRecChange
|
||||
|
||||
#TODO synaptic strength
|
||||
|
||||
#TODO neuroplasticity
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
end # module
|
||||
77
previousVersion/0.0.1/src/snnUtil.jl
Normal file
77
previousVersion/0.0.1/src/snnUtil.jl
Normal file
@@ -0,0 +1,77 @@
|
||||
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 # active
|
||||
view(refractoryActive, 1, 1, i, j) .= 1
|
||||
view(refractoryInactive, 1, 1, i, j) .= 0
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
end # module
|
||||
344
previousVersion/0.0.1/src/type.jl
Normal file
344
previousVersion/0.0.1/src/type.jl
Normal file
@@ -0,0 +1,344 @@
|
||||
module type
|
||||
|
||||
export
|
||||
# struct
|
||||
kfn_1
|
||||
|
||||
# function
|
||||
|
||||
using Random, GeneralUtils
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
rng = MersenneTwister(1234)
|
||||
|
||||
abstract type Ironpen end
|
||||
abstract type knowledgeFn <: Ironpen end
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
Base.@kwdef mutable struct kfn_1 <: knowledgeFn
|
||||
params::Union{Dict, Nothing} = nothing # store params of knowledgeFn itself for later use
|
||||
|
||||
timeStep::Union{AbstractArray, Nothing} = nothing
|
||||
learningStage::Union{AbstractArray, Nothing} = nothing # 0 inference, 1 start, 2 during, 3 end learning
|
||||
zit::Union{AbstractArray, Nothing} = nothing # 3D activation matrix
|
||||
|
||||
# ---------------------------------------------------------------------------- #
|
||||
# LIF Neurons #
|
||||
# ---------------------------------------------------------------------------- #
|
||||
# a projection of kfn.zit into lif dimension for broadcasting later)
|
||||
lif_zit::Union{AbstractArray, Nothing} = nothing
|
||||
|
||||
# main variables according to papers
|
||||
lif_wRec::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_refractoryDuration::Union{AbstractArray, Nothing} = nothing
|
||||
lif_alpha::Union{AbstractArray, Nothing} = nothing
|
||||
lif_delta::Union{AbstractFloat, Nothing} = nothing
|
||||
lif_tau_m::Union{AbstractFloat, Nothing} = nothing
|
||||
lif_phi::Union{AbstractArray, Nothing} = nothing
|
||||
lif_epsilonRec::Union{AbstractArray, Nothing} = nothing
|
||||
lif_eRec::Union{AbstractArray, Nothing} = nothing
|
||||
lif_eta::Union{AbstractArray, Nothing} = nothing
|
||||
lif_gammaPd::Union{AbstractArray, Nothing} = nothing
|
||||
lif_wRecChange::Union{AbstractArray, Nothing} = nothing
|
||||
|
||||
lif_firingCounter::Union{AbstractArray, Nothing} = nothing
|
||||
lif_arraySize::Union{AbstractArray, Nothing} = nothing
|
||||
|
||||
# pre-allocation array
|
||||
lif_arrayProjection3DTo4D::Union{AbstractArray, Nothing} = nothing # use to project 3d array to 4d
|
||||
lif_recSignal::Union{AbstractArray, Nothing} = nothing
|
||||
lif_decayed_Vt0::Union{AbstractArray, Nothing} = nothing
|
||||
lif_decayed_EpsilonRec::Union{AbstractArray, Nothing} = nothing
|
||||
lif_vt1_diff_vth::Union{AbstractArray, Nothing} = nothing
|
||||
lif_vt1_diff_vth_div_vth::Union{AbstractArray, Nothing} = nothing
|
||||
lif_gammaPd_div_vth::Union{AbstractArray, Nothing} = nothing
|
||||
lif_phiActivation::Union{AbstractArray, Nothing} = nothing
|
||||
|
||||
# ---------------------------------------------------------------------------- #
|
||||
# ALIF Neurons #
|
||||
# ---------------------------------------------------------------------------- #
|
||||
alif_zit::Union{AbstractArray, Nothing} = nothing
|
||||
|
||||
alif_wRec::Union{AbstractArray, Nothing} = nothing
|
||||
alif_vt0::Union{AbstractArray, Nothing} = nothing
|
||||
alif_vt1::Union{AbstractArray, Nothing} = nothing
|
||||
alif_vth::Union{AbstractArray, Nothing} = nothing
|
||||
alif_avth::Union{AbstractArray, Nothing} = nothing
|
||||
alif_vRest::Union{AbstractArray, Nothing} = nothing
|
||||
alif_zt0::Union{AbstractArray, Nothing} = nothing
|
||||
alif_zt1::Union{AbstractArray, Nothing} = nothing
|
||||
alif_refractoryCounter::Union{AbstractArray, Nothing} = nothing
|
||||
alif_refractoryDuration::Union{AbstractArray, Nothing} = nothing
|
||||
alif_alpha::Union{AbstractArray, Nothing} = nothing
|
||||
alif_delta::Union{AbstractFloat, Nothing} = nothing
|
||||
alif_tau_m::Union{AbstractFloat, Nothing} = nothing
|
||||
alif_phi::Union{AbstractArray, Nothing} = nothing
|
||||
alif_epsilonRec::Union{AbstractArray, Nothing} = nothing
|
||||
alif_epsilonRecA::Union{AbstractArray, Nothing} = nothing
|
||||
alif_eRec::Union{AbstractArray, Nothing} = nothing
|
||||
alif_eta::Union{AbstractArray, Nothing} = nothing
|
||||
alif_gammaPd::Union{AbstractArray, Nothing} = nothing
|
||||
alif_wRecChange::Union{AbstractArray, Nothing} = nothing
|
||||
|
||||
alif_firingCounter::Union{AbstractArray, Nothing} = nothing
|
||||
alif_arraySize::Union{AbstractArray, Nothing} = nothing
|
||||
alif_arrayProjection3DTo4D::Union{AbstractArray, Nothing} = nothing # use to project 3d array to 4d
|
||||
|
||||
alif_a::Union{AbstractArray, Nothing} = nothing # threshold adaptation
|
||||
alif_beta::Union{AbstractArray, Nothing} = nothing # β, constant, value from paper
|
||||
alif_rho::Union{AbstractArray, Nothing} = nothing # ρ, threshold adaptation decay factor
|
||||
alif_tau_a::Union{AbstractFloat, Nothing} = nothing # τ_a, adaption time constant in millisecond
|
||||
|
||||
# ---------------------------------------------------------------------------- #
|
||||
# Output Neurons #
|
||||
# ---------------------------------------------------------------------------- #
|
||||
# output neuron is based on LIF
|
||||
on_zit::Union{AbstractArray, Nothing} = nothing
|
||||
|
||||
on_wOut::Union{AbstractArray, Nothing} = nothing # same as lif_wRec
|
||||
on_vt0::Union{AbstractArray, Nothing} = nothing
|
||||
on_vt1::Union{AbstractArray, Nothing} = nothing
|
||||
on_vth::Union{AbstractArray, Nothing} = nothing
|
||||
on_vRest::Union{AbstractArray, Nothing} = nothing
|
||||
on_zt0::Union{AbstractArray, Nothing} = nothing
|
||||
on_zt1::Union{AbstractArray, Nothing} = nothing
|
||||
on_refractoryCounter::Union{AbstractArray, Nothing} = nothing
|
||||
on_refractoryDuration::Union{AbstractArray, Nothing} = nothing
|
||||
on_alpha::Union{AbstractArray, Nothing} = nothing
|
||||
on_delta::Union{AbstractFloat, Nothing} = nothing
|
||||
on_tau_m::Union{AbstractFloat, Nothing} = nothing
|
||||
on_phi::Union{AbstractArray, Nothing} = nothing
|
||||
on_epsilonRec::Union{AbstractArray, Nothing} = nothing
|
||||
on_eRec::Union{AbstractArray, Nothing} = nothing
|
||||
on_eta::Union{AbstractArray, Nothing} = nothing
|
||||
on_gammaPd::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_arraySize::Union{AbstractArray, Nothing} = nothing
|
||||
on_arrayProjection3DTo4D::Union{AbstractArray, Nothing} = nothing # use to project 3d array to 4d
|
||||
end
|
||||
|
||||
# outer constructor
|
||||
function kfn_1(params::Dict; device=cpu)
|
||||
kfn = kfn_1()
|
||||
kfn.params = params
|
||||
kfn.timeStep = [0] |> device
|
||||
kfn.learningStage = [0] |> device
|
||||
|
||||
# ---------------------------------------------------------------------------- #
|
||||
# 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 += kfn.params[:inputPort][:noise][:numbers][1]
|
||||
col += kfn.params[:inputPort][:noise][:numbers][2]
|
||||
col += kfn.params[:computeNeuron][:lif][:numbers][2]
|
||||
col += kfn.params[:computeNeuron][:alif][:numbers][2]
|
||||
|
||||
# activation matrix
|
||||
kfn.zit = zeros(row, col, batch) |> device
|
||||
|
||||
# ---------------------------------------------------------------------------- #
|
||||
# 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)
|
||||
n = kfn.params[:computeNeuron][:lif][:numbers][1] * kfn.params[:computeNeuron][:lif][:numbers][2]
|
||||
|
||||
# subscription
|
||||
w = zeros(row, col, n)
|
||||
synapticConnectionPercent = kfn.params[:computeNeuron][:lif][: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.lif_wRec
|
||||
kfn.lif_wRec = reshape(w, (row, col, n, 1)) .* ones(row, col, n, batch) |> device
|
||||
|
||||
kfn.lif_zit = similar(kfn.lif_wRec) .= 0 |> device
|
||||
kfn.lif_vt0 = zeros(1, 1, n, batch) |> device
|
||||
kfn.lif_vt1 = zeros(1, 1, n, batch) |> device
|
||||
kfn.lif_vth = ones(1, 1, n, batch) |> device
|
||||
kfn.lif_vRest = zeros(1, 1, n, batch) |> device
|
||||
# kfn.lif_zt0 = zeros(1, 1, n, batch) |> device
|
||||
kfn.lif_zt1 = zeros(1, 1, n, batch) |> device
|
||||
kfn.lif_refractoryCounter = zeros(1, 1, n, batch) |> device
|
||||
kfn.lif_refractoryDuration = ones(1, 1, n, batch) .* 3 |> device
|
||||
kfn.lif_delta = 1.0
|
||||
kfn.lif_tau_m = 20.0
|
||||
kfn.lif_alpha = ones(1, 1, n, batch) .* (exp(-kfn.lif_delta / kfn.lif_tau_m)) |> device
|
||||
kfn.lif_phi = zeros(1, 1, n, batch) |> device
|
||||
kfn.lif_epsilonRec = zeros(row, col, n, batch) |> device
|
||||
# kfn.lif_eRec = zeros(row, col, n, batch)
|
||||
kfn.lif_eta = zeros(1, 1, n, batch) |> device
|
||||
kfn.lif_gammaPd = zeros(1, 1, n, batch) .* 0.3 |> device
|
||||
kfn.lif_wRecChange = zeros(row, col, n, batch) |> device
|
||||
|
||||
kfn.lif_firingCounter = zeros(1, 1, n, batch) |> device
|
||||
kfn.lif_arraySize = [row, col, n, batch] |> device
|
||||
kfn.lif_arrayProjection3DTo4D = ones(row, col, n, batch) |> device
|
||||
|
||||
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------- #
|
||||
# ALIF config #
|
||||
# ---------------------------------------------------------------------------- #
|
||||
n = kfn.params[:computeNeuron][:alif][:numbers][1] * kfn.params[:computeNeuron][:alif][:numbers][2]
|
||||
kfn.alif_zit = zeros(row, col, n, batch) |> device
|
||||
kfn.alif_vt0 = zeros(1, 1, n, batch) |> device
|
||||
kfn.alif_vt1 = zeros(1, 1, n, batch) |> device
|
||||
kfn.alif_vth = ones(1, 1, n, batch) |> device
|
||||
kfn.alif_avth = ones(1, 1, n, batch) |> device
|
||||
kfn.alif_vRest = zeros(1, 1, n, batch) |> device
|
||||
# kfn.alif_zt0 = zeros(1, 1, n, batch) |> device
|
||||
kfn.alif_zt1 = zeros(1, 1, n, batch) |> device
|
||||
kfn.alif_refractoryCounter = zeros(1, 1, n, batch) |> device
|
||||
kfn.alif_refractoryDuration = ones(1, 1, n, batch) .* 3 |> device
|
||||
kfn.alif_delta = 1.0
|
||||
kfn.alif_tau_m = 20.0
|
||||
kfn.alif_alpha = ones(1, 1, n, batch) .* (exp(-kfn.alif_delta / kfn.alif_tau_m)) |> device
|
||||
kfn.alif_phi = zeros(1, 1, n, batch) |> device
|
||||
kfn.alif_epsilonRec = zeros(row, col, n, batch) |> device
|
||||
kfn.alif_epsilonRecA = zeros(row, col, n, batch) |> device
|
||||
# kfn.alif_eRec = zeros(row, col, n, batch)
|
||||
kfn.alif_eta = zeros(1, 1, n, batch) |> device
|
||||
kfn.alif_gammaPd = zeros(1, 1, n, batch) .* 0.3 |> device
|
||||
kfn.alif_wRecChange = zeros(row, col, n, batch) |> device
|
||||
|
||||
kfn.alif_a = zeros(1, 1, n, batch) |> device
|
||||
kfn.alif_beta = zeros(1, 1, n, batch) .* 0.15 |> device
|
||||
kfn.alif_tau_a = 100.0
|
||||
kfn.alif_rho = zeros(1, 1, n, batch) .* (exp(-kfn.alif_delta / kfn.alif_tau_a)) |> device
|
||||
|
||||
kfn.alif_firingCounter = zeros(1, 1, n, batch) |> device
|
||||
kfn.alif_arraySize = [row, col, n, batch] |> device
|
||||
kfn.alif_arrayProjection3DTo4D = ones(row, col, n, batch) |> device
|
||||
|
||||
# subscription
|
||||
w = zeros(row, col, n)
|
||||
synapticConnectionPercent = kfn.params[:computeNeuron][:alif][: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.alif_wRec
|
||||
kfn.alif_wRec = reshape(w, (row, col, n, 1)) .* ones(row, col, n, batch) |> device
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------- #
|
||||
# output config #
|
||||
# ---------------------------------------------------------------------------- #
|
||||
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
|
||||
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
|
||||
|
||||
|
||||
|
||||
return kfn
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
end # module
|
||||
Reference in New Issue
Block a user