From ef72f7949aaefc0dec7cefaa30a281e93b2770c2 Mon Sep 17 00:00:00 2001 From: ton Date: Sat, 26 Aug 2023 11:16:45 +0700 Subject: [PATCH] temp --- src/forward.jl | 4 +-- src/learn.jl | 86 ++++++++++++++++++++++++++++++++++++++++++++++---- src/type.jl | 49 ++++++++++++---------------- 3 files changed, 103 insertions(+), 36 deletions(-) diff --git a/src/forward.jl b/src/forward.jl index 36e441b..79bdc93 100644 --- a/src/forward.jl +++ b/src/forward.jl @@ -273,7 +273,7 @@ function lifForward( zit, vt[i1,i2,i3,i4] = vRest[i1,i2,i3,i4] # reset counter if neuron fires - neuronInactivityCounter[i1,i2,i3,i4] = 10000 + neuronInactivityCounter[i1,i2,i3,i4] = 0 else zt[i1,i2,i3,i4] = 0 neuronInactivityCounter[i1,i2,i3,i4] -= 1 @@ -456,7 +456,7 @@ function alifForward( zit, firingCounter[i1,i2,i3,i4] += 1 vt[i1,i2,i3,i4] = vRest[i1,i2,i3,i4] a[i1,i2,i3,i4] = (rho[i1,i2,i3,i4] * a[i1,i2,i3,i4]) + 1 - neuronInactivityCounter[i1,i2,i3,i4] = 10000 + neuronInactivityCounter[i1,i2,i3,i4] = 0 else zt[i1,i2,i3,i4] = 0 a[i1,i2,i3,i4] = (rho[i1,i2,i3,i4] * a[i1,i2,i3,i4]) diff --git a/src/learn.jl b/src/learn.jl index f1f989b..b7a2cb3 100644 --- a/src/learn.jl +++ b/src/learn.jl @@ -325,7 +325,6 @@ function lifLearn!(wRec, # 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, zitCumulative_cpu, @@ -377,15 +376,17 @@ end function neuroplasticity(synapticConnectionNumber, zitCumulative, # (row, col) wRec, # (row, col, n) - neuronInactivityCounter, #WORKING neuron die i.e. reset all weight - synapticInactivityCounter) # (row, col, n) + neuronInactivityCounter, + synapticInactivityCounter) # (row, col, n) + #WORKING + # - synapticInactivityCounter -10000 to 10000, weight liquidity range from 1.0 to 0.1 respectively + # - at synapticInactivityCounter -10000 mark -1.0 for new conn i1,i2,i3 = size(wRec) # for each neuron, find total number of synaptic conn that should draw # new connection to firing and non-firing neurons pool subToFireNeuron_toBe = Int(floor(0.7 * synapticConnectionNumber)) - subToNonFiringNeuron_toBe = synapticConnectionNumber - subToFireNeuron_toBe # for each neuron, count how many synap already subscribed to firing-neurons zw = zitCumulative .* wRec @@ -395,17 +396,23 @@ function neuroplasticity(synapticConnectionNumber, zitMask = zitMask .* projection # (row, col, n) totalNewConn = sum(isequal.(wRec, -1.0), dims=(1,2)) # count new conn mark (-1.0), (1, 1, n) - #WORKING clear -1.0 marker + # 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 println("/////////") + println("zitCumulative ", zitCumulative) + println("neuronInactivityCounter ", size(neuronInactivityCounter)) println("wRec 1 ", wRec[:,:,1]) - println("synapticInactivityCounter 1 ", synapticInactivityCounter[:,:,1]) + println("synapticInactivityCounter 1 ", neuronInactivityCounter[1,1,:], synapticInactivityCounter[:,:,1]) println("wRec 2 ", wRec[:,:,2]) println("synapticInactivityCounter 2 ", synapticInactivityCounter[:,:,2]) for i in 1:i3 + # if neuronInactivityCounter[1:1:i] <= -10000 + # w = wRec(i1,i2,1,) #WORKING neuron die i.e. reset all weight + + remaining = 0 if subToFireNeuron_current[1,1,i] < subToFireNeuron_toBe toAddConn = subToFireNeuron_toBe - subToFireNeuron_current[1,1,i] @@ -442,6 +449,73 @@ end +# function neuroplasticity(synapticConnectionNumber, +# zitCumulative, # (row, col) +# wRec, # (row, col, n) +# neuronInactivityCounter, #WORKING neuron die i.e. reset all weight +# synapticInactivityCounter) # (row, col, n) + +# i1,i2,i3 = size(wRec) + +# # for each neuron, find total number of synaptic conn that should draw +# # new connection to firing and non-firing neurons pool +# subToFireNeuron_toBe = Int(floor(0.7 * synapticConnectionNumber)) +# subToNonFiringNeuron_toBe = synapticConnectionNumber - subToFireNeuron_toBe + +# # 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) +# zitMask = zitMask .* projection # (row, col, n) +# totalNewConn = sum(isequal.(wRec, -1.0), dims=(1,2)) # count new conn mark (-1.0), (1, 1, n) + +# # 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 + +# println("/////////") +# println("zitCumulative ", zitCumulative) +# println("wRec 1 ", wRec[:,:,1]) +# println("synapticInactivityCounter 1 ", synapticInactivityCounter[:,:,1]) +# println("wRec 2 ", wRec[:,:,2]) +# println("synapticInactivityCounter 2 ", synapticInactivityCounter[:,:,2]) + +# for i in 1:i3 +# 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 + +# # 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 1 ", wRec[:,:,1]) +# println("synapticInactivityCounter 1 ", synapticInactivityCounter[:,:,1]) +# println("wRec 2 ", wRec[:,:,2]) +# println("synapticInactivityCounter 2 ", synapticInactivityCounter[:,:,2]) + +# # error("DEBUG -> neuroplasticity $(Dates.now())") +# return wRec +# end + diff --git a/src/type.jl b/src/type.jl index d05fbc3..0350828 100644 --- a/src/type.jl +++ b/src/type.jl @@ -196,20 +196,8 @@ function kfn_1(params::Dict; device=cpu) lif_n = kfn.params[:computeNeuron][:lif][:numbers][1] * kfn.params[:computeNeuron][:lif][:numbers][2] # subscription - w = zeros(row, col, lif_n) synapticConnectionPercent = kfn.params[:computeNeuron][:lif][:params][:synapticConnectionPercent] - kfn.lif_synapticConnectionNumber = Int(floor(row*col * synapticConnectionPercent/100)) - for slice in eachslice(w, dims=3) - pool = shuffle!([1:row*col...])[1:kfn.lif_synapticConnectionNumber] - for i in pool - slice[i] = rand() # assign weight to synaptic connection. /10 to start small, - # otherwise RSNN's vt Usually stay negative (-) - end - end - - # 10% of neuron connection should be enough to start to make neuron fires - should_be_avg_weight = 1 / (0.1 * lif_n) - w = w .* (should_be_avg_weight / maximum(w)) # adjust overall weight + kfn.lif_synapticConnectionNumber, w = wRec(row, col, lif_n, synapticConnectionPercent) # project 3D w into 4D kfn.lif_wRec (row, col, n, batch) kfn.lif_wRec = reshape(w, (row, col, lif_n, 1)) .* ones(row, col, lif_n, batch) |> device @@ -234,7 +222,7 @@ function kfn_1(params::Dict; device=cpu) kfn.lif_firingCounter = (similar(kfn.lif_wRec) .= 0) kfn.lif_firingTargetFrequency = (similar(kfn.lif_wRec) .= 0.1) - kfn.lif_neuronInactivityCounter = (similar(kfn.lif_wRec) .= 10000) + kfn.lif_neuronInactivityCounter = (similar(kfn.lif_wRec) .= 0) kfn.lif_synapticInactivityCounter = Array(similar(kfn.lif_wRec) .= -0.99) # -9 for non-sub conn mask = Array((!iszero).(kfn.lif_wRec)) GeneralUtils.replace_elements!(mask, 1, kfn.lif_synapticInactivityCounter, 0) # initial value subscribed conn @@ -255,20 +243,8 @@ function kfn_1(params::Dict; device=cpu) alif_n = kfn.params[:computeNeuron][:alif][:numbers][1] * kfn.params[:computeNeuron][:alif][:numbers][2] # subscription - w = zeros(row, col, alif_n) synapticConnectionPercent = kfn.params[:computeNeuron][:alif][:params][:synapticConnectionPercent] - kfn.alif_synapticConnectionNumber = Int(floor(row*col * synapticConnectionPercent/100)) - for slice in eachslice(w, dims=3) - pool = shuffle!([1:row*col...])[1:kfn.alif_synapticConnectionNumber] - for i in pool - slice[i] = rand() # assign weight to synaptic connection. /10 to start small, - # otherwise RSNN's vt Usually stay negative (-) - end - end - - # 10% of neuron connection should be enough to start to make neuron fires - should_be_avg_weight = 1 / (0.1 * alif_n) - w = w .* (should_be_avg_weight / maximum(w)) # adjust overall weight + kfn.alif_synapticConnectionNumber, w = wRec(row, col, alif_n, synapticConnectionPercent) # project 3D w into 4D kfn.alif_wRec kfn.alif_wRec = reshape(w, (row, col, alif_n, 1)) .* ones(row, col, alif_n, batch) |> device @@ -293,7 +269,7 @@ function kfn_1(params::Dict; device=cpu) kfn.alif_firingCounter = (similar(kfn.alif_wRec) .= 0) kfn.alif_firingTargetFrequency = (similar(kfn.alif_wRec) .= 0.1) - kfn.alif_neuronInactivityCounter = (similar(kfn.alif_wRec) .= 10000) + kfn.alif_neuronInactivityCounter = (similar(kfn.alif_wRec) .= 0) kfn.alif_synapticInactivityCounter = Array(similar(kfn.alif_wRec) .= -0.99) # -9 for non-sub conn mask = Array((!iszero).(kfn.alif_wRec)) GeneralUtils.replace_elements!(mask, 1, kfn.alif_synapticInactivityCounter, 0) # initial value subscribed conn @@ -383,7 +359,24 @@ function kfn_1(params::Dict; device=cpu) return kfn end +function wRec(row, col, n, synapticConnectionPercent) + # subscription + w = zeros(row, col, n) + synapticConnectionNumber = Int(floor(row*col * synapticConnectionPercent/100)) + for slice in eachslice(w, dims=3) + pool = shuffle!([1:row*col...])[1:synapticConnectionNumber] + for i in pool + slice[i] = rand() # assign weight to synaptic connection. /10 to start small, + # otherwise RSNN's vt Usually stay negative (-) + end + end + # 10% of neuron connection should be enough to start to make neuron fires + should_be_avg_weight = 1 / (0.1 * synapticConnectionNumber) + w = w .* (should_be_avg_weight / maximum(w)) # adjust overall weight + + return synapticConnectionNumber, w +end