version 0.0.7
This commit is contained in:
36
src/learn.jl
36
src/learn.jl
@@ -9,9 +9,6 @@ using ..type, ..snnUtil
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
function compute_paramsChange!(kfn::kfn_1, modelError, outputError)
|
||||
# modelError = reshape(modelError, (1,1,1,:)) # (1,1,1,batch)
|
||||
modelError = reshape(modelError, (1,1,:, size(modelError, 2)))
|
||||
modelError = sum(modelError, dims=3)
|
||||
|
||||
lifComputeParamsChange!(kfn.timeStep,
|
||||
kfn.lif_phi,
|
||||
@@ -19,6 +16,7 @@ function compute_paramsChange!(kfn::kfn_1, modelError, outputError)
|
||||
kfn.lif_eta,
|
||||
kfn.lif_eRec,
|
||||
kfn.lif_wRec,
|
||||
kfn.lif_exInType,
|
||||
kfn.lif_wRecChange,
|
||||
kfn.on_wOut,
|
||||
kfn.lif_firingCounter,
|
||||
@@ -36,6 +34,7 @@ function compute_paramsChange!(kfn::kfn_1, modelError, outputError)
|
||||
kfn.alif_eta,
|
||||
kfn.alif_eRec,
|
||||
kfn.alif_wRec,
|
||||
kfn.alif_exInType,
|
||||
kfn.alif_wRecChange,
|
||||
kfn.on_wOut,
|
||||
kfn.alif_firingCounter,
|
||||
@@ -66,6 +65,7 @@ function lifComputeParamsChange!( timeStep::CuArray,
|
||||
eta::CuArray,
|
||||
eRec::CuArray,
|
||||
wRec::CuArray,
|
||||
exInType::CuArray,
|
||||
wRecChange::CuArray,
|
||||
wOut::CuArray,
|
||||
firingCounter::CuArray,
|
||||
@@ -122,6 +122,7 @@ function alifComputeParamsChange!( timeStep::CuArray,
|
||||
eta::CuArray,
|
||||
eRec::CuArray,
|
||||
wRec::CuArray,
|
||||
exInType::CuArray,
|
||||
wRecChange::CuArray,
|
||||
wOut::CuArray,
|
||||
firingCounter::CuArray,
|
||||
@@ -268,22 +269,26 @@ function learn!(kfn::kfn_1, device=cpu)
|
||||
# lif learn
|
||||
kfn.lif_wRec, kfn.lif_neuronInactivityCounter, kfn.lif_synapticInactivityCounter =
|
||||
lifLearn(kfn.lif_wRec,
|
||||
kfn.lif_exInType,
|
||||
kfn.lif_wRecChange,
|
||||
kfn.lif_arrayProjection4d,
|
||||
kfn.lif_neuronInactivityCounter,
|
||||
kfn.lif_synapticInactivityCounter,
|
||||
kfn.lif_synapticConnectionNumber,
|
||||
kfn.lif_synapticWChangeCounter,
|
||||
kfn.zitCumulative,
|
||||
device)
|
||||
|
||||
# alif learn
|
||||
kfn.alif_wRec, kfn.alif_neuronInactivityCounter, kfn.alif_synapticInactivityCounter =
|
||||
alifLearn(kfn.alif_wRec,
|
||||
kfn.alif_exInType,
|
||||
kfn.alif_wRecChange,
|
||||
kfn.alif_arrayProjection4d,
|
||||
kfn.alif_neuronInactivityCounter,
|
||||
kfn.alif_synapticInactivityCounter,
|
||||
kfn.alif_synapticConnectionNumber,
|
||||
kfn.alif_synapticWChangeCounter,
|
||||
kfn.zitCumulative,
|
||||
device)
|
||||
|
||||
@@ -300,18 +305,19 @@ function learn!(kfn::kfn_1, device=cpu)
|
||||
end
|
||||
|
||||
function lifLearn(wRec,
|
||||
exInType,
|
||||
wRecChange,
|
||||
arrayProjection4d,
|
||||
neuronInactivityCounter,
|
||||
synapticInactivityCounter,
|
||||
synapticConnectionNumber,
|
||||
synapticWChangeCounter, #WORKING
|
||||
zitCumulative,
|
||||
device)
|
||||
#WORKING - synapticInactivityCounter -10000 to 10000, weight change liquidity range from 1.0 to 0.1 respectively
|
||||
|
||||
# merge learning weight with average learning weight of all batch
|
||||
wch = sum(wRecChange, dims=4) ./ (size(wRec, 4)) .* arrayProjection4d
|
||||
wRec .+= wch
|
||||
wRec .= (exInType .* wRec) .+ wch
|
||||
|
||||
arrayProjection4d_cpu = arrayProjection4d |> cpu
|
||||
wRec_cpu = wRec |> cpu
|
||||
@@ -327,7 +333,7 @@ function lifLearn(wRec,
|
||||
wRec_cpu = GeneralUtils.replaceBetween.(wRec_cpu, 0.0, 0.01, -1.0) # mark with -1.0
|
||||
|
||||
# synaptic connection that has no activity will get randomed in neuroplasticity()
|
||||
mask = isless.(synapticInactivityCounter_cpu, -10_000)
|
||||
mask = isless.(synapticInactivityCounter_cpu, -100000)
|
||||
GeneralUtils.replace_elements!(mask, 1, wRec_cpu, -1.0)
|
||||
# reset lif_inactivity elements to base value
|
||||
GeneralUtils.replace_elements!(mask, 1, synapticInactivityCounter_cpu, 0.0)
|
||||
@@ -347,24 +353,25 @@ function lifLearn(wRec,
|
||||
|
||||
synapticInactivityCounter_cpu = synapticInactivityCounter_cpu .* arrayProjection4d_cpu
|
||||
synapticInactivityCounter = synapticInactivityCounter_cpu |> device
|
||||
|
||||
|
||||
# error("DEBUG -> lifLearn! $(Dates.now())")
|
||||
return wRec, neuronInactivityCounter, synapticInactivityCounter
|
||||
end
|
||||
|
||||
function alifLearn(wRec,
|
||||
exInType,
|
||||
wRecChange,
|
||||
arrayProjection4d,
|
||||
neuronInactivityCounter,
|
||||
synapticInactivityCounter,
|
||||
synapticConnectionNumber,
|
||||
synapticWChangeCounter,
|
||||
zitCumulative,
|
||||
device)
|
||||
#WORKING - synapticInactivityCounter -10000 to 10000, weight change liquidity range from 1.0 to 0.1 respectively
|
||||
|
||||
# merge learning weight with average learning weight of all batch
|
||||
wch = sum(wRecChange, dims=4) ./ (size(wRec, 4)) .* arrayProjection4d
|
||||
wRec .+= wch
|
||||
wRec .= (exInType .* wRec) .+ wch
|
||||
|
||||
arrayProjection4d_cpu = arrayProjection4d |> cpu
|
||||
wRec_cpu = wRec |> cpu
|
||||
@@ -380,7 +387,7 @@ function alifLearn(wRec,
|
||||
wRec_cpu = GeneralUtils.replaceBetween.(wRec_cpu, 0.0, 0.01, -1.0) # mark with -1.0
|
||||
|
||||
# synaptic connection that has no activity will get randomed in neuroplasticity()
|
||||
mask = isless.(synapticInactivityCounter_cpu, -10_000)
|
||||
mask = isless.(synapticInactivityCounter_cpu, -100000)
|
||||
GeneralUtils.replace_elements!(mask, 1, wRec_cpu, -1.0)
|
||||
# reset alif_inactivity elements to base value
|
||||
GeneralUtils.replace_elements!(mask, 1, synapticInactivityCounter_cpu, 0.0)
|
||||
@@ -439,17 +446,18 @@ function neuroplasticity(synapticConnectionNumber,
|
||||
projection = ones(i1,i2,i3)
|
||||
zitMask = zitMask .* projection # (row, col, n)
|
||||
totalNewConn = sum(isequal.(wRec, -1.0), dims=(1,2)) # count new conn mark (-1.0), (1, 1, n)
|
||||
println("neuroplasticity, from $synapticConnectionNumber, $totalNewConn are replaced")
|
||||
# println("neuroplasticity, from $synapticConnectionNumber, $totalNewConn are replaced")
|
||||
|
||||
# clear -1.0 marker
|
||||
GeneralUtils.replace_elements!(wRec, -1.0, synapticInactivityCounter, -0.99)
|
||||
GeneralUtils.replace_elements!(wRec, -1.0, 0.0) # -1.0 marker is no longer required
|
||||
|
||||
for i in 1:i3
|
||||
if neuronInactivityCounter[1:1:i][1] < -10_000 # neuron die i.e. reset all weight
|
||||
if neuronInactivityCounter[1:1:i][1] < -10000 # neuron die i.e. reset all weight
|
||||
println("neuron die")
|
||||
neuronInactivityCounter[:,:,i] .= 0 # reset
|
||||
w = wRec(i1,i2,1,synapticConnectionNumber)
|
||||
wRec[:,:,i] = w
|
||||
w = random_wRec(i1,i2,1,synapticConnectionNumber)
|
||||
wRec[:,:,i] .= w
|
||||
|
||||
a = similar(w) .= -0.99 # synapticConnectionNumber of this neuron
|
||||
mask = (!iszero).(w)
|
||||
|
||||
Reference in New Issue
Block a user