clear marker
This commit is contained in:
115
src/learn.jl
115
src/learn.jl
@@ -270,8 +270,9 @@ function learn!(kfn::kfn_1, device=cpu)
|
||||
kfn.lif_wRecChange,
|
||||
kfn.lif_arrayProjection4d,
|
||||
kfn.lif_neuronInactivityCounter,
|
||||
kfn.lif_synapticInactivityCounter,
|
||||
kfn.lif_synapticConnectionNumber,
|
||||
kfn.zit_cumulative,
|
||||
kfn.zitCumulative,
|
||||
device)
|
||||
|
||||
# alif learn
|
||||
@@ -279,8 +280,9 @@ function learn!(kfn::kfn_1, device=cpu)
|
||||
kfn.alif_wRecChange,
|
||||
kfn.alif_arrayProjection4d,
|
||||
kfn.alif_neuronInactivityCounter,
|
||||
kfn.lif_synapticInactivityCounter,
|
||||
kfn.alif_synapticConnectionNumber,
|
||||
kfn.zit_cumulative,
|
||||
kfn.zitCumulative,
|
||||
device)
|
||||
|
||||
# on learn
|
||||
@@ -298,31 +300,38 @@ end
|
||||
function lifLearn!(wRec,
|
||||
wRecChange,
|
||||
arrayProjection4d,
|
||||
inactivityCounter,
|
||||
neuronInactivityCounter,
|
||||
synapticInactivityCounter,
|
||||
synapticConnectionNumber,
|
||||
zit_cumulative,
|
||||
zitCumulative,
|
||||
device)
|
||||
# merge learning weight with average learning weight of all batch
|
||||
wRec .+= (sum(wRecChange, dims=4) ./ (size(wRec, 4))) .* arrayProjection4d
|
||||
|
||||
wRec_cpu = wRec |> cpu
|
||||
wRec_cpu = wRec_cpu[:,:,:,1] # since every batch has the same neuron wRec, (row, col, n)
|
||||
inactivityCounter_cpu = inactivityCounter |> cpu
|
||||
inactivityCounter_cpu = inactivityCounter_cpu[:,:,:,1] # (row, col, n)
|
||||
zit_cumulative_cpu = zit_cumulative |> cpu
|
||||
zit_cumulative_cpu = zit_cumulative_cpu[:,:,1] # (row, col)
|
||||
neuronInactivityCounter_cpu = neuronInactivityCounter |> cpu
|
||||
neuronInactivityCounter_cpu = neuronInactivityCounter_cpu[:,:,:,1] # (row, col, n)
|
||||
synapticInactivityCounter_cpu = synapticInactivityCounter |> cpu
|
||||
synapticInactivityCounter_cpu = synapticInactivityCounter_cpu[:,:,:,1]
|
||||
zitCumulative_cpu = zitCumulative |> cpu
|
||||
zitCumulative_cpu = zitCumulative_cpu[:,:,1] # (row, col)
|
||||
|
||||
# weak / negative synaptic connection will get randomed in neuroplasticity()
|
||||
wRec_cpu = GeneralUtils.replaceBetween.(wRec_cpu, 0.0, 0.1, -1.0) # mark with -1.0
|
||||
|
||||
# synaptic connection that has no inactivity will get randomed in neuroplasticity()
|
||||
GeneralUtils.replace_elements!(inactivityCounter_cpu, 0.0, wRec_cpu, -1.0)
|
||||
# reset lif_inactivity elements to 10000
|
||||
GeneralUtils.replace_elements!(inactivityCounter_cpu, 0.0, -9.0) # -9.0 is base value
|
||||
GeneralUtils.replace_elements!(neuronInactivityCounter_cpu, 0.0, wRec_cpu, -1.0)
|
||||
# reset lif_inactivity elements to -9
|
||||
GeneralUtils.replace_elements!(neuronInactivityCounter_cpu, 0.0, -9.0) # -9.0 is base value
|
||||
|
||||
|
||||
#WORKING neuroplasticity
|
||||
wRec_cpu = neuroplasticity(synapticConnectionNumber, zit_cumulative_cpu, wRec_cpu,
|
||||
inactivityCounter_cpu)
|
||||
wRec_cpu = neuroplasticity(synapticConnectionNumber,
|
||||
zitCumulative_cpu,
|
||||
wRec_cpu,
|
||||
neuronInactivityCounter_cpu,
|
||||
synapticInactivityCounter_cpu)
|
||||
error("DEBUG -> lifLearn! $(Dates.now())")
|
||||
# #TODO send to device with correct dimension
|
||||
# wRec = wRec |> device
|
||||
@@ -333,9 +342,10 @@ end
|
||||
function alifLearn!(wRec,
|
||||
wRecChange,
|
||||
arrayProjection4d,
|
||||
inactivityCounter,
|
||||
neuronInactivityCounter,
|
||||
synapticInactivityCounter,
|
||||
synapticConnectionNumber,
|
||||
zit_cumulative,
|
||||
zitCumulative,
|
||||
device)
|
||||
# merge learning weight with average learning weight
|
||||
wRec .+= (sum(wRecChange, dims=4) ./ (size(wRec, 4))) .* arrayProjection4d
|
||||
@@ -365,9 +375,10 @@ function onLearn!(wOut,
|
||||
end
|
||||
|
||||
function neuroplasticity(synapticConnectionNumber,
|
||||
zit_cumulative, # (row, col)
|
||||
zitCumulative, # (row, col)
|
||||
wRec, # (row, col, n)
|
||||
inactivityCounter_cpu) # (row, col, n)
|
||||
neuronInactivityCounter, #WORKING neuron die i.e. reset all weight
|
||||
synapticInactivityCounter) # (row, col, n)
|
||||
|
||||
i1,i2,i3 = size(wRec)
|
||||
|
||||
@@ -376,42 +387,52 @@ function neuroplasticity(synapticConnectionNumber,
|
||||
subToFireNeuron_toBe = Int(floor(0.7 * synapticConnectionNumber))
|
||||
subToNonFiringNeuron_toBe = synapticConnectionNumber - subToFireNeuron_toBe
|
||||
|
||||
#WORKING for each neuron, count how many synap already subscribed to firing-neurons
|
||||
subToFireNeuron_current = sum((!iszero).(zit_cumulative .* wRec), dims=(1,2)) # (1, 1, n)
|
||||
subToNonFiringNeuron_current = synapticConnectionNumber .- subToFireNeuron_current # (1, 1, n)
|
||||
mask = (!iszero).(zit_cumulative) # mask of firing neurons = 1, non-firing = 0
|
||||
# for each neuron, count how many synap already subscribed to firing-neurons
|
||||
zw = zitCumulative .* wRec
|
||||
subToFireNeuron_current = sum(GeneralUtils.isBetween.(zw, 0.0, 100.0), dims=(1,2)) # (1, 1, n)
|
||||
zitMask = (!iszero).(zitCumulative) # zitMask of firing neurons = 1, non-firing = 0
|
||||
projection = ones(i1,i2,i3)
|
||||
mask = mask .* projection # (row, col, n)
|
||||
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("mask ", size(mask))
|
||||
println("wRec ", size(wRec))
|
||||
println("inactivityCounter_cpu ", size(inactivityCounter_cpu))
|
||||
println("totalNeurons ", totalNewConn, size(totalNewConn))
|
||||
error("DEBUG -> neuroplasticity $(Dates.now())")
|
||||
|
||||
#WORKING clear -1.0 marker
|
||||
GeneralUtils.replace_elements!(wRec, -1.0, synapticInactivityCounter, -9.0)
|
||||
GeneralUtils.replace_elements!(wRec, -1.0, 0.0) # -1.0 marker is no longer required
|
||||
|
||||
println("/////////")
|
||||
println("wRec 1 ", wRec[:,:,1])
|
||||
println("synapticInactivityCounter 1 ", synapticInactivityCounter[:,:,1])
|
||||
|
||||
for i in 1:i3
|
||||
|
||||
|
||||
# add new conn to firing neurons pool
|
||||
remaining = GeneralUtils.replace_elements(mask[:,:,i],
|
||||
1,
|
||||
wRecmask[:,:,i],
|
||||
inactivityCounter_cpumask[:,:,i],
|
||||
totalNewConn[:,:,i])
|
||||
remaining = 0
|
||||
if subToFireNeuron_current[1,1,i] < subToFireNeuron_toBe
|
||||
toAddConn = subToFireNeuron_toBe - subToFireNeuron_current[1,1,i]
|
||||
totalNewConn[1,1,i] = totalNewConn[1,1,i] - toAddConn
|
||||
# add new conn to firing neurons pool
|
||||
remaining = addNewSynapticConn!(zitMask[:,:,i], 1,
|
||||
@view(wRec[:,:,i]),
|
||||
@view(synapticInactivityCounter[:,:,i]),
|
||||
toAddConn)
|
||||
totalNewConn[1,1,i] += remaining
|
||||
end
|
||||
|
||||
#TODO add new conn to non-firing neurons pool
|
||||
|
||||
# add new conn to non-firing neurons pool
|
||||
remaining = addNewSynapticConn!(zitMask[:,:,i], 0,
|
||||
@view(wRec[:,:,i]),
|
||||
@view(synapticInactivityCounter[:,:,i]),
|
||||
totalNewConn[1,1,i])
|
||||
if remaining > 0 # final get-all round if somehow non-firing pool has not enough slot
|
||||
remaining = addNewSynapticConn!(zitMask[:,:,i], 1,
|
||||
@view(wRec[:,:,i]),
|
||||
@view(synapticInactivityCounter[:,:,i]),
|
||||
remaining)
|
||||
end
|
||||
end
|
||||
println("==========")
|
||||
println("wRec 2 ", wRec[:,:,1])
|
||||
println("synapticInactivityCounter 2 ", synapticInactivityCounter[:,:,1])
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
newFiringConn = subToFireNeuron_toBe - subToFireNeuron_current
|
||||
newFiringConn = newFiringConn > 0 ? newFiringConn : 0
|
||||
|
||||
newNonFiringConn = subToNonFiringNeuron_toBe - subToNonFiringNeuron_current
|
||||
|
||||
# error("DEBUG -> neuroplasticity $(Dates.now())")
|
||||
return wRec
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user