This commit is contained in:
ton
2023-09-14 10:28:30 +07:00
parent 2f55cdf7a7
commit 9e701cd042
3 changed files with 128 additions and 101 deletions

View File

@@ -18,7 +18,7 @@ function (kfn::kfn_1)(input::AbstractArray)
# what to do at the start of learning round # what to do at the start of learning round
if view(kfn.learningStage, 1)[1] == 1 if view(kfn.learningStage, 1)[1] == 1
# reset learning params # reset learning params
kfn.zitCumulative .= 0 kfn.zitCumulative = kfn.zitCumulative[:,:,1,:]
kfn.lif_vt .= 0 kfn.lif_vt .= 0
kfn.lif_wRecChange .= 0 kfn.lif_wRecChange .= 0
@@ -26,7 +26,7 @@ function (kfn::kfn_1)(input::AbstractArray)
kfn.lif_firingCounter .= 0 kfn.lif_firingCounter .= 0
kfn.lif_refractoryCounter .= 0 kfn.lif_refractoryCounter .= 0
kfn.lif_zt .= 0 kfn.lif_zt .= 0
kfn.lif_synapseReconnectDelayCounter .= 0 kfn.lif_synapseReconnectDelay .= 0
kfn.alif_vt .= 0 kfn.alif_vt .= 0
kfn.alif_a .= 0 kfn.alif_a .= 0
@@ -36,7 +36,7 @@ function (kfn::kfn_1)(input::AbstractArray)
kfn.alif_firingCounter .= 0 kfn.alif_firingCounter .= 0
kfn.alif_refractoryCounter .= 0 kfn.alif_refractoryCounter .= 0
kfn.alif_zt .= 0 kfn.alif_zt .= 0
kfn.alif_synapseReconnectDelayCounter .= 0 kfn.alif_synapseReconnectDelay .= 0
kfn.on_vt .= 0 kfn.on_vt .= 0
kfn.on_epsilonRec .= 0 kfn.on_epsilonRec .= 0
@@ -77,7 +77,8 @@ function (kfn::kfn_1)(input::AbstractArray)
kfn.lif_exInType, kfn.lif_exInType,
kfn.lif_wRecChange, kfn.lif_wRecChange,
kfn.lif_neuronInactivityCounter, kfn.lif_neuronInactivityCounter,
kfn.lif_synapseReconnectDelayCounter, kfn.lif_synapseReconnectDelay,
kfn.timeStep,
) )
end end
@async begin @async begin
@@ -103,12 +104,13 @@ function (kfn::kfn_1)(input::AbstractArray)
kfn.alif_exInType, kfn.alif_exInType,
kfn.alif_wRecChange, kfn.alif_wRecChange,
kfn.alif_neuronInactivityCounter, kfn.alif_neuronInactivityCounter,
kfn.alif_synapseReconnectDelayCounter, kfn.alif_synapseReconnectDelay,
kfn.alif_epsilonRecA, kfn.alif_epsilonRecA,
kfn.alif_a, kfn.alif_a,
kfn.alif_avth, kfn.alif_avth,
kfn.alif_beta, kfn.alif_beta,
kfn.alif_rho, kfn.alif_rho,
kfn.timeStep,
) )
end end
end end
@@ -123,7 +125,9 @@ function (kfn::kfn_1)(input::AbstractArray)
reshape(kfn.lif_zt, (size(input, 1), :, 1, size(input, 3))), reshape(kfn.lif_zt, (size(input, 1), :, 1, size(input, 3))),
reshape(kfn.alif_zt, (size(input, 1), :, 1, size(input, 3))), dims=2) reshape(kfn.alif_zt, (size(input, 1), :, 1, size(input, 3))), dims=2)
kfn.zit .= reshape(_zit, (size(input, 1), :, size(input, 3))) kfn.zit .= reshape(_zit, (size(input, 1), :, size(input, 3)))
kfn.zitCumulative .+= kfn.zit kfn.zitCumulative = sum(kfn.zitCumulative) == 0 ? kfn.zit : cat(kfn.zitCumulative, kfn.zit, dims=3)
# kfn.zitCumulative = cat(kfn.zitCumulative, kfn.zit, dims=3)
# kfn.zitCumulative .+= kfn.zit
# project 3D kfn zit into 4D on zit # project 3D kfn zit into 4D on zit
i1, i2, i3, i4 = size(kfn.on_zit) i1, i2, i3, i4 = size(kfn.on_zit)
@@ -171,7 +175,8 @@ function lifForward( zit::CuArray,
exInType::CuArray, exInType::CuArray,
wRecChange::CuArray, wRecChange::CuArray,
neuronInactivityCounter::CuArray, neuronInactivityCounter::CuArray,
synapseReconnectDelayCounter::CuArray, synapseReconnectDelay::CuArray,
timeStep::CuArray,
) )
kernel = @cuda launch=false lifForward( zit, kernel = @cuda launch=false lifForward( zit,
@@ -191,8 +196,9 @@ function lifForward( zit::CuArray,
exInType, exInType,
wRecChange, wRecChange,
neuronInactivityCounter, neuronInactivityCounter,
synapseReconnectDelayCounter, synapseReconnectDelay,
GeneralUtils.linear_to_cartesian, GeneralUtils.linear_to_cartesian,
timeStep,
) )
config = launch_configuration(kernel.fun) config = launch_configuration(kernel.fun)
@@ -225,8 +231,9 @@ function lifForward( zit::CuArray,
exInType, exInType,
wRecChange, wRecChange,
neuronInactivityCounter, neuronInactivityCounter,
synapseReconnectDelayCounter, synapseReconnectDelay,
GeneralUtils.linear_to_cartesian; threads, blocks) GeneralUtils.linear_to_cartesian,
timeStep; threads, blocks)
end end
end end
@@ -248,8 +255,9 @@ function lifForward( zit,
exInType, exInType,
wRecChange, wRecChange,
neuronInactivityCounter, neuronInactivityCounter,
synapseReconnectDelayCounter, synapseReconnectDelay,
linear_to_cartesian, linear_to_cartesian,
timeStep,
) )
i = (blockIdx().x - 1) * blockDim().x + threadIdx().x # gpu threads index i = (blockIdx().x - 1) * blockDim().x + threadIdx().x # gpu threads index
@@ -297,12 +305,11 @@ function lifForward( zit,
(zit[i1,i2,i3,i4] * !iszero(wRec[i1,i2,i3,i4])) (zit[i1,i2,i3,i4] * !iszero(wRec[i1,i2,i3,i4]))
# !iszero indicates synaptic subscription # !iszero indicates synaptic subscription
# count synaptic inactivity
if !iszero(wRec[i1,i2,i3,i4]) # check if this is wRec subscription if !iszero(wRec[i1,i2,i3,i4]) # check if this is wRec subscription
if !iszero(zit[i1,i2,i3,i4]) # synapse is active synapseReconnectDelay[i1,i2,i3,i4] -= 1
synapseReconnectDelayCounter[i1,i2,i3,i4] += 1 if synapseReconnectDelay[i1,i2,i3,i4] == 0
else # synapse is inactive # mark timestep
synapseReconnectDelayCounter[i1,i2,i3,i4] += 0 synapseReconnectDelay[i1,i2,i3,i4] = sum(timeStep)
end end
end end
# voltage regulator # voltage regulator
@@ -331,12 +338,13 @@ function alifForward( zit::CuArray,
exInType::CuArray, exInType::CuArray,
wRecChange::CuArray, wRecChange::CuArray,
neuronInactivityCounter::CuArray, neuronInactivityCounter::CuArray,
synapseReconnectDelayCounter::CuArray, synapseReconnectDelay::CuArray,
epsilonRecA::CuArray, epsilonRecA::CuArray,
a::CuArray, a::CuArray,
avth::CuArray, avth::CuArray,
beta::CuArray, beta::CuArray,
rho::CuArray, rho::CuArray,
timeStep::CuArray,
) )
kernel = @cuda launch=false alifForward( zit, kernel = @cuda launch=false alifForward( zit,
@@ -356,13 +364,14 @@ function alifForward( zit::CuArray,
exInType, exInType,
wRecChange, wRecChange,
neuronInactivityCounter, neuronInactivityCounter,
synapseReconnectDelayCounter, synapseReconnectDelay,
epsilonRecA, epsilonRecA,
a, a,
avth, avth,
beta, beta,
rho, rho,
GeneralUtils.linear_to_cartesian, GeneralUtils.linear_to_cartesian,
timeStep,
) )
config = launch_configuration(kernel.fun) config = launch_configuration(kernel.fun)
@@ -394,13 +403,14 @@ function alifForward( zit::CuArray,
exInType, exInType,
wRecChange, wRecChange,
neuronInactivityCounter, neuronInactivityCounter,
synapseReconnectDelayCounter, synapseReconnectDelay,
epsilonRecA, epsilonRecA,
a, a,
avth, avth,
beta, beta,
rho, rho,
GeneralUtils.linear_to_cartesian; threads, blocks) GeneralUtils.linear_to_cartesian,
timeStep; threads, blocks)
end end
end end
@@ -422,13 +432,14 @@ function alifForward( zit,
exInType, exInType,
wRecChange, wRecChange,
neuronInactivityCounter, neuronInactivityCounter,
synapseReconnectDelayCounter, synapseReconnectDelay,
epsilonRecA, epsilonRecA,
a, a,
avth, avth,
beta, beta,
rho, rho,
linear_to_cartesian, linear_to_cartesian,
timeStep,
) )
i = (blockIdx().x - 1) * blockDim().x + threadIdx().x # gpu threads index i = (blockIdx().x - 1) * blockDim().x + threadIdx().x # gpu threads index
@@ -490,12 +501,10 @@ function alifForward( zit,
(phi[i1,i2,i3,i4] * epsilonRec[i1,i2,i3,i4])) + (phi[i1,i2,i3,i4] * epsilonRec[i1,i2,i3,i4])) +
(zit[i1,i2,i3,i4] * !iszero(wRec[i1,i2,i3,i4])) (zit[i1,i2,i3,i4] * !iszero(wRec[i1,i2,i3,i4]))
# count synaptic inactivity
if !iszero(wRec[i1,i2,i3,i4]) # check if this is wRec subscription if !iszero(wRec[i1,i2,i3,i4]) # check if this is wRec subscription
if !iszero(zit[i1,i2,i3,i4]) # synapse is active synapseReconnectDelay[i1,i2,i3,i4] -= 1
synapseReconnectDelayCounter[i1,i2,i3,i4] += 1 if synapseReconnectDelay[i1,i2,i3,i4] == 0
else # synapse is inactive synapseReconnectDelay[i1,i2,i3,i4] = sum(timeStep)
synapseReconnectDelayCounter[i1,i2,i3,i4] += 0
end end
end end
# voltage regulator # voltage regulator

View File

@@ -92,10 +92,16 @@ function lifComputeParamsChange!( timeStep::CuArray,
startCol = CartesianIndices(wRec)[startIndex][2] startCol = CartesianIndices(wRec)[startIndex][2]
stopCol = CartesianIndices(wRec)[stopIndex][2] stopCol = CartesianIndices(wRec)[stopIndex][2]
# some RSNN neuron that has direct connection to output neuron need to get Bjk
# from output neuron that represent correct answer, the rest of RSNN get random Bjk
onW = @view(wOut[:, startCol:stopCol, sum(label), 1])
_bk = @view(bk[:, startCol:stopCol, 1]) _bk = @view(bk[:, startCol:stopCol, 1])
nError = _bk .* modelError mask = iszero.(onW)
bk_ = mask .* _bk
bkComposed = onW .+ bk_
nError = bkComposed .* modelError
nError = reshape(nError, (1,1,:,1)) nError = reshape(nError, (1,1,:,1))
# _,_,i3,_ = size(wOut) # _,_,i3,_ = size(wOut)
# for i in 1:i3 # for i in 1:i3
# # nError a.k.a. learning signal use dopamine concept, # # nError a.k.a. learning signal use dopamine concept,
@@ -131,7 +137,7 @@ function lifComputeParamsChange!( timeStep::CuArray,
# println("") # println("")
# error("DEBUG lifComputeParamsChange!") # error("DEBUG lifComputeParamsChange!")
# end # end
# error("DEBUG lifComputeParamsChange!")
# reset epsilonRec # reset epsilonRec
epsilonRec .= 0 epsilonRec .= 0
end end
@@ -167,8 +173,14 @@ function alifComputeParamsChange!( timeStep::CuArray,
startCol = CartesianIndices(wRec)[startIndex][2] startCol = CartesianIndices(wRec)[startIndex][2]
stopCol = CartesianIndices(wRec)[stopIndex][2] stopCol = CartesianIndices(wRec)[stopIndex][2]
# some RSNN neuron that has direct connection to output neuron need to get Bjk
# from output neuron that represent correct answer, the rest of RSNN get random Bjk
onW = @view(wOut[:, startCol:stopCol, sum(label), 1])
_bk = @view(bk[:, startCol:stopCol, 1]) _bk = @view(bk[:, startCol:stopCol, 1])
nError = _bk .* modelError mask = iszero.(onW)
bk_ = mask .* _bk
bkComposed = onW .+ bk_
nError = bkComposed .* modelError
nError = reshape(nError, (1,1,:,1)) nError = reshape(nError, (1,1,:,1))
wRecChange .+= (eta .* nError .* eRec) wRecChange .+= (eta .* nError .* eRec)
@@ -291,30 +303,28 @@ end
function learn!(kfn::kfn_1, device=cpu) function learn!(kfn::kfn_1, device=cpu)
# lif learn # lif learn
kfn.lif_wRec, kfn.lif_neuronInactivityCounter, kfn.lif_synapseReconnectDelayCounter = kfn.lif_wRec, kfn.lif_neuronInactivityCounter, kfn.lif_synapseReconnectDelay =
lifLearn(kfn.lif_wRec, lifLearn(kfn.lif_wRec,
kfn.lif_wRecChange, kfn.lif_wRecChange,
kfn.lif_exInType, kfn.lif_exInType,
kfn.lif_arrayProjection4d, kfn.lif_arrayProjection4d,
kfn.lif_neuronInactivityCounter, kfn.lif_neuronInactivityCounter,
kfn.lif_synapseReconnectDelayCounter, kfn.lif_synapseReconnectDelay,
kfn.lif_synapseConnectionNumber, kfn.lif_synapseConnectionNumber,
kfn.lif_synapticWChangeCounter,
kfn.lif_eta, kfn.lif_eta,
kfn.lif_vt, kfn.lif_vt,
kfn.zitCumulative, kfn.zitCumulative,
device) device)
# alif learn # alif learn
kfn.alif_wRec, kfn.alif_neuronInactivityCounter, kfn.alif_synapseReconnectDelayCounter = kfn.alif_wRec, kfn.alif_neuronInactivityCounter, kfn.alif_synapseReconnectDelay =
alifLearn(kfn.alif_wRec, alifLearn(kfn.alif_wRec,
kfn.alif_wRecChange, kfn.alif_wRecChange,
kfn.alif_exInType, kfn.alif_exInType,
kfn.alif_arrayProjection4d, kfn.alif_arrayProjection4d,
kfn.alif_neuronInactivityCounter, kfn.alif_neuronInactivityCounter,
kfn.alif_synapseReconnectDelayCounter, kfn.alif_synapseReconnectDelay,
kfn.alif_synapseConnectionNumber, kfn.alif_synapseConnectionNumber,
kfn.alif_synapticWChangeCounter,
kfn.alif_eta, kfn.alif_eta,
kfn.alif_vt, kfn.alif_vt,
kfn.zitCumulative, kfn.zitCumulative,
@@ -337,7 +347,7 @@ end
# wRecChange, # wRecChange,
# arrayProjection4d, # arrayProjection4d,
# neuronInactivityCounter, # neuronInactivityCounter,
# synapseReconnectDelayCounter, # synapseReconnectDelay,
# synapseConnectionNumber, # synapseConnectionNumber,
# synapticWChangeCounter, #TODO # synapticWChangeCounter, #TODO
# eta, # eta,
@@ -355,15 +365,15 @@ end
# eta_cpu = eta_cpu[:,:,:,1] # eta_cpu = eta_cpu[:,:,:,1]
# neuronInactivityCounter_cpu = neuronInactivityCounter |> cpu # neuronInactivityCounter_cpu = neuronInactivityCounter |> cpu
# neuronInactivityCounter_cpu = neuronInactivityCounter_cpu[:,:,:,1] # (row, col, n) # neuronInactivityCounter_cpu = neuronInactivityCounter_cpu[:,:,:,1] # (row, col, n)
# synapseReconnectDelayCounter_cpu = synapseReconnectDelayCounter |> cpu # synapseReconnectDelay_cpu = synapseReconnectDelay |> cpu
# synapseReconnectDelayCounter_cpu = synapseReconnectDelayCounter_cpu[:,:,:,1] # synapseReconnectDelay_cpu = synapseReconnectDelay_cpu[:,:,:,1]
# zitCumulative_cpu = zitCumulative |> cpu # zitCumulative_cpu = zitCumulative |> cpu
# zitCumulative_cpu = zitCumulative_cpu[:,:,1] # (row, col) # zitCumulative_cpu = zitCumulative_cpu[:,:,1] # (row, col)
# # -W if less than 10% of repeat avg, +W otherwise # # -W if less than 10% of repeat avg, +W otherwise
# _, _, i3 = size(wRec_cpu) # _, _, i3 = size(wRec_cpu)
# for i in 1:i3 # for i in 1:i3
# x = 0.1 * (sum(synapseReconnectDelayCounter[:,:,i]) / length(synapseReconnectDelayCounter[:,:,i])) # x = 0.1 * (sum(synapseReconnectDelay[:,:,i]) / length(synapseReconnectDelay[:,:,i]))
# mask = GeneralUtils.replaceLessThan.(wRec_cpu[:,:,i], x, -1, 1) # mask = GeneralUtils.replaceLessThan.(wRec_cpu[:,:,i], x, -1, 1)
# wRec_cpu[:,:,i] .+= mask .* eta_cpu[:,:,i] .* wRec_cpu[:,:,i] # wRec_cpu[:,:,i] .+= mask .* eta_cpu[:,:,i] .* wRec_cpu[:,:,i]
# end # end
@@ -376,7 +386,7 @@ end
# zitCumulative_cpu, # zitCumulative_cpu,
# wRec_cpu, # wRec_cpu,
# neuronInactivityCounter_cpu, # neuronInactivityCounter_cpu,
# synapseReconnectDelayCounter_cpu) # synapseReconnectDelay_cpu)
# wRec_cpu = wRec_cpu .* arrayProjection4d_cpu # wRec_cpu = wRec_cpu .* arrayProjection4d_cpu
# wRec = wRec_cpu |> device # wRec = wRec_cpu |> device
@@ -384,10 +394,10 @@ end
# neuronInactivityCounter_cpu = neuronInactivityCounter_cpu .* arrayProjection4d_cpu # neuronInactivityCounter_cpu = neuronInactivityCounter_cpu .* arrayProjection4d_cpu
# neuronInactivityCounter = neuronInactivityCounter_cpu |> device # neuronInactivityCounter = neuronInactivityCounter_cpu |> device
# synapseReconnectDelayCounter_cpu = synapseReconnectDelayCounter_cpu .* arrayProjection4d_cpu # synapseReconnectDelay_cpu = synapseReconnectDelay_cpu .* arrayProjection4d_cpu
# synapseReconnectDelayCounter = synapseReconnectDelayCounter_cpu |> device # synapseReconnectDelay = synapseReconnectDelay_cpu |> device
# return wRec, neuronInactivityCounter, synapseReconnectDelayCounter # return wRec, neuronInactivityCounter, synapseReconnectDelay
# end # end
function lifLearn(wRec, function lifLearn(wRec,
@@ -395,14 +405,14 @@ function lifLearn(wRec,
exInType, exInType,
arrayProjection4d, arrayProjection4d,
neuronInactivityCounter, neuronInactivityCounter,
synapseReconnectDelayCounter, synapseReconnectDelay,
synapseConnectionNumber, synapseConnectionNumber,
synapticWChangeCounter, #TODO
eta, eta,
vt, vt,
zitCumulative, zitCumulative,
device) device)
# transfer data to cpu # transfer data to cpu
arrayProjection4d_cpu = arrayProjection4d |> cpu arrayProjection4d_cpu = arrayProjection4d |> cpu
wRec_cpu = wRec |> cpu wRec_cpu = wRec |> cpu
@@ -413,20 +423,20 @@ function lifLearn(wRec,
eta_cpu = eta_cpu[:,:,:,1] eta_cpu = eta_cpu[:,:,:,1]
neuronInactivityCounter_cpu = neuronInactivityCounter |> cpu neuronInactivityCounter_cpu = neuronInactivityCounter |> cpu
neuronInactivityCounter_cpu = neuronInactivityCounter_cpu[:,:,:,1] # (row, col, n) neuronInactivityCounter_cpu = neuronInactivityCounter_cpu[:,:,:,1] # (row, col, n)
synapseReconnectDelayCounter_cpu = synapseReconnectDelayCounter |> cpu synapseReconnectDelay_cpu = synapseReconnectDelay |> cpu
synapseReconnectDelayCounter_cpu = synapseReconnectDelayCounter_cpu[:,:,:,1] synapseReconnectDelay_cpu = synapseReconnectDelay_cpu[:,:,:,1]
zitCumulative_cpu = zitCumulative |> cpu zitCumulative_cpu = zitCumulative |> cpu
zitCumulative_cpu = zitCumulative_cpu[:,:,1] zitCumulative_cpu = zitCumulative_cpu[:,:,1]
#TODO neuroplasticity, work on CPU side # neuroplasticity, work on CPU side
wRec_cpu, neuronInactivityCounter_cpu, synapseReconnectDelayCounter_cpu = wRec_cpu, neuronInactivityCounter_cpu, synapseReconnectDelay_cpu =
neuroplasticity(synapseConnectionNumber, neuroplasticity(synapseConnectionNumber,
zitCumulative_cpu, zitCumulative_cpu,
wRec_cpu, wRec_cpu,
wRecChange_cpu, wRecChange_cpu,
vt, vt,
neuronInactivityCounter_cpu, neuronInactivityCounter_cpu,
synapseReconnectDelayCounter_cpu) synapseReconnectDelay_cpu)
@@ -444,7 +454,7 @@ function lifLearn(wRec,
# # -W if less than 10% of repeat avg, +W otherwise # # -W if less than 10% of repeat avg, +W otherwise
# _, _, i3 = size(wRec_cpu) # _, _, i3 = size(wRec_cpu)
# for i in 1:i3 # for i in 1:i3
# x = 0.1 * (sum(synapseReconnectDelayCounter[:,:,i]) / length(synapseReconnectDelayCounter[:,:,i])) # x = 0.1 * (sum(synapseReconnectDelay[:,:,i]) / length(synapseReconnectDelay[:,:,i]))
# mask = GeneralUtils.replaceLessThan.(wRec_cpu[:,:,i], x, -1, 1) # mask = GeneralUtils.replaceLessThan.(wRec_cpu[:,:,i], x, -1, 1)
# wRec_cpu[:,:,i] .+= mask .* eta_cpu[:,:,i] .* wRec_cpu[:,:,i] # wRec_cpu[:,:,i] .+= mask .* eta_cpu[:,:,i] .* wRec_cpu[:,:,i]
# end # end
@@ -459,17 +469,17 @@ function lifLearn(wRec,
# wRecChange_cpu, # wRecChange_cpu,
# vt, # vt,
# neuronInactivityCounter_cpu, # neuronInactivityCounter_cpu,
# synapseReconnectDelayCounter_cpu) # synapseReconnectDelay_cpu)
# transfer data backto gpu # transfer data backto gpu
wRec_cpu = wRec_cpu .* arrayProjection4d_cpu wRec_cpu = wRec_cpu .* arrayProjection4d_cpu
wRec = wRec_cpu |> device wRec = wRec_cpu |> device
neuronInactivityCounter_cpu = neuronInactivityCounter_cpu .* arrayProjection4d_cpu neuronInactivityCounter_cpu = neuronInactivityCounter_cpu .* arrayProjection4d_cpu
neuronInactivityCounter = neuronInactivityCounter_cpu |> device neuronInactivityCounter = neuronInactivityCounter_cpu |> device
synapseReconnectDelayCounter_cpu = synapseReconnectDelayCounter_cpu .* arrayProjection4d_cpu synapseReconnectDelay_cpu = synapseReconnectDelay_cpu .* arrayProjection4d_cpu
synapseReconnectDelayCounter = synapseReconnectDelayCounter_cpu |> device synapseReconnectDelay = synapseReconnectDelay_cpu |> device
return wRec, neuronInactivityCounter, synapseReconnectDelayCounter return wRec, neuronInactivityCounter, synapseReconnectDelay
end end
#WORKING 1) implement 90% +w, 10% -w 2) rewrite this function #WORKING 1) implement 90% +w, 10% -w 2) rewrite this function
@@ -479,9 +489,9 @@ function neuroplasticity(synapseConnectionNumber,
wRecChange, wRecChange,
vt, vt,
neuronInactivityCounter, neuronInactivityCounter,
synapseReconnectDelayCounter) # (row, col, n) synapseReconnectDelay) # (row, col, n)
i1,i2,i3 = size(wRec) i1,i2,i3 = size(wRec)
error("DEBUG -> neuroplasticity $(Dates.now())")
# merge weight # merge weight
@@ -518,7 +528,7 @@ function neuroplasticity(synapseConnectionNumber,
println("neuroplasticity, from $(synapseConnectionNumber*size(totalNewConn, 3)) conn, $(sum(totalNewConn)) are replaced") println("neuroplasticity, from $(synapseConnectionNumber*size(totalNewConn, 3)) conn, $(sum(totalNewConn)) are replaced")
# clear -1.0 marker # clear -1.0 marker
GeneralUtils.replaceElements!(wRec, -1.0, synapseReconnectDelayCounter, -0.99) GeneralUtils.replaceElements!(wRec, -1.0, synapseReconnectDelay, -0.99)
GeneralUtils.replaceElements!(wRec, -1.0, 0.0) # -1.0 marker is no longer required GeneralUtils.replaceElements!(wRec, -1.0, 0.0) # -1.0 marker is no longer required
for i in 1:i3 for i in 1:i3
@@ -531,7 +541,7 @@ function neuroplasticity(synapseConnectionNumber,
a = similar(w) .= -0.99 # synapseConnectionNumber of this neuron a = similar(w) .= -0.99 # synapseConnectionNumber of this neuron
mask = (!iszero).(w) mask = (!iszero).(w)
GeneralUtils.replaceElements!(mask, 1, a, 0) GeneralUtils.replaceElements!(mask, 1, a, 0)
synapseReconnectDelayCounter[:,:,i] = a synapseReconnectDelay[:,:,i] = a
else else
remaining = 0 remaining = 0
if subToFireNeuron_current[1,1,i] < subToFireNeuron_toBe if subToFireNeuron_current[1,1,i] < subToFireNeuron_toBe
@@ -540,7 +550,7 @@ function neuroplasticity(synapseConnectionNumber,
# add new conn to firing neurons pool # add new conn to firing neurons pool
remaining = addNewSynapticConn!(zitMask[:,:,i], 1, remaining = addNewSynapticConn!(zitMask[:,:,i], 1,
@view(wRec[:,:,i]), @view(wRec[:,:,i]),
@view(synapseReconnectDelayCounter[:,:,i]), @view(synapseReconnectDelay[:,:,i]),
toAddConn) toAddConn)
totalNewConn[1,1,i] += remaining totalNewConn[1,1,i] += remaining
end end
@@ -548,12 +558,12 @@ function neuroplasticity(synapseConnectionNumber,
# add new conn to non-firing neurons pool # add new conn to non-firing neurons pool
remaining = addNewSynapticConn!(zitMask[:,:,i], 0, remaining = addNewSynapticConn!(zitMask[:,:,i], 0,
@view(wRec[:,:,i]), @view(wRec[:,:,i]),
@view(synapseReconnectDelayCounter[:,:,i]), @view(synapseReconnectDelay[:,:,i]),
totalNewConn[1,1,i]) totalNewConn[1,1,i])
if remaining > 0 # final get-all round if somehow non-firing pool has not enough slot if remaining > 0 # final get-all round if somehow non-firing pool has not enough slot
remaining = addNewSynapticConn!(zitMask[:,:,i], 1, remaining = addNewSynapticConn!(zitMask[:,:,i], 1,
@view(wRec[:,:,i]), @view(wRec[:,:,i]),
@view(synapseReconnectDelayCounter[:,:,i]), @view(synapseReconnectDelay[:,:,i]),
remaining) remaining)
end end
end end
@@ -568,9 +578,8 @@ function alifLearn(wRec,
exInType, exInType,
arrayProjection4d, arrayProjection4d,
neuronInactivityCounter, neuronInactivityCounter,
synapseReconnectDelayCounter, synapseReconnectDelay,
synapseConnectionNumber, synapseConnectionNumber,
synapticWChangeCounter, #TODO
eta, eta,
vt, vt,
zitCumulative, zitCumulative,
@@ -587,15 +596,15 @@ function alifLearn(wRec,
eta_cpu = eta_cpu[:,:,:,1] eta_cpu = eta_cpu[:,:,:,1]
neuronInactivityCounter_cpu = neuronInactivityCounter |> cpu neuronInactivityCounter_cpu = neuronInactivityCounter |> cpu
neuronInactivityCounter_cpu = neuronInactivityCounter_cpu[:,:,:,1] # (row, col, n) neuronInactivityCounter_cpu = neuronInactivityCounter_cpu[:,:,:,1] # (row, col, n)
synapseReconnectDelayCounter_cpu = synapseReconnectDelayCounter |> cpu synapseReconnectDelay_cpu = synapseReconnectDelay |> cpu
synapseReconnectDelayCounter_cpu = synapseReconnectDelayCounter_cpu[:,:,:,1] synapseReconnectDelay_cpu = synapseReconnectDelay_cpu[:,:,:,1]
zitCumulative_cpu = zitCumulative |> cpu zitCumulative_cpu = zitCumulative |> cpu
zitCumulative_cpu = zitCumulative_cpu[:,:,1] # (row, col) zitCumulative_cpu = zitCumulative_cpu[:,:,1] # (row, col)
# -W if less than 10% of repeat avg, +W otherwise # -W if less than 10% of repeat avg, +W otherwise
_, _, i3 = size(wRec_cpu) _, _, i3 = size(wRec_cpu)
for i in 1:i3 for i in 1:i3
x = 0.1 * (sum(synapseReconnectDelayCounter[:,:,i]) / length(synapseReconnectDelayCounter[:,:,i])) x = 0.1 * (sum(synapseReconnectDelay[:,:,i]) / length(synapseReconnectDelay[:,:,i]))
mask = GeneralUtils.replaceLessThan.(wRec_cpu[:,:,i], x, -1, 1) mask = GeneralUtils.replaceLessThan.(wRec_cpu[:,:,i], x, -1, 1)
wRec_cpu[:,:,i] .+= mask .* eta_cpu[:,:,i] .* wRec_cpu[:,:,i] wRec_cpu[:,:,i] .+= mask .* eta_cpu[:,:,i] .* wRec_cpu[:,:,i]
end end
@@ -608,7 +617,7 @@ function alifLearn(wRec,
zitCumulative_cpu, zitCumulative_cpu,
wRec_cpu, wRec_cpu,
neuronInactivityCounter_cpu, neuronInactivityCounter_cpu,
synapseReconnectDelayCounter_cpu) synapseReconnectDelay_cpu)
wRec_cpu = wRec_cpu .* arrayProjection4d_cpu wRec_cpu = wRec_cpu .* arrayProjection4d_cpu
wRec = wRec_cpu |> device wRec = wRec_cpu |> device
@@ -616,11 +625,11 @@ function alifLearn(wRec,
neuronInactivityCounter_cpu = neuronInactivityCounter_cpu .* arrayProjection4d_cpu neuronInactivityCounter_cpu = neuronInactivityCounter_cpu .* arrayProjection4d_cpu
neuronInactivityCounter = neuronInactivityCounter_cpu |> device neuronInactivityCounter = neuronInactivityCounter_cpu |> device
synapseReconnectDelayCounter_cpu = synapseReconnectDelayCounter_cpu .* arrayProjection4d_cpu synapseReconnectDelay_cpu = synapseReconnectDelay_cpu .* arrayProjection4d_cpu
synapseReconnectDelayCounter = synapseReconnectDelayCounter_cpu |> device synapseReconnectDelay = synapseReconnectDelay_cpu |> device
# error("DEBUG -> alifLearn! $(Dates.now())") # error("DEBUG -> alifLearn! $(Dates.now())")
return wRec, neuronInactivityCounter, synapseReconnectDelayCounter return wRec, neuronInactivityCounter, synapseReconnectDelay
end end
function onLearn!(wOut, function onLearn!(wOut,
@@ -640,7 +649,7 @@ end
# zitCumulative, # (row, col) # zitCumulative, # (row, col)
# wRec, # (row, col, n) # wRec, # (row, col, n)
# neuronInactivityCounter, # neuronInactivityCounter,
# synapseReconnectDelayCounter) # (row, col, n) # synapseReconnectDelay) # (row, col, n)
# i1,i2,i3 = size(wRec) # i1,i2,i3 = size(wRec)
@@ -658,7 +667,7 @@ end
# println("neuroplasticity, from $(synapseConnectionNumber*size(totalNewConn, 3)) conn, $(sum(totalNewConn)) are replaced") # println("neuroplasticity, from $(synapseConnectionNumber*size(totalNewConn, 3)) conn, $(sum(totalNewConn)) are replaced")
# # clear -1.0 marker # # clear -1.0 marker
# GeneralUtils.replaceElements!(wRec, -1.0, synapseReconnectDelayCounter, -0.99) # GeneralUtils.replaceElements!(wRec, -1.0, synapseReconnectDelay, -0.99)
# GeneralUtils.replaceElements!(wRec, -1.0, 0.0) # -1.0 marker is no longer required # GeneralUtils.replaceElements!(wRec, -1.0, 0.0) # -1.0 marker is no longer required
# for i in 1:i3 # for i in 1:i3
@@ -671,7 +680,7 @@ end
# a = similar(w) .= -0.99 # synapseConnectionNumber of this neuron # a = similar(w) .= -0.99 # synapseConnectionNumber of this neuron
# mask = (!iszero).(w) # mask = (!iszero).(w)
# GeneralUtils.replaceElements!(mask, 1, a, 0) # GeneralUtils.replaceElements!(mask, 1, a, 0)
# synapseReconnectDelayCounter[:,:,i] = a # synapseReconnectDelay[:,:,i] = a
# else # else
# remaining = 0 # remaining = 0
# if subToFireNeuron_current[1,1,i] < subToFireNeuron_toBe # if subToFireNeuron_current[1,1,i] < subToFireNeuron_toBe
@@ -680,7 +689,7 @@ end
# # add new conn to firing neurons pool # # add new conn to firing neurons pool
# remaining = addNewSynapticConn!(zitMask[:,:,i], 1, # remaining = addNewSynapticConn!(zitMask[:,:,i], 1,
# @view(wRec[:,:,i]), # @view(wRec[:,:,i]),
# @view(synapseReconnectDelayCounter[:,:,i]), # @view(synapseReconnectDelay[:,:,i]),
# toAddConn) # toAddConn)
# totalNewConn[1,1,i] += remaining # totalNewConn[1,1,i] += remaining
# end # end
@@ -688,12 +697,12 @@ end
# # add new conn to non-firing neurons pool # # add new conn to non-firing neurons pool
# remaining = addNewSynapticConn!(zitMask[:,:,i], 0, # remaining = addNewSynapticConn!(zitMask[:,:,i], 0,
# @view(wRec[:,:,i]), # @view(wRec[:,:,i]),
# @view(synapseReconnectDelayCounter[:,:,i]), # @view(synapseReconnectDelay[:,:,i]),
# totalNewConn[1,1,i]) # totalNewConn[1,1,i])
# if remaining > 0 # final get-all round if somehow non-firing pool has not enough slot # if remaining > 0 # final get-all round if somehow non-firing pool has not enough slot
# remaining = addNewSynapticConn!(zitMask[:,:,i], 1, # remaining = addNewSynapticConn!(zitMask[:,:,i], 1,
# @view(wRec[:,:,i]), # @view(wRec[:,:,i]),
# @view(synapseReconnectDelayCounter[:,:,i]), # @view(synapseReconnectDelay[:,:,i]),
# remaining) # remaining)
# end # end
# end # end

View File

@@ -59,9 +59,9 @@ Base.@kwdef mutable struct kfn_1 <: knowledgeFn
lif_firingCounter::Union{AbstractArray, Nothing} = nothing lif_firingCounter::Union{AbstractArray, Nothing} = nothing
lif_firingTargetFrequency::Union{AbstractArray, Nothing} = nothing lif_firingTargetFrequency::Union{AbstractArray, Nothing} = nothing
lif_neuronInactivityCounter::Union{AbstractArray, Nothing} = nothing lif_neuronInactivityCounter::Union{AbstractArray, Nothing} = nothing
lif_synapseReconnectDelayCounter::Union{AbstractArray, Nothing} = nothing lif_synapseReconnectDelay::Union{AbstractArray, Nothing} = nothing
lif_synapseConnectionNumber::Union{Int, Nothing} = nothing lif_synapseConnectionNumber::Union{Int, Nothing} = nothing
lif_synapticWChangeCounter::Union{AbstractArray, Nothing} = nothing # lif_synapticWChangeCounter::Union{AbstractArray, Nothing} = nothing
# pre-allocation array # pre-allocation array
lif_arrayProjection4d::Union{AbstractArray, Nothing} = nothing # use to project 3d array to 4d lif_arrayProjection4d::Union{AbstractArray, Nothing} = nothing # use to project 3d array to 4d
@@ -100,9 +100,9 @@ Base.@kwdef mutable struct kfn_1 <: knowledgeFn
alif_firingCounter::Union{AbstractArray, Nothing} = nothing alif_firingCounter::Union{AbstractArray, Nothing} = nothing
alif_firingTargetFrequency::Union{AbstractArray, Nothing} = nothing alif_firingTargetFrequency::Union{AbstractArray, Nothing} = nothing
alif_neuronInactivityCounter::Union{AbstractArray, Nothing} = nothing alif_neuronInactivityCounter::Union{AbstractArray, Nothing} = nothing
alif_synapseReconnectDelayCounter::Union{AbstractArray, Nothing} = nothing alif_synapseReconnectDelay::Union{AbstractArray, Nothing} = nothing
alif_synapseConnectionNumber::Union{Int, Nothing} = nothing alif_synapseConnectionNumber::Union{Int, Nothing} = nothing
alif_synapticWChangeCounter::Union{AbstractArray, Nothing} = nothing # alif_synapticWChangeCounter::Union{AbstractArray, Nothing} = nothing
# pre-allocation array # pre-allocation array
alif_arrayProjection4d::Union{AbstractArray, Nothing} = nothing # use to project 3d array to 4d alif_arrayProjection4d::Union{AbstractArray, Nothing} = nothing # use to project 3d array to 4d
@@ -189,7 +189,7 @@ function kfn_1(params::Dict; device=cpu)
# activation matrix # activation matrix
kfn.zit = zeros(row, col, batch) |> device kfn.zit = zeros(row, col, batch) |> device
kfn.zitCumulative = (similar(kfn.zit) .= 0) kfn.zitCumulative = zeros(row, col, 1, batch) |> device
kfn.modelError = zeros(1) |> device kfn.modelError = zeros(1) |> device
kfn.bk = rand(size(kfn.zit)...) |> device kfn.bk = rand(size(kfn.zit)...) |> device
@@ -232,17 +232,21 @@ function kfn_1(params::Dict; device=cpu)
# count subscribed synapse activity, just like epsilonRec but without decay. # count subscribed synapse activity, just like epsilonRec but without decay.
# use to adjust weight based on how often neural pathway is used # use to adjust weight based on how often neural pathway is used
kfn.lif_synapseReconnectDelayCounter = Array(similar(kfn.lif_wRec) .= -0.99) # -0.99 for non-sub conn kfn.lif_synapseReconnectDelay = Array(similar(kfn.lif_wRec) .= -0.99) # -0.99 for non-sub conn
mask = Array((!iszero).(kfn.lif_wRec)) mask = Array((!iszero).(kfn.lif_wRec))
# initial value subscribed conn, synapseReconnectDelayCounter range -10000 to +10000 # initial value subscribed conn
GeneralUtils.replaceElements!(mask, 1, kfn.lif_synapseReconnectDelayCounter, 0) for i in eachindex(mask)
kfn.lif_synapseReconnectDelayCounter = kfn.lif_synapseReconnectDelayCounter |> device if mask[i] == 1
kfn.lif_synapseReconnectDelay[i] = rand(1:100)
end
end
kfn.lif_synapseReconnectDelay = kfn.lif_synapseReconnectDelay |> device
kfn.lif_synapticWChangeCounter = Array(similar(kfn.lif_wRec) .= -0.99) # -0.99 for non-sub conn # kfn.lif_synapticWChangeCounter = Array(similar(kfn.lif_wRec) .= -0.99) # -0.99 for non-sub conn
mask = Array((!iszero).(kfn.lif_wRec)) # mask = Array((!iszero).(kfn.lif_wRec))
# initial value subscribed conn, synapseReconnectDelayCounter range -10000 to +10000 # # initial value subscribed conn, synapseReconnectDelay range -10000 to +10000
GeneralUtils.replaceElements!(mask, 1, kfn.lif_synapticWChangeCounter, 1.0) # GeneralUtils.replaceElements!(mask, 1, kfn.lif_synapticWChangeCounter, 1.0)
kfn.lif_synapticWChangeCounter = kfn.lif_synapticWChangeCounter |> device # kfn.lif_synapticWChangeCounter = kfn.lif_synapticWChangeCounter |> device
kfn.lif_arrayProjection4d = (similar(kfn.lif_wRec) .= 1) kfn.lif_arrayProjection4d = (similar(kfn.lif_wRec) .= 1)
kfn.lif_recSignal = (similar(kfn.lif_wRec) .= 0) kfn.lif_recSignal = (similar(kfn.lif_wRec) .= 0)
@@ -287,16 +291,21 @@ function kfn_1(params::Dict; device=cpu)
kfn.alif_firingCounter = (similar(kfn.alif_wRec) .= 0) kfn.alif_firingCounter = (similar(kfn.alif_wRec) .= 0)
kfn.alif_firingTargetFrequency = (similar(kfn.alif_wRec) .= 0.1) kfn.alif_firingTargetFrequency = (similar(kfn.alif_wRec) .= 0.1)
kfn.alif_neuronInactivityCounter = (similar(kfn.alif_wRec) .= 0) kfn.alif_neuronInactivityCounter = (similar(kfn.alif_wRec) .= 0)
kfn.alif_synapseReconnectDelayCounter = Array(similar(kfn.alif_wRec) .= -0.99) # -9 for non-sub conn kfn.alif_synapseReconnectDelay = Array(similar(kfn.alif_wRec) .= -0.99) # -9 for non-sub conn
mask = Array((!iszero).(kfn.alif_wRec)) mask = Array((!iszero).(kfn.alif_wRec))
# initial value subscribed conn, synapseReconnectDelayCounter range -10000 to +10000 # initial value subscribed conn
GeneralUtils.replaceElements!(mask, 1, kfn.alif_synapseReconnectDelayCounter, 0) for i in eachindex(mask)
kfn.alif_synapseReconnectDelayCounter = kfn.alif_synapseReconnectDelayCounter |> device if mask[i] == 1
kfn.alif_synapticWChangeCounter = Array(similar(kfn.alif_wRec) .= -0.99) # -9 for non-sub conn kfn.alif_synapseReconnectDelay[i] = rand(1:100)
mask = Array((!iszero).(kfn.alif_wRec)) end
# initial value subscribed conn, synapseReconnectDelayCounter range -10000 to +10000 end
GeneralUtils.replaceElements!(mask, 1, kfn.alif_synapticWChangeCounter, 1.0) kfn.alif_synapseReconnectDelay = kfn.alif_synapseReconnectDelay |> device
kfn.alif_synapticWChangeCounter = kfn.alif_synapticWChangeCounter |> device
# kfn.alif_synapticWChangeCounter = Array(similar(kfn.alif_wRec) .= -0.99) # -9 for non-sub conn
# mask = Array((!iszero).(kfn.alif_wRec))
# # initial value subscribed conn, synapseReconnectDelay range -10000 to +10000
# GeneralUtils.replaceElements!(mask, 1, kfn.alif_synapticWChangeCounter, 1.0)
# kfn.alif_synapticWChangeCounter = kfn.alif_synapticWChangeCounter |> device
kfn.alif_arrayProjection4d = (similar(kfn.alif_wRec) .= 1) kfn.alif_arrayProjection4d = (similar(kfn.alif_wRec) .= 1)
kfn.alif_recSignal = (similar(kfn.alif_wRec) .= 0) kfn.alif_recSignal = (similar(kfn.alif_wRec) .= 0)