84 lines
1.8 KiB
Julia
84 lines
1.8 KiB
Julia
module readout
|
|
|
|
using Flux.Optimise: apply!
|
|
|
|
using Statistics, Flux, Random, LinearAlgebra
|
|
using GeneralUtils
|
|
using ..types, ..readout, ..learn, ..forward
|
|
|
|
export readout!
|
|
|
|
#------------------------------------------------------------------------------------------------100
|
|
|
|
function readout!(kfn::knowledgeFn; correctAnswer=nothing) # correctAnswer=nothing use for inference
|
|
# clear output to start reading
|
|
# kfn.on_out_t0 *= 0.0 #FIXME should I clear it before RSNN readout?
|
|
respondCount = zeros(length(kfn.on_out_t0))
|
|
|
|
# prepare signal used to read RSNN
|
|
readoutSignal = zeros(length(kfn.passthrough_zt0))
|
|
readoutSignal[1] = 1
|
|
readoutSignal[end] = 1
|
|
|
|
lastKfnTimeStamp = kfn.timeStamp[1]
|
|
for t in 1:kfn.on_tauOut[1]
|
|
# println("t $t")
|
|
tick = lastKfnTimeStamp + t
|
|
if t == kfn.on_tauOut[1]
|
|
println("")
|
|
end
|
|
if kfn.learningStage[1] == 0 # RSNN is in inference mode, do not change marker
|
|
# skip
|
|
else # RSNN is in learning mode, assign marker for commiting wChange at the end of readout window.
|
|
marker = t == kfn.on_tauOut[1] ? 4 : kfn.learningStage[1]
|
|
end
|
|
|
|
# RSNN forward ----------
|
|
singleTimeReadout, on_out_t0, softmaxRespond = kfn(readoutSignal, tick, marker,
|
|
correctAnswer=correctAnswer)
|
|
_, _, respondPosition = Utils.findMax(softmaxRespond)
|
|
respondCount += respondPosition
|
|
|
|
if correctAnswer !== nothing
|
|
kfn.kfnError = [Flux.logitcrossentropy(on_out_t0, correctAnswer)]
|
|
learn!(kfn)
|
|
end
|
|
end
|
|
|
|
_, readout, _ = Utils.findMax(respondCount/kfn.on_tauOut[1])
|
|
|
|
return readout, kfn.on_out_t0
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
end # module
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|