refractoring
This commit is contained in:
153
src/DB_services.jl
Normal file
153
src/DB_services.jl
Normal file
@@ -0,0 +1,153 @@
|
||||
module DB_services
|
||||
|
||||
""" version 0.2
|
||||
"""
|
||||
|
||||
using DataStructures: count
|
||||
export send_to_DB, data_prep_for_DB
|
||||
|
||||
using DataStructures
|
||||
using JSON3
|
||||
using Redis
|
||||
using Random
|
||||
using UUIDs
|
||||
|
||||
include("Utils.jl")
|
||||
using .Utils
|
||||
|
||||
"""
|
||||
Dummy iron_pen_ai for raw_data_db_service testing
|
||||
"""
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
|
||||
""" Prepare model data for sending to raw_data_db_service by flattening all hierarchy
|
||||
data structure inside model_data into 1-dept JSON3.
|
||||
This function output is flattened JSON3 data
|
||||
*** all parameter name that is going to Cassandra must not contain a capital letter ***
|
||||
"""
|
||||
function data_prep_for_DB(model_name::String, experiment_number::Int, episode_number::Int,
|
||||
time_stamp::Int, model_data::OrderedDict)::Array{OrderedDict, 1}
|
||||
|
||||
payload_template = OrderedDict{Any, Any}(
|
||||
:model_name => model_name,
|
||||
:knowledgeFn_name => "none",
|
||||
:experiment_number => experiment_number,
|
||||
:episode_number => episode_number,
|
||||
)
|
||||
payloads = []
|
||||
for (k, v) in model_data[:m][:knowledgeFn] # loop over each knowledgeFn
|
||||
payload = deepcopy(payload_template)
|
||||
payload[:knowledgeFn_name] = v[:knowledgefn_name]
|
||||
payload[:neurons_list] = []
|
||||
for (k1, v1) in v
|
||||
if k1 == :neurons_array || k1 == :output_neurons_array
|
||||
for (k2, v2) in v1 # loop over each neuron
|
||||
if k2 != :type # add the following additonal data into neuron's ODict data (already have its parameters in there)
|
||||
neuron = OrderedDict(v2) # v2 is still in JSON3 format but
|
||||
# to be able to add new value to
|
||||
# it, it needs to be in
|
||||
# OrderedDict format
|
||||
|
||||
# # add corresponding knowledgeFn to neuron OrderedDict
|
||||
# neuron[:knowledgefn_name] = v[:knowledgefn_name]
|
||||
|
||||
# add corresponding experiment_number to neuron OrderedDict
|
||||
neuron[:experiment_number] = experiment_number
|
||||
|
||||
# add corresponding episode_number to neuron OrderedDict
|
||||
neuron[:episode_number] = episode_number
|
||||
|
||||
# # add corresponding tick_number to neuron OrderedDict
|
||||
# neuron[:tick_number] = tick_number
|
||||
|
||||
""" add neuron name of itself to neuron OrderedDict
|
||||
since neurons in neurons_array and output_neurons_array has the
|
||||
same name (because its name derived from its position in the
|
||||
array it lives in). In order to store them in the same
|
||||
OrderedDict, I need to change their name so I prefix their name
|
||||
with their array name
|
||||
"""
|
||||
neuron[:neuron_name] = Symbol(string(k1) * "_" * string(k2))
|
||||
|
||||
neuron[:model_error] = model_data[:m][:model_error]
|
||||
|
||||
neuron[:knowledgefn_error] = model_data[:m][:knowledgeFn][k][:knowledgeFn_error]
|
||||
|
||||
neuron[:model_name] = model_name
|
||||
|
||||
# use as identifier durin debug
|
||||
# neuron[:random] = Random.rand(1:100)
|
||||
|
||||
push!(payload[:neurons_list], neuron)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
push!(payloads, payload)
|
||||
end
|
||||
return payloads
|
||||
end
|
||||
|
||||
function send_to_DB(model_name::String, experiment_number::Int, episode_number::Int,
|
||||
tick_number::Int, model_json_string::String, redis_server_ip::String,
|
||||
pub_channel::String, sub_channel::String)
|
||||
model_ordereddict = OrderedDict(JSON3.read(model_json_string))
|
||||
payloads = data_prep_for_DB(model_name, experiment_number, episode_number, tick_number,
|
||||
model_ordereddict)
|
||||
|
||||
for payload in payloads
|
||||
# ask raw data service whether it is ready
|
||||
# println("checking raw_data_db_service")
|
||||
ask = Dict(:sender => "ironpen_ai",
|
||||
:topic => "whois", # [uuid1(), "whois"] to get name of the receiver
|
||||
:topic_id => uuid1(),
|
||||
:responding_to => nothing, # receiver fills in the message uuid it is responding to
|
||||
:communication_channel => sub_channel, # a channel that sender wants receiver to send message to or "none" to get message at receiver's default respond channel
|
||||
:instruction => nothing,
|
||||
:payload => nothing,
|
||||
:isreturn => true)
|
||||
incoming_message = Utils.service_query(redis_server_ip, pub_channel, sub_channel, ask)
|
||||
# println("raw_data_db_service ok")
|
||||
if UUID(incoming_message[:responding_to]) == ask[:topic_id]
|
||||
message = Dict(:sender => "ironpen_ai",
|
||||
:topic => "process", # [uuid1(), "whois"] to get name of the receiver
|
||||
:topic_id => uuid1(),
|
||||
:responding_to => nothing, # receiver fills in the message uuid it is responding to
|
||||
:communication_channel => sub_channel, # a channel that sender wants receiver to send message to or "none" to get message at receiver's default respond channel
|
||||
:instruction => "insert",
|
||||
:payload => payload,
|
||||
:isreturn => false)
|
||||
|
||||
result = Utils.service_query(redis_server_ip, pub_channel, sub_channel, message)
|
||||
# println("published")
|
||||
else
|
||||
error("raw_data_db_service not respond")
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
end # module end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
89
src/Ironpen.jl
Normal file
89
src/Ironpen.jl
Normal file
@@ -0,0 +1,89 @@
|
||||
module Ironpen
|
||||
|
||||
export kfn_1
|
||||
|
||||
|
||||
""" Order by dependencies of each file. The 1st included file must not depend on any other
|
||||
files and each file can only depend on the file included before it.
|
||||
"""
|
||||
|
||||
include("types.jl")
|
||||
using .types # bring model into this module namespace (this module is a parent module)
|
||||
|
||||
include("snn_utils.jl")
|
||||
using .snn_utils
|
||||
|
||||
# include("Save_and_load.jl")
|
||||
# using .Save_and_load
|
||||
|
||||
# include("DB_services.jl")
|
||||
# using .DB_services
|
||||
|
||||
include("forward.jl")
|
||||
using .forward
|
||||
|
||||
include("learn.jl")
|
||||
using .learn
|
||||
|
||||
include("readout.jl")
|
||||
using .readout
|
||||
|
||||
include("interface.jl")
|
||||
using .interface
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
"""
|
||||
Todo:
|
||||
[*3] no "start learning" use reset learning and "inference", "learning" mode instead
|
||||
[4] add time-based learning method. Also implement "thinking period"
|
||||
[5] verify that model can complete learning cycle with no error
|
||||
[6] neuroplasticity() with synaptic connection strength concept
|
||||
[] using RL to control learning signal
|
||||
[] consider using Dates.now() instead of timestamp because time_stamp may overflow
|
||||
[] training should include adjusting α, neuron membrane potential decay factor
|
||||
which defined by neuron.tau_m formular in type.jl
|
||||
|
||||
[DONE] each knowledgeFn should have its own noise generater
|
||||
[DONE] where to put pseudo derivative (n.phi)
|
||||
[DONE] add excitatory, inhabitory to neuron
|
||||
|
||||
Change from version: v06_36a
|
||||
-
|
||||
|
||||
All features
|
||||
- multidispatch + for loop as main compute method
|
||||
- hard connection constrain yes
|
||||
- normalize output yes
|
||||
- allow -w_rec yes
|
||||
- voltage drop when neuron fires voltage drop equals to vth
|
||||
- v_t decay during refractory
|
||||
duration exponantial decay
|
||||
- input data population encoding, each pixel data =>
|
||||
population encoding, ralative between pixel data
|
||||
- compute neuron weight init rand()
|
||||
- output neuron weight init randn()
|
||||
"""
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
end # module end
|
||||
200
src/WPembeddings.jl
Normal file
200
src/WPembeddings.jl
Normal file
@@ -0,0 +1,200 @@
|
||||
"
|
||||
version 0.4
|
||||
Word and Positional embedding module
|
||||
"
|
||||
module WPembeddings
|
||||
|
||||
using Embeddings
|
||||
using JSON3
|
||||
using Redis
|
||||
|
||||
include("Utils.jl")
|
||||
|
||||
export get_word_embedding, get_positional_embedding, wp_embedding
|
||||
|
||||
|
||||
#----------------------------------------------------------------------------------------------
|
||||
# user setting for word embedding
|
||||
GloVe_embedding_filepath = "C:\\myWork\\my_projects\\AI\\NLP\\my_NLP\\glove.840B.300d.txt"
|
||||
max_GloVe_vocab_size = 0 # size 10000+ or "all"
|
||||
#----------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# load GloVe word embedding. URL of the embedding file: https://nlp.stanford.edu/projects/glove/
|
||||
if max_GloVe_vocab_size == 0
|
||||
# don't load vocab
|
||||
elseif max_GloVe_vocab_size != "all"
|
||||
@time const embtable = Embeddings.load_embeddings(GloVe{:en}, GloVe_embedding_filepath,
|
||||
max_vocab_size=max_GloVe_vocab_size) # size 10000 or something
|
||||
const get_word_index = Dict(word=>ii for (ii,word) in enumerate(embtable.vocab))
|
||||
else
|
||||
@time const embtable = Embeddings.load_embeddings(GloVe{:en}, GloVe_embedding_filepath)
|
||||
const get_word_index = Dict(word=>ii for (ii,word) in enumerate(embtable.vocab))
|
||||
end
|
||||
|
||||
|
||||
# if max_GloVe_vocab_size != "all"
|
||||
# @time const embtable = Embeddings.load_embeddings(GloVe{:en}, GloVe_embedding_filepath,
|
||||
# max_vocab_size=max_GloVe_vocab_size) # size 10000 or something
|
||||
# const get_word_index = Dict(word=>ii for (ii,word) in enumerate(embtable.vocab))
|
||||
# elseif max_GloVe_vocab_size == 0
|
||||
# else
|
||||
# @time const embtable = Embeddings.load_embeddings(GloVe{:en}, GloVe_embedding_filepath)
|
||||
# const get_word_index = Dict(word=>ii for (ii,word) in enumerate(embtable.vocab))
|
||||
# end
|
||||
|
||||
|
||||
"""
|
||||
get_word_embedding(word::String)
|
||||
|
||||
Get embedding vector of a word. Its dimention is depend on GloVe file used
|
||||
|
||||
# Example
|
||||
|
||||
we_matrix = get_word_embedding("blue")
|
||||
"""
|
||||
function get_word_embedding(word::String)
|
||||
index = get_word_index[word]
|
||||
embedding = embtable.embeddings[:,index]
|
||||
return embedding
|
||||
end
|
||||
|
||||
|
||||
"""
|
||||
get_positional_embedding(total_word_position::Integer, word_embedding_dimension::Integer=300)
|
||||
|
||||
return positional embedding matrix of size [word_embedding_dimension * total_word_position]
|
||||
|
||||
# Example
|
||||
|
||||
pe_matrix = get_positional_embedding(length(content), 300)
|
||||
"""
|
||||
function get_positional_embedding(total_word_position::Integer, word_embedding_dimension::Integer=300)
|
||||
d = word_embedding_dimension
|
||||
p = total_word_position
|
||||
pe = [x = i%2 == 0 ? cos(j/(10^(2i/d))) : sin(j/(10^(2i/d))) for i = 1:d, j = 1:p]
|
||||
return pe
|
||||
|
||||
end
|
||||
|
||||
|
||||
"""
|
||||
wp_embedding(tokenized_word::Array{String}, positional_embedding::Bool=false)
|
||||
|
||||
Word embedding with positional embedding.
|
||||
tokenized_word = sentense's tokenized word (not sentense in English definition but BERT definition.
|
||||
1-BERT sentense can be 20+ English's sentense)
|
||||
|
||||
# Example
|
||||
|
||||
|
||||
"""
|
||||
function wp_embedding(tokenized_word::Array{String}, positional_embedding::Bool=false)
|
||||
we_matrix = 0
|
||||
for (i, v) in enumerate(tokenized_word)
|
||||
if i == 1
|
||||
we_matrix = get_word_embedding(v)
|
||||
else
|
||||
we_matrix = hcat(we_matrix, get_word_embedding(v))
|
||||
end
|
||||
end
|
||||
|
||||
if positional_embedding
|
||||
pe_matrix = get_positional_embedding(length(tokenized_word), 300) # positional embedding
|
||||
wp_matrix = we_matrix + pe_matrix
|
||||
|
||||
return wp_matrix
|
||||
else
|
||||
return we_matrix
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
"""
|
||||
wp_query(tokenized_word::Array{String}, positional_embedding::Bool=false)
|
||||
|
||||
convert tokenized_word into JSON3 String to be sent to GloVe docker server
|
||||
"""
|
||||
function wp_query_send(tokenized_word::Array{String}, positional_embedding::Bool=false)
|
||||
d = Dict("tokenized_word"=> tokenized_word, "positional_embedding"=>positional_embedding)
|
||||
json3_str = JSON3.write(d)
|
||||
return json3_str
|
||||
end
|
||||
|
||||
|
||||
"""
|
||||
wp_query(tokenized_word::Array{String}, positional_embedding::Bool=false)
|
||||
|
||||
Using inside word_embedding_server to receive word embedding job
|
||||
convert JSON3 String into tokenized_word and positional_embedding
|
||||
"""
|
||||
function wp_query_receive(json3_str::String)
|
||||
d = JSON3.read(json3_str)
|
||||
tokenized_word = Array(d.tokenized_word)
|
||||
positional_embedding = d.positional_embedding
|
||||
|
||||
return tokenized_word, positional_embedding
|
||||
end
|
||||
|
||||
|
||||
"""
|
||||
Send tokenized_word to word_embedding_server and return word embedding
|
||||
|
||||
# Example
|
||||
|
||||
WPembeddings.query_wp_server(tokenized_word)
|
||||
"""
|
||||
function query_wp_server(query;
|
||||
host="0.0.0.0",
|
||||
port=6379,
|
||||
publish_channel="word_embedding_server/input",
|
||||
positional_encoding=true)
|
||||
|
||||
# channel used to receive JSON String from word_embedding_server
|
||||
wp_channel = Channel(10)
|
||||
function wp_receive(x)
|
||||
array = Utils.JSON3_str_to_Array(x)
|
||||
put!(wp_channel, array)
|
||||
end
|
||||
|
||||
# establish connection to word_embedding_server using default port
|
||||
conn = Redis.RedisConnection(host=host, port=port)
|
||||
sub = Redis.open_subscription(conn)
|
||||
Redis.subscribe(sub, "word_embedding_server/output", wp_receive)
|
||||
# Redis.subscribe(sub, "word_embedding_server/output", WPembeddings.wp_receive)
|
||||
|
||||
# set positional_encoding = true to enable positional encoding
|
||||
query = WPembeddings.wp_query_send(query, positional_encoding)
|
||||
# Ask word_embedding_server for word embedding
|
||||
Redis.publish(conn, publish_channel, query);
|
||||
wait(wp_channel) # wait for word_embedding_server to response
|
||||
embedded_word = take!(wp_channel)
|
||||
|
||||
disconnect(conn)
|
||||
return embedded_word
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
end
|
||||
236
src/forward.jl
Normal file
236
src/forward.jl
Normal file
@@ -0,0 +1,236 @@
|
||||
module forward
|
||||
|
||||
using Flux.Optimise: apply!
|
||||
|
||||
using Statistics, Flux, Random, LinearAlgebra
|
||||
using GeneralUtils
|
||||
using ..types, ..snn_utils
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
""" Model forward()
|
||||
"""
|
||||
function (m::model)(input_data::AbstractVector)
|
||||
# m.global_tick += 1
|
||||
m.time_stamp += 1
|
||||
|
||||
# process all corresponding KFN
|
||||
raw_model_respond = m.knowledgeFn[:I](m, input_data)
|
||||
|
||||
# the 2nd return (KFN error) should not be used as model error but I use it because there is
|
||||
# only one KFN in a model right now
|
||||
return raw_model_respond
|
||||
end
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
""" knowledgeFn forward()
|
||||
"""
|
||||
|
||||
function (kfn::kfn_1)(m::model, input_data::AbstractVector)
|
||||
kfn.time_stamp = m.time_stamp
|
||||
kfn.softreset = m.softreset
|
||||
kfn.learning_stage = m.learning_stage
|
||||
kfn.error = m.error
|
||||
|
||||
# generate noise
|
||||
noise = [GeneralUtils.randomChoiceWithProb([true, false],[0.5,0.5])
|
||||
for i in 1:length(input_data)]
|
||||
# noise = [rand(rng, Distributions.Binomial(1, 0.5)) for i in 1:10] # another option
|
||||
|
||||
input_data = [noise; input_data] # noise start from neuron id 1
|
||||
|
||||
for n in kfn.neurons_array
|
||||
timestep_forward!(n)
|
||||
end
|
||||
for n in kfn.output_neurons_array
|
||||
timestep_forward!(n)
|
||||
end
|
||||
|
||||
kfn.learning_stage = m.learning_stage
|
||||
if kfn.learning_stage == "start_learning"
|
||||
# reset params here instead of at the end_learning so that neuron's parameter data
|
||||
# don't gets wiped and can be logged for visualization later
|
||||
for n in kfn.neurons_array
|
||||
# epsilon_rec need to be reset because it counting how many each synaptic fires and
|
||||
# use this info to calculate how much synaptic weight should be adjust
|
||||
reset_learning_params!(n)
|
||||
end
|
||||
|
||||
# clear variables
|
||||
kfn.firing_neurons_list = Vector{Int64}()
|
||||
kfn.outputs = nothing
|
||||
end
|
||||
|
||||
# pass input_data into input neuron.
|
||||
# number of data point equals to number of input neuron starting from id 1
|
||||
for (i, data) in enumerate(input_data)
|
||||
kfn.neurons_array[i].z_t1 = data
|
||||
end
|
||||
|
||||
kfn.snn_firing_state_t0 = [n.z_t for n in kfn.neurons_array] #TODO check if it is used?
|
||||
|
||||
#CHANGE Threads.@threads for n in kfn.neurons_array
|
||||
for n in kfn.neurons_array
|
||||
n(kfn)
|
||||
end
|
||||
|
||||
kfn.snn_firing_state_t1 = [n.z_t1 for n in kfn.neurons_array]
|
||||
append!(kfn.firing_neurons_list, findall(kfn.snn_firing_state_t1)) # store id of neuron that fires
|
||||
if kfn.learning_stage == "end_learning" # use for random new neuron connection
|
||||
kfn.firing_neurons_list |> unique!
|
||||
end
|
||||
|
||||
# Threads.@threads for n in kfn.output_neurons_array
|
||||
for n in kfn.output_neurons_array
|
||||
n(kfn)
|
||||
end
|
||||
|
||||
out = [n.out_t1 for n in kfn.output_neurons_array]
|
||||
|
||||
return out
|
||||
end
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
""" passthrough_neuron forward()
|
||||
"""
|
||||
function (n::passthrough_neuron)(kfn::knowledgeFn)
|
||||
n.time_stamp = kfn.time_stamp
|
||||
# n.global_tick = kfn.global_tick
|
||||
end
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
""" lif_neuron forward()
|
||||
"""
|
||||
function (n::lif_neuron)(kfn::knowledgeFn)
|
||||
n.time_stamp = kfn.time_stamp
|
||||
|
||||
# pulling other neuron's firing status at time t
|
||||
n.z_i_t = getindex(kfn.snn_firing_state_t0, n.subscription_list)
|
||||
n.z_i_t .*= n.sub_ExIn_type
|
||||
|
||||
if n.refractory_counter != 0
|
||||
n.refractory_counter -= 1
|
||||
|
||||
# neuron is in refractory state, skip all calculation
|
||||
n.z_t1 = false # used by timestep_forward() in kfn. Set to zero because neuron spike
|
||||
# last only 1 timestep follow by a period of refractory.
|
||||
n.recurrent_signal = n.recurrent_signal * 0.0
|
||||
|
||||
# Exponantial decay of v_t1
|
||||
n.v_t1 = n.v_t * n.alpha^(n.time_stamp - n.last_firing_time) # or n.v_t1 = n.alpha * n.v_t
|
||||
else
|
||||
n.recurrent_signal = sum(n.w_rec .* n.z_i_t) # signal from other neuron that this neuron subscribed
|
||||
|
||||
n.alpha_v_t = n.alpha * n.v_t
|
||||
n.v_t1 = n.alpha_v_t + n.recurrent_signal
|
||||
|
||||
if n.v_t1 > n.v_th
|
||||
n.z_t1 = true
|
||||
n.refractory_counter = n.refractory_duration
|
||||
n.firing_counter += 1
|
||||
n.v_t1 = n.v_t1 - n.v_th
|
||||
else
|
||||
n.z_t1 = false
|
||||
end
|
||||
|
||||
# there is a difference from alif formula
|
||||
n.phi = (n.gamma_pd / n.v_th) * max(0, 1 - (n.v_t1 - n.v_th) / n.v_th)
|
||||
end
|
||||
end
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
""" alif_neuron forward()
|
||||
"""
|
||||
function (n::alif_neuron)(kfn::knowledgeFn)
|
||||
n.time_stamp = kfn.time_stamp
|
||||
|
||||
n.z_i_t = getindex(kfn.snn_firing_state_t0, n.subscription_list)
|
||||
n.z_i_t .*= n.sub_ExIn_type
|
||||
|
||||
if n.refractory_counter != 0
|
||||
n.refractory_counter -= 1
|
||||
|
||||
# neuron is in refractory state, skip all calculation
|
||||
n.z_t1 = false # used by timestep_forward() in kfn. Set to zero because neuron spike last only 1 timestep follow by a period of refractory.
|
||||
n.a = (n.rho * n.a) + ((1 - n.rho) * n.z_t)
|
||||
n.recurrent_signal = n.recurrent_signal * 0.0
|
||||
|
||||
# Exponantial decay of v_t1
|
||||
n.v_t1 = n.v_t * n.alpha^(n.time_stamp - n.last_firing_time) # or n.v_t1 = n.alpha * n.v_t
|
||||
n.phi = 0
|
||||
else
|
||||
n.z_t = isnothing(n.z_t) ? false : n.z_t
|
||||
n.a = (n.rho * n.a) + ((1 - n.rho) * n.z_t)
|
||||
n.av_th = n.v_th + (n.beta * n.a)
|
||||
n.recurrent_signal = sum(n.w_rec .* n.z_i_t) # signal from other neuron that this neuron subscribed
|
||||
n.alpha_v_t = n.alpha * n.v_t
|
||||
n.v_t1 = n.alpha_v_t + n.recurrent_signal
|
||||
if n.v_t1 > n.av_th
|
||||
n.z_t1 = true
|
||||
n.refractory_counter = n.refractory_duration
|
||||
n.firing_counter += 1
|
||||
n.v_t1 = n.v_t1 - n.v_th
|
||||
else
|
||||
n.z_t1 = false
|
||||
end
|
||||
|
||||
# there is a difference from lif formula
|
||||
n.phi = (n.gamma_pd / n.v_th) * max(0, 1 - (n.v_t1 - n.av_th) / n.v_th)
|
||||
end
|
||||
end
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
""" linear_neuron forward()
|
||||
In this implementation, each output neuron is fully connected to every lif and alif neuron.
|
||||
"""
|
||||
function (n::linear_neuron)(kfn::T) where T<:knowledgeFn
|
||||
n.time_stamp = kfn.time_stamp
|
||||
n.out_t1 = getindex(kfn.snn_firing_state_t1, n.subscription_list)[1]
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
end # end module
|
||||
79
src/interface.jl
Normal file
79
src/interface.jl
Normal file
@@ -0,0 +1,79 @@
|
||||
module interface
|
||||
|
||||
|
||||
# export
|
||||
|
||||
# using
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
end
|
||||
304
src/learn.jl
Normal file
304
src/learn.jl
Normal file
@@ -0,0 +1,304 @@
|
||||
module learn
|
||||
|
||||
using Flux.Optimise: apply!
|
||||
|
||||
using Statistics, Flux, Random, LinearAlgebra
|
||||
using GeneralUtils
|
||||
using ..types
|
||||
|
||||
export learn!
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
function learn!(m::model, model_respond, correct_answer)
|
||||
if m.learning_stage == "learning"
|
||||
model_error = Flux.logitcrossentropy(model_respond, correct_answer)
|
||||
output_elements_error = model_respond - correct_answer
|
||||
|
||||
learn!(m.knowledgeFn[:I], model_error, output_elements_error)
|
||||
|
||||
#WORKING compute error
|
||||
# if m.time_stamp < m.m
|
||||
model_error = model_respond .- correct_answer
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
else
|
||||
model_error = nothing
|
||||
end
|
||||
|
||||
return model_error
|
||||
end
|
||||
|
||||
|
||||
function learn!(m::model, raw_model_respond, correct_answer=nothing)
|
||||
if m.learning_stage != "doing_inference"
|
||||
model_error = Flux.logitcrossentropy(raw_model_respond, correct_answer)
|
||||
output_elements_error = raw_model_respond - correct_answer
|
||||
|
||||
learn!(m.knowledgeFn[:I], model_error, output_elements_error)
|
||||
else
|
||||
model_error = nothing
|
||||
end
|
||||
|
||||
return model_error
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
""" knowledgeFn learn()
|
||||
"""
|
||||
function learn!(kfn::knowledgeFn, error::Union{Float64,Nothing}=nothing,
|
||||
output_error::Union{Vector,Nothing}=nothing)
|
||||
kfn.error = error
|
||||
kfn.output_error = output_error
|
||||
|
||||
# Threads.@threads for n in kfn.neurons_array
|
||||
for n in kfn.neurons_array
|
||||
learn!(n, kfn) # Neurons are always learning, besides error from model output
|
||||
end
|
||||
|
||||
if kfn.output_error !== nothing
|
||||
# Threads.@threads for n in kfn.output_neurons_array
|
||||
for n in kfn.output_neurons_array # not use multithreading because 1st output neuron
|
||||
# will set learning rate that will be used by
|
||||
# other output neurons
|
||||
learn!(n, kfn)
|
||||
end
|
||||
#TODO: put other KFN to learn here
|
||||
|
||||
# for main loop user's display and training's exit condition
|
||||
avg_neurons_firing_rate = 0.0
|
||||
for n in kfn.neurons_array
|
||||
if typeof(n) <: compute_neuron
|
||||
avg_neurons_firing_rate += n.firing_rate
|
||||
end
|
||||
end
|
||||
kfn.avg_neurons_firing_rate = avg_neurons_firing_rate /
|
||||
kfn.kfn_params[:compute_neuron_number]
|
||||
avg_neurons_v_t1 = 0.0
|
||||
for n in kfn.neurons_array
|
||||
if typeof(n) <: compute_neuron
|
||||
avg_neurons_v_t1 += n.v_t1
|
||||
end
|
||||
end
|
||||
kfn.avg_neurons_v_t1 = avg_neurons_v_t1 / kfn.kfn_params[:compute_neuron_number]
|
||||
end
|
||||
end
|
||||
|
||||
""" passthrough_neuron learn()
|
||||
"""
|
||||
function learn!(n::passthrough_neuron, kfn::knowledgeFn)
|
||||
# skip
|
||||
end
|
||||
|
||||
""" lif learn()
|
||||
"""
|
||||
function learn!(n::lif_neuron, kfn::knowledgeFn)
|
||||
if n.learnable_flag == true
|
||||
|
||||
n.decayed_epsilon_rec = n.alpha * n.epsilon_rec
|
||||
n.epsilon_rec = n.decayed_epsilon_rec + n.z_i_t
|
||||
n.e_rec = n.phi * n.epsilon_rec
|
||||
end
|
||||
|
||||
# a piece of knowledgeFn error that belongs to this neuron
|
||||
n.error = isnothing(kfn.error) ? nothing : kfn.error * n.Bn
|
||||
n.learning_stage = kfn.learning_stage
|
||||
|
||||
# accumulate voltage regularization terms
|
||||
Snn_utils.cal_v_reg!(n)
|
||||
|
||||
if n.learning_stage == "doing_inference"
|
||||
# no learning
|
||||
elseif n.learning_stage == "start_learning" ||
|
||||
n.learning_stage == "start_learning_no_wchange_reset"
|
||||
|
||||
# if error signal available then accumulates Δw
|
||||
if n.error !== nothing
|
||||
Snn_utils.firing_rate!(n)
|
||||
Snn_utils.firing_diff!(n)
|
||||
n.w_rec_change = n.w_rec_change +
|
||||
-apply!(n.optimiser, n.w_rec,
|
||||
(n.error + Snn_utils.voltage_error!(n) + n.firing_rate_error) * n.e_rec) +
|
||||
-Snn_utils.firing_rate_regulator!(n) +
|
||||
-Snn_utils.voltage_regulator!(n)
|
||||
end
|
||||
elseif n.learning_stage == "during_learning"
|
||||
# if error signal available then accumulates Δw
|
||||
if n.error !== nothing
|
||||
Snn_utils.firing_rate!(n)
|
||||
Snn_utils.firing_diff!(n)
|
||||
n.w_rec_change = n.w_rec_change +
|
||||
-apply!(n.optimiser, n.w_rec,
|
||||
(n.error + Snn_utils.voltage_error!(n) + n.firing_rate_error) * n.e_rec) +
|
||||
-Snn_utils.firing_rate_regulator!(n) +
|
||||
-Snn_utils.voltage_regulator!(n)
|
||||
end
|
||||
elseif n.learning_stage == "end_learning"
|
||||
# if error signal available then accumulates Δw
|
||||
if n.error !== nothing
|
||||
Snn_utils.firing_rate!(n)
|
||||
Snn_utils.firing_diff!(n)
|
||||
n.w_rec_change = n.w_rec_change +
|
||||
-apply!(n.optimiser, n.w_rec,
|
||||
(n.error + Snn_utils.voltage_error!(n) + n.firing_rate_error) * n.e_rec) +
|
||||
-Snn_utils.firing_rate_regulator!(n) +
|
||||
-Snn_utils.voltage_regulator!(n)
|
||||
end
|
||||
|
||||
not_zero = (!iszero).(n.w_rec)
|
||||
# set 0 in w_rec_change update according to 0 in w_rec for hard constrain connection
|
||||
n.w_rec = n.w_rec + (not_zero .* n.w_rec_change)
|
||||
replace!(x -> x < 0 ? 0 : x, n.w_rec) # no negative weight
|
||||
|
||||
Snn_utils.neuroplasticity!(n, kfn.firing_neurons_list)
|
||||
end
|
||||
end
|
||||
|
||||
""" alif_neuron learn()
|
||||
"""
|
||||
function learn!(n::alif_neuron, kfn::knowledgeFn)
|
||||
n.decayed_epsilon_rec = n.alpha * n.epsilon_rec
|
||||
n.epsilon_rec = n.decayed_epsilon_rec + n.z_i_t
|
||||
n.epsilon_rec_a = (n.phi * n.epsilon_rec) +
|
||||
((n.rho - (n.phi * n.beta)) * n.epsilon_rec_a)
|
||||
n.e_rec_v = n.phi * n.epsilon_rec
|
||||
n.e_rec_a = -n.phi * n.beta * n.epsilon_rec_a
|
||||
n.e_rec = n.e_rec_v + n.e_rec_a
|
||||
|
||||
# a piece of knowledgeFn error that belongs to this neuron
|
||||
n.error = isnothing(kfn.error) ? nothing : kfn.error * n.Bn
|
||||
n.learning_stage = kfn.learning_stage
|
||||
|
||||
|
||||
|
||||
if n.learning_stage == "doing_inference"
|
||||
# no learning
|
||||
elseif n.learning_stage == "start_learning" ||
|
||||
n.learning_stage == "start_learning_no_wchange_reset"
|
||||
|
||||
# if error signal available then accumulates Δw
|
||||
if n.error !== nothing
|
||||
Snn_utils.firing_rate!(n)
|
||||
Snn_utils.firing_diff!(n)
|
||||
n.w_rec_change = n.w_rec_change +
|
||||
-apply!(n.optimiser, n.w_rec,
|
||||
(n.error + Snn_utils.voltage_error!(n) + n.firing_rate_error) * n.e_rec) +
|
||||
-Snn_utils.firing_rate_regulator!(n) +
|
||||
-Snn_utils.voltage_regulator!(n)
|
||||
end
|
||||
elseif n.learning_stage == "during_learning"
|
||||
# if error signal available then accumulates Δw
|
||||
if n.error !== nothing
|
||||
Snn_utils.firing_rate!(n)
|
||||
Snn_utils.firing_diff!(n)
|
||||
n.w_rec_change = n.w_rec_change +
|
||||
-apply!(n.optimiser, n.w_rec,
|
||||
(n.error + Snn_utils.voltage_error!(n) + n.firing_rate_error) * n.e_rec) +
|
||||
-Snn_utils.firing_rate_regulator!(n) +
|
||||
-Snn_utils.voltage_regulator!(n)
|
||||
end
|
||||
elseif n.learning_stage == "end_learning"
|
||||
|
||||
# if error signal available then accumulates Δw
|
||||
if n.error !== nothing
|
||||
Snn_utils.firing_rate!(n)
|
||||
Snn_utils.firing_diff!(n)
|
||||
n.w_rec_change = n.w_rec_change +
|
||||
-apply!(n.optimiser, n.w_rec,
|
||||
(n.error + Snn_utils.voltage_error!(n) + n.firing_rate_error) * n.e_rec) +
|
||||
-Snn_utils.firing_rate_regulator!(n) +
|
||||
-Snn_utils.voltage_regulator!(n)
|
||||
end
|
||||
|
||||
not_zero = (!iszero).(n.w_rec)
|
||||
# set 0 in w_rec_change update according to 0 in w_rec for hard constrain connection
|
||||
n.w_rec = n.w_rec + (not_zero .* n.w_rec_change)
|
||||
replace!(x -> x < 0 ? 0 : x, n.w_rec) # no negative weight
|
||||
|
||||
Snn_utils.neuroplasticity!(n, kfn.firing_neurons_list)
|
||||
end
|
||||
end
|
||||
|
||||
""" linear_neuron learn()
|
||||
"""
|
||||
function learn!(n::linear_neuron, kfn::knowledgeFn)
|
||||
n.error = kfn.output_error[n.id]
|
||||
n.learning_stage = kfn.learning_stage
|
||||
|
||||
if n.learning_stage == "doing_inference"
|
||||
# no learning
|
||||
elseif n.learning_stage == "start_learning"
|
||||
# if error signal available then accumulates Δw
|
||||
if n.error !== nothing && n.id == 1 # NOT working w/ multithreading training
|
||||
Δw = -apply!(n.optimiser, n.w_out, (n.error * n.epsilon_j))
|
||||
n.w_out_change = n.w_out_change + Δw
|
||||
n.eta = n.optimiser.eta
|
||||
Δb = -n.eta * n.error
|
||||
n.b_change = n.b_change + Δb
|
||||
elseif n.error !== nothing && n.id !== 1
|
||||
n.eta = kfn.output_neurons_array[1].eta
|
||||
Δw = -n.eta * n.error * n.epsilon_j
|
||||
n.w_out_change = n.w_out_change + Δw
|
||||
Δb = -n.eta * n.error
|
||||
n.b_change = n.b_change + Δb
|
||||
end
|
||||
elseif n.learning_stage == "during_learning"
|
||||
# if error signal available then accumulates Δw
|
||||
if n.error !== nothing && n.id == 1 # NOT working w/ multithreading training
|
||||
Δw = -apply!(n.optimiser, n.w_out, (n.error * n.epsilon_j))
|
||||
n.w_out_change = n.w_out_change + Δw
|
||||
n.eta = n.optimiser.eta
|
||||
Δb = -n.eta * n.error
|
||||
n.b_change = n.b_change + Δb
|
||||
elseif n.error !== nothing && n.id !== 1
|
||||
n.eta = kfn.output_neurons_array[1].eta
|
||||
Δw = -n.eta * n.error * n.epsilon_j
|
||||
n.w_out_change = n.w_out_change + Δw
|
||||
Δb = -n.eta * n.error
|
||||
n.b_change = n.b_change + Δb
|
||||
end
|
||||
elseif n.learning_stage == "end_learning"
|
||||
# if error signal available then accumulates Δw
|
||||
if n.error !== nothing && n.id == 1 # NOT working w/ multithreading training
|
||||
Δw = -apply!(n.optimiser, n.w_out, (n.error * n.epsilon_j))
|
||||
n.w_out_change = n.w_out_change + Δw
|
||||
n.eta = n.optimiser.eta
|
||||
Δb = -n.eta * n.error
|
||||
n.b_change = n.b_change + Δb
|
||||
elseif n.error !== nothing && n.id !== 1
|
||||
n.eta = kfn.output_neurons_array[1].eta
|
||||
Δw = -n.eta * n.error * n.epsilon_j
|
||||
n.w_out_change = n.w_out_change + Δw
|
||||
Δb = -n.eta * n.error
|
||||
n.b_change = n.b_change + Δb
|
||||
end
|
||||
|
||||
n.w_out = n.w_out + n.w_out_change
|
||||
n.b = n.b + n.b_change
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
end # module end
|
||||
83
src/readout.jl
Normal file
83
src/readout.jl
Normal file
@@ -0,0 +1,83 @@
|
||||
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
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
319
src/snn_utils.jl
Normal file
319
src/snn_utils.jl
Normal file
@@ -0,0 +1,319 @@
|
||||
module snn_utils
|
||||
|
||||
using Flux.Optimise: apply!
|
||||
export calculate_α, calculate_ρ, calculate_k, timestep_forward!, init_neuron, no_negative,
|
||||
precision, calculate_w_change!, store_knowledgefn_error!, interneurons_adjustment!,
|
||||
reset_z_t!, reset_learning_params!, reset_learning_history_params!,
|
||||
cal_v_reg!, calculate_w_change_end!,
|
||||
firing_rate_error!, firing_rate_regulator!, update_Bn!, cal_firing_reg!,
|
||||
neuroplasticity!, shakeup!, reset_learning_no_wchange!, adjust_internal_learning_rate!,
|
||||
gradient_withloss
|
||||
|
||||
using Statistics, Random, LinearAlgebra, Distributions, Zygote
|
||||
|
||||
using ..types
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
function timestep_forward!(x::passthrough_neuron)
|
||||
x.z_t = x.z_t1
|
||||
end
|
||||
|
||||
function timestep_forward!(x::compute_neuron)
|
||||
x.z_t = x.z_t1
|
||||
x.v_t = x.v_t1
|
||||
end
|
||||
|
||||
function timestep_forward!(x::linear_neuron)
|
||||
x.out_t = x.out_t1
|
||||
end
|
||||
|
||||
no_negative(n) = n < 0.0 ? 0.0 : x
|
||||
precision(x::Array{<:Array}) = ( std(mean.(x)) / mean(mean.(x)) ) * 100
|
||||
|
||||
# reset functions for LIF/ALIF neuron
|
||||
reset_last_firing_time!(n::compute_neuron) = n.last_firing_time = 0.0
|
||||
reset_refractory_state_active!(n::compute_neuron) = n.refractory_state_active = false
|
||||
reset_v_t!(n::compute_neuron) = n.v_t = n.v_t_default
|
||||
reset_z_t!(n::compute_neuron) = n.z_t = false
|
||||
reset_epsilon_rec!(n::compute_neuron) = n.epsilon_rec = n.epsilon_rec * 0.0
|
||||
reset_epsilon_rec_a!(n::alif_neuron) = n.epsilon_rec_a = n.epsilon_rec_a * 0.0
|
||||
reset_epsilon_in!(n::compute_neuron) = n.epsilon_in = isnothing(n.epsilon_in) ? nothing : n.epsilon_in * 0.0
|
||||
reset_error!(n::Union{compute_neuron, linear_neuron}) = n.error = nothing
|
||||
reset_w_in_change!(n::compute_neuron) = n.w_in_change = isnothing(n.w_in_change) ? nothing : n.w_in_change * 0.0
|
||||
reset_w_rec_change!(n::compute_neuron) = n.w_rec_change = n.w_rec_change * 0.0
|
||||
reset_a!(n::alif_neuron) = n.a = n.a * 0.0
|
||||
reset_reg_voltage_a!(n::compute_neuron) = n.reg_voltage_a = n.reg_voltage_a * 0.0
|
||||
reset_reg_voltage_b!(n::compute_neuron) = n.reg_voltage_b = n.reg_voltage_b * 0.0
|
||||
reset_reg_voltage_error!(n::compute_neuron) = n.reg_voltage_error = n.reg_voltage_error * 0.0
|
||||
reset_firing_counter!(n::compute_neuron) = n.firing_counter = n.firing_counter * 0.0
|
||||
reset_firing_diff!(n::Union{compute_neuron, linear_neuron}) = n.firing_diff = n.firing_diff * 0.0
|
||||
reset_previous_error!(n::Union{compute_neuron}) =
|
||||
n.previous_error = n.previous_error * 0.0
|
||||
|
||||
# reset function for output neuron
|
||||
reset_epsilon_j!(n::linear_neuron) = n.epsilon_j = n.epsilon_j * 0.0
|
||||
reset_out_t!(n::linear_neuron) = n.out_t = n.out_t * 0.0
|
||||
reset_w_out_change!(n::linear_neuron) = n.w_out_change = n.w_out_change * 0.0
|
||||
reset_b_change!(n::linear_neuron) = n.b_change = n.b_change * 0.0
|
||||
|
||||
|
||||
""" Reset a part of learning-related params that used to collect learning history during learning
|
||||
session
|
||||
"""
|
||||
# function reset_learning_no_wchange!(n::lif_neuron)
|
||||
# reset_epsilon_rec!(n)
|
||||
# # reset_v_t!(n)
|
||||
# # reset_z_t!(n)
|
||||
# # reset_reg_voltage_a!(n)
|
||||
# # reset_reg_voltage_b!(n)
|
||||
# # reset_reg_voltage_error!(n)
|
||||
# reset_firing_counter!(n)
|
||||
# reset_firing_diff!(n)
|
||||
# reset_previous_error!(n)
|
||||
# reset_error!(n)
|
||||
|
||||
# # # reset refractory state at the end of episode. Otherwise once neuron goes into refractory state,
|
||||
# # # it will stay in refractory state forever
|
||||
# # reset_refractory_state_active!(n)
|
||||
# end
|
||||
# function reset_learning_no_wchange!(n::Union{alif_neuron, elif_neuron})
|
||||
# reset_epsilon_rec!(n)
|
||||
# reset_epsilon_rec_a!(n)
|
||||
# reset_v_t!(n)
|
||||
# reset_z_t!(n)
|
||||
# # reset_a!(n)
|
||||
# reset_reg_voltage_a!(n)
|
||||
# reset_reg_voltage_b!(n)
|
||||
# reset_reg_voltage_error!(n)
|
||||
# reset_firing_counter!(n)
|
||||
# reset_firing_diff!(n)
|
||||
# reset_previous_error!(n)
|
||||
# reset_error!(n)
|
||||
|
||||
# # reset refractory state at the end of episode. Otherwise once neuron goes into refractory state,
|
||||
# # it will stay in refractory state forever
|
||||
# reset_refractory_state_active!(n)
|
||||
# end
|
||||
# function reset_learning_no_wchange!(n::linear_neuron)
|
||||
# reset_epsilon_j!(n)
|
||||
# reset_out_t!(n)
|
||||
# reset_error!(n)
|
||||
# end
|
||||
|
||||
""" Reset all learning-related params at the END of learning session
|
||||
"""
|
||||
function reset_learning_params!(n::lif_neuron)
|
||||
reset_epsilon_rec!(n)
|
||||
reset_w_rec_change!(n)
|
||||
# reset_v_t!(n)
|
||||
# reset_z_t!(n)
|
||||
# reset_reg_voltage_a!(n)
|
||||
# reset_reg_voltage_b!(n)
|
||||
# reset_reg_voltage_error!(n)
|
||||
reset_firing_counter!(n)
|
||||
reset_firing_diff!(n)
|
||||
reset_previous_error!(n)
|
||||
reset_error!(n)
|
||||
|
||||
# # reset refractory state at the end of episode. Otherwise once neuron goes into refractory state,
|
||||
# # it will stay in refractory state forever
|
||||
# reset_refractory_state_active!(n)
|
||||
end
|
||||
function reset_learning_params!(n::alif_neuron)
|
||||
reset_epsilon_rec!(n)
|
||||
reset_epsilon_rec_a!(n)
|
||||
reset_w_rec_change!(n)
|
||||
# reset_v_t!(n)
|
||||
# reset_z_t!(n)
|
||||
# reset_a!(n)
|
||||
# reset_reg_voltage_a!(n)
|
||||
# reset_reg_voltage_b!(n)
|
||||
# reset_reg_voltage_error!(n)
|
||||
reset_firing_counter!(n)
|
||||
reset_firing_diff!(n)
|
||||
reset_previous_error!(n)
|
||||
reset_error!(n)
|
||||
|
||||
# # reset refractory state at the end of episode. Otherwise once neuron goes into refractory state,
|
||||
# # it will stay in refractory state forever
|
||||
# reset_refractory_state_active!(n)
|
||||
end
|
||||
|
||||
# function reset_learning_no_wchange!(n::passthrough_neuron)
|
||||
# end
|
||||
|
||||
function reset_learning_params!(n::passthrough_neuron)
|
||||
# skip
|
||||
end
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
function store_knowledgefn_error!(kfn::knowledgeFn)
|
||||
# condition to adjust nueron in KFN plane in addition to weight adjustment inside each neuron
|
||||
if kfn.learning_stage == "start_learning"
|
||||
if kfn.recent_knowledgeFn_error === nothing && kfn.knowledgeFn_error === nothing
|
||||
kfn.recent_knowledgeFn_error = [[]]
|
||||
elseif kfn.recent_knowledgeFn_error === nothing
|
||||
kfn.recent_knowledgeFn_error = [[kfn.knowledgeFn_error]]
|
||||
elseif kfn.recent_knowledgeFn_error !== nothing && kfn.knowledgeFn_error === nothing
|
||||
push!(kfn.recent_knowledgeFn_error, [])
|
||||
else
|
||||
push!(kfn.recent_knowledgeFn_error, [kfn.knowledgeFn_error])
|
||||
end
|
||||
elseif kfn.learning_stage == "during_learning"
|
||||
if kfn.knowledgeFn_error === nothing
|
||||
#skip
|
||||
else
|
||||
push!(kfn.recent_knowledgeFn_error[end], kfn.knowledgeFn_error)
|
||||
end
|
||||
elseif kfn.learning_stage == "end_learning"
|
||||
if kfn.recent_knowledgeFn_error === nothing
|
||||
#skip
|
||||
else
|
||||
push!(kfn.recent_knowledgeFn_error[end], kfn.knowledgeFn_error)
|
||||
end
|
||||
else
|
||||
error("case does not defined yet")
|
||||
end
|
||||
|
||||
if length(kfn.recent_knowledgeFn_error) > 3
|
||||
deleteat!(kfn.recent_knowledgeFn_error, 1)
|
||||
end
|
||||
end
|
||||
|
||||
function update_Bn!(kfn::knowledgeFn)
|
||||
Δw = nothing
|
||||
for n in kfn.output_neurons_array
|
||||
Δw = Δw === nothing ? n.w_out_change : Δw + n.w_out_change
|
||||
n.w_out = n.w_out - (n.Bn_wout_decay * n.w_out) # w_out decay
|
||||
end
|
||||
# Δw = Δw / kfn.kfn_params[:linear_neuron_number] # average
|
||||
|
||||
input_neuron_number = kfn.kfn_params[:input_neuron_number] # skip input neuron
|
||||
for i = 1:kfn.kfn_params[:compute_neuron_number]
|
||||
n = kfn.neurons_array[input_neuron_number+i]
|
||||
n.Bn = n.Bn + Δw[i]
|
||||
n.Bn = n.Bn - (n.Bn_wout_decay * n.Bn) # w_out decay
|
||||
end
|
||||
end
|
||||
|
||||
""" Regulates membrane potential to stay under v_th, output is weight change
|
||||
"""
|
||||
function cal_v_reg!(n::lif_neuron)
|
||||
# retified linear function
|
||||
component_a1 = n.v_t1 - n.v_th < 0 ? 0 : (n.v_t1 - n.v_th)^2
|
||||
component_a2 = -n.v_t1 - n.v_th < 0 ? 0 : (-n.v_t1 - n.v_th)^2
|
||||
n.reg_voltage_a = n.reg_voltage_a + component_a1 + component_a2
|
||||
|
||||
component_b = n.v_t1 - n.v_th < 0 ? 0 : n.v_t1 - n.v_th
|
||||
#FIXME: not sure the following line is correct
|
||||
n.reg_voltage_b = n.reg_voltage_b + (component_b * n.epsilon_rec)
|
||||
end
|
||||
|
||||
function cal_v_reg!(n::alif_neuron)
|
||||
# retified linear function
|
||||
component_a1 = n.v_t1 - n.av_th < 0 ? 0 : (n.v_t1 - n.av_th)^2
|
||||
component_a2 = -n.v_t1 - n.av_th < 0 ? 0 : (-n.v_t1 - n.av_th)^2
|
||||
n.reg_voltage_a = n.reg_voltage_a + component_a1 + component_a2
|
||||
|
||||
component_b = n.v_t1 - n.av_th < 0 ? 0 : n.v_t1 - n.av_th
|
||||
#FIXME: not sure the following line is correct
|
||||
n.reg_voltage_b = n.reg_voltage_b + (component_b * (n.epsilon_rec - n.epsilon_rec_a))
|
||||
end
|
||||
|
||||
function voltage_error!(n::compute_neuron)
|
||||
n.reg_voltage_error = 0.5 * n.reg_voltage_a
|
||||
return n.reg_voltage_error
|
||||
end
|
||||
|
||||
function voltage_regulator!(n::compute_neuron) # running average
|
||||
Δw = n.optimiser.eta * n.c_reg_v * n.reg_voltage_b
|
||||
return Δw
|
||||
end
|
||||
|
||||
function firing_rate_error(kfn::knowledgeFn)
|
||||
start_id = kfn.kfn_params[:input_neuron_number] + 1
|
||||
return 0.5 * sum([(n.firing_diff)^2 for n in kfn.neurons_array[start_id:end]])
|
||||
end
|
||||
|
||||
function firing_rate_regulator!(n::compute_neuron)
|
||||
# n.firing_rate NOT running average (average over learning batch)
|
||||
Δw = n.optimiser.eta * n.c_reg *
|
||||
(n.firing_rate - n.firing_rate_target) * n.e_rec
|
||||
Δw = n.firing_rate > n.firing_rate_target ? Δw : Δw * 0.0
|
||||
return Δw
|
||||
end
|
||||
|
||||
firing_rate!(n::compute_neuron) = n.firing_rate = (n.firing_counter / n.time_stamp) * 1000
|
||||
firing_diff!(n::compute_neuron) = n.firing_diff = n.firing_rate - n.firing_rate_target
|
||||
|
||||
function neuroplasticity!(n::compute_neuron, firing_neurons_list::Vector)
|
||||
# if there is 0-weight then replace it with new connection
|
||||
zero_weight_index = findall(iszero.(n.w_rec))
|
||||
if length(zero_weight_index) != 0
|
||||
""" sampling new connection from list of neurons that fires instead of ramdom choose from
|
||||
all compute neuron because there is no point to connect to neuron that not fires i.e.
|
||||
not fire = no information
|
||||
"""
|
||||
|
||||
subscribe_options = filter(x -> x ∉ [n.id], firing_neurons_list) # exclude this neuron id from the list
|
||||
filter!(x -> x ∉ n.subscription_list, subscribe_options) # exclude this neuron's subscription_list from the list
|
||||
shuffle!(subscribe_options)
|
||||
end
|
||||
|
||||
new_connection_percent = 10 - ((n.optimiser.eta / 0.0001) / 10) # percent is in range 0.1 to 10
|
||||
percentage = [new_connection_percent, 100.0 - new_connection_percent] / 100.0
|
||||
for i in zero_weight_index
|
||||
if Utils.random_choices([true, false], percentage)
|
||||
n.subscription_list[i] = pop!(subscribe_options)
|
||||
n.w_rec[i] = 0.01 # new connection should not send large signal otherwise it would throw
|
||||
# RSNN off path. Let weight grow by an optimiser
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function adjust_internal_learning_rate!(n::compute_neuron)
|
||||
n.internal_learning_rate = n.error_diff[end] < 0.0 ? n.internal_learning_rate * 0.99 :
|
||||
n.internal_learning_rate * 1.005
|
||||
end
|
||||
|
||||
function push_epsilon_rec_a!(n::lif_neuron)
|
||||
# skip
|
||||
end
|
||||
|
||||
function push_epsilon_rec_a!(n::alif_neuron)
|
||||
push!(n.epsilon_rec_a, 0)
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
end # end module
|
||||
763
src/types.jl
Normal file
763
src/types.jl
Normal file
@@ -0,0 +1,763 @@
|
||||
module types
|
||||
|
||||
export
|
||||
# struct
|
||||
IronpenStruct, model, knowledgeFn, lif_neuron, alif_neuron, linear_neuron,
|
||||
kfn_1, compute_neuron, neuron, output_neuron, passthrough_neuron,
|
||||
|
||||
# function
|
||||
instantiate_custom_types, init_neuron, populate_neuron,
|
||||
add_neuron!
|
||||
|
||||
using Random, Flux, LinearAlgebra
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
abstract type Ironpen end
|
||||
abstract type knowledgeFn <: Ironpen end
|
||||
abstract type neuron <: Ironpen end
|
||||
abstract type input_neuron <: neuron end
|
||||
abstract type output_neuron <: neuron end
|
||||
abstract type compute_neuron <: neuron end
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
""" Model struct
|
||||
"""
|
||||
Base.@kwdef mutable struct model <: Ironpen
|
||||
knowledgeFn::Union{Dict,Nothing} = nothing
|
||||
model_params::Union{Dict,Nothing} = nothing
|
||||
error::Union{Float64,Nothing} = 0.0
|
||||
output_error::Union{Array,Nothing} = Vector{AbstractFloat}()
|
||||
|
||||
""" "inference" = no learning params will be collected.
|
||||
"learning" = neuron will accumulate epsilon_j, compute Δw_rec_change each time
|
||||
correct answer is available then merge Δw_rec_change into w_rec_change then
|
||||
reset epsilon_j.
|
||||
"reflect" = neuron will merge w_rec_change into w_rec then reset w_rec_change. """
|
||||
learning_stage::String = "inference"
|
||||
|
||||
softreset::Bool = false
|
||||
time_stamp::Number = 0.0
|
||||
end
|
||||
""" Model outer constructor
|
||||
|
||||
# Example
|
||||
I_kfnparams = Dict(
|
||||
:type => "lif_neuron",
|
||||
:v_t1 => 0.0, # neuron membrane potential at time = t+1
|
||||
:v_th => 2.0, # neuron firing threshold (this value is treated as maximum bound if I use auto generate)
|
||||
:z_t => false, # neuron firing status at time = t
|
||||
:z_t1 => false, # neuron firing status at time = t+1
|
||||
:gamma_pd => 0.3, # discount factor. The value is from the paper
|
||||
:phi => 0.0, # psuedo derivative
|
||||
:refractory_duration => 2.0, # neuron refractory period in tick
|
||||
:delta => 1.0,
|
||||
:tau_m => 20.0, # membrane time constant in millisecond. The value is from the paper
|
||||
:eta => 0.01, # learning rate
|
||||
|
||||
I_kfn = Ironpen_ai_gpu.knowledgeFn(I_kfnparams, lif_neuron_params, alif_neuron_params,
|
||||
linear_neuron_params)
|
||||
|
||||
model_params_1 = Dict(:knowledgeFn => Dict(:I => I_kfn,
|
||||
:run => run_kfn),
|
||||
:learning_stage => "doing_inference",)
|
||||
|
||||
model_1 = Ironpen_ai_gpu.model(model_params_1)
|
||||
"""
|
||||
function model(params::Dict)
|
||||
m = model()
|
||||
m.model_params = params
|
||||
|
||||
fields = fieldnames(typeof(m))
|
||||
for i in fields
|
||||
if i in keys(params)
|
||||
m.:($i) = params[i] # assign params to n struct fields
|
||||
end
|
||||
end
|
||||
|
||||
return m
|
||||
end
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
""" knowledgeFn struct
|
||||
"""
|
||||
Base.@kwdef mutable struct kfn_1 <: knowledgeFn
|
||||
knowledgefn_name::Union{String,Nothing} = nothing
|
||||
kfn_params::Union{Dict,Nothing} = nothing # store params of knowledgeFn itself for later use
|
||||
time_stamp::Number = 0.0
|
||||
|
||||
# Bn contain error coefficient for both neurons and output neurons in one place
|
||||
Bn::Vector{Float64} = Vector{Float64}() # error projection coefficient from kfn output's error to each neurons's error
|
||||
neurons_array::Union{Array,Nothing} = [] # put neurons here
|
||||
|
||||
""" put output neuron here. I seperate output neuron because
|
||||
1. its calculation is difference than other neuron types
|
||||
2. other neuron type will not induced to connnect to output neuron
|
||||
3. output neuron does not induced to connect to its own type """
|
||||
output_neurons_array::Union{Array,Nothing} = []
|
||||
|
||||
""" "inference" = no learning params will be collected.
|
||||
"learning" = neuron will accumulate epsilon_j, compute Δw_rec_change each time
|
||||
correct answer is available then merge Δw_rec_change into w_rec_change then
|
||||
reset epsilon_j.
|
||||
"reflect" = neuron will merge w_rec_change into w_rec then reset w_rec_change. """
|
||||
learning_stage::String = "inference"
|
||||
|
||||
error::Union{Float64,Nothing} = nothing
|
||||
output_error::Union{Array,Nothing} = Vector{AbstractFloat}()
|
||||
recent_knowledgeFn_error::Union{Any,Nothing} = nothing
|
||||
softreset::Bool = false
|
||||
meta_params::Union{Dict{Any,Any},Nothing} = Dict()
|
||||
|
||||
firing_neurons_list::Array{Int64} = Vector{Int64}() # store id of firing neurons
|
||||
snn_firing_state_t0::Union{Vector{Bool},Nothing} = nothing # store firing state of all neurons at t0
|
||||
snn_firing_state_t1::Union{Vector{Bool},Nothing} = nothing # store firing state of all neurons at t1
|
||||
|
||||
avg_neurons_firing_rate::Union{Float64,Nothing} = 0.0 # for displaying average firing rate over all neurons
|
||||
avg_neurons_v_t1::Union{Float64,Nothing} = 0.0 # for displaying average v_t1 over all neurons
|
||||
end
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
""" Knowledge function outer constructor >>> auto generate <<<
|
||||
|
||||
# Example
|
||||
|
||||
lif_neuron_params = Dict(
|
||||
:type => "lif_neuron",
|
||||
:v_th => 1.2, # neuron firing threshold (this value is treated as maximum bound if I use auto generate)
|
||||
:z_t => false, # neuron firing status at time = t
|
||||
:gamma_pd => 0.3, # discount factor. The value is from the paper
|
||||
:refractory_duration => 2.0, # neuron refractory period in tick
|
||||
:delta => 1.0,
|
||||
:tau_m => 5.0, # membrane time constant in millisecond. It should equals to time use for 1 sequence
|
||||
)
|
||||
|
||||
alif_neuron_params = Dict(
|
||||
:type => "alif_neuron",
|
||||
:v_th => 1.2, # neuron firing threshold (this value is treated as maximum bound if I use auto generate)
|
||||
:z_t => false, # neuron firing status at time = t
|
||||
:gamma_pd => 0.3, # discount factor. The value is from the paper
|
||||
:refractory_duration => 2.0, # neuron refractory period in millisecond
|
||||
:delta => 1.0,
|
||||
:tau_m => 5.0, # membrane time constant in millisecond. It should equals to time use for 1 sequence
|
||||
|
||||
# adaptation time constant in millisecond. It should equals to total time SNN takes to
|
||||
# perform a task i.e. equals to episode length
|
||||
:tau_a => 10.0,
|
||||
:beta => 0.15, # constant.
|
||||
:a => 0.0,
|
||||
)
|
||||
|
||||
linear_neuron_params = Dict(
|
||||
:type => "linear_neuron",
|
||||
:k => 0.9, # output leakink coefficient
|
||||
:tau_out => 5.0, # output time constant in millisecond. It should equals to time use for 1 sequence
|
||||
:out => 0.0, # neuron's output value store here
|
||||
)
|
||||
|
||||
I_kfnparams = Dict(
|
||||
:knowledgefn_name => "I",
|
||||
:lif_neuron_number => 200,
|
||||
:alif_neuron_number => 100, # from Allen Institute, ALIF is 40% of LIF
|
||||
:linear_neuron_number => 5, # output neuron, this is also the output length
|
||||
:Bn => "random", # error projection coefficient from kfn output's error to each neurons's error
|
||||
:learning_rate => 0.01,
|
||||
:neuron_connection_pattern => "100%", # number of each neuron subscribe to other neuron in knowledgeFn.neurons_array
|
||||
:output_neuron_connection_pattern => "100%", # "60%" of kfn.neurons_array or number
|
||||
:maximum_input_data_length => 5, # in case of GloVe word encoding, it is 300
|
||||
:neuron_w_in_generation_pattern => "random", # number or "random"
|
||||
:neuron_w_rec_generation_pattern => "random",
|
||||
:neuron_v_t_default => 0.5,
|
||||
:neuron_voltage_drop_percentage => "100%",
|
||||
:neuron_firing_rate_target => 50.0,
|
||||
:neuron_learning_rate => 0.01,
|
||||
:neuron_c_reg => 0.0001,
|
||||
:neuron_c_reg_v => 0.0001,
|
||||
:neuron_optimiser => "ADAM",
|
||||
:meta_params => Dict(:is_first_cycle => true,
|
||||
:launch_time => 0.0,))
|
||||
|
||||
kfn1 = knowledgeFn(kfn_params, lif_neuron_params, alif_neuron_params, linear_neuron_params)
|
||||
"""
|
||||
function kfn_1(kfn_params::Dict)
|
||||
|
||||
kfn = kfn_1()
|
||||
kfn.kfn_params = kfn_params
|
||||
kfn.knowledgefn_name = kfn.kfn_params[:knowledgefn_name]
|
||||
|
||||
if kfn.kfn_params[:compute_neuron_number] < kfn.kfn_params[:total_input_port]
|
||||
throw(error("number of compute neuron must be greater than input neuron"))
|
||||
end
|
||||
|
||||
# Bn
|
||||
if kfn.kfn_params[:Bn] == "random"
|
||||
kfn.Bn = [Random.rand(0:0.001:1) for i in 1:kfn.kfn_params[:compute_neuron_number]]
|
||||
else # in case I want to specify manually
|
||||
kfn.Bn = [kfn.kfn_params[:Bn] for i in 1:kfn.kfn_params[:compute_neuron_number]]
|
||||
end
|
||||
|
||||
# assign neurons ID by their position in kfn.neurons array because I think it is
|
||||
# straight forward way
|
||||
|
||||
# add input port
|
||||
for (k, v) in kfn.kfn_params[:input_port]
|
||||
current_type = kfn.kfn_params[:input_port][k]
|
||||
for i = 1:current_type[:numbers]
|
||||
n_id = length(kfn.neurons_array) + 1
|
||||
neuron = init_neuron(n_id, current_type[:params], kfn.kfn_params)
|
||||
push!(kfn.neurons_array, neuron)
|
||||
end
|
||||
end
|
||||
|
||||
# add compute neurons
|
||||
for (k, v) in kfn.kfn_params[:compute_neuron]
|
||||
current_type = kfn.kfn_params[:compute_neuron][k]
|
||||
for i = 1:current_type[:numbers]
|
||||
n_id = length(kfn.neurons_array) + 1
|
||||
neuron = init_neuron(n_id, current_type[:params], kfn.kfn_params)
|
||||
push!(kfn.neurons_array, neuron)
|
||||
end
|
||||
end
|
||||
|
||||
for i = 1:kfn.kfn_params[:output_port][:numbers]
|
||||
neuron = init_neuron(i, kfn.kfn_params[:output_port][:params],
|
||||
kfn.kfn_params)
|
||||
push!(kfn.output_neurons_array, neuron)
|
||||
end
|
||||
|
||||
# random which neuron output port subscribed to, 1-compute_neuron for each output port
|
||||
sub_list = shuffle!([kfn.kfn_params[:total_input_port]+1:length(kfn.neurons_array)...])
|
||||
sub_output_neuron = [pop!(sub_list) for i in 1:kfn.kfn_params[:output_port][:numbers]]
|
||||
for i in kfn.output_neurons_array
|
||||
i.subscription_list = [pop!(sub_output_neuron)]
|
||||
end
|
||||
|
||||
for n in kfn.neurons_array
|
||||
if typeof(n) <: compute_neuron
|
||||
n.firing_rate_target = kfn.kfn_params[:neuron_firing_rate_target]
|
||||
end
|
||||
end
|
||||
|
||||
# excitatory neuron to inhabitory neuron = 60:40 % of compute_neuron
|
||||
ex_number = Int(floor(0.6 * kfn.kfn_params[:compute_neuron_number]))
|
||||
ex_n = [1 for i in 1:ex_number]
|
||||
in_number = kfn.kfn_params[:compute_neuron_number] - ex_number
|
||||
in_n = [-1 for i in 1:in_number]
|
||||
ex_in = shuffle!([ex_n; in_n])
|
||||
|
||||
# input neurons are always excitatory, compute_neurons are random between excitatory
|
||||
# and inhabitory
|
||||
for n in reverse(kfn.neurons_array)
|
||||
try n.ExIn_type = pop!(ex_in) catch end
|
||||
end
|
||||
|
||||
# add ExIn_type into each compute_neuron sub_ExIn_type
|
||||
for n in reverse(kfn.neurons_array)
|
||||
try # input neuron doest have n.subscription_list
|
||||
for sub_id in n.subscription_list
|
||||
n_ExIn_type = kfn.neurons_array[sub_id].ExIn_type
|
||||
push!(n.sub_ExIn_type, n_ExIn_type)
|
||||
end
|
||||
catch
|
||||
end
|
||||
end
|
||||
|
||||
return kfn
|
||||
end
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
""" passthrough_neuron struct
|
||||
"""
|
||||
Base.@kwdef mutable struct passthrough_neuron <: input_neuron
|
||||
id::Union{Int64,Nothing} = nothing # ID of this neuron which is it position in knowledgeFn array
|
||||
type::String = "passthrough_neuron"
|
||||
knowledgefn_name::Union{String,Nothing} = nothing # knowledgeFn that this neuron belongs to
|
||||
z_t::Bool = false
|
||||
z_t1::Bool = false
|
||||
time_stamp::Number = 0.0 # current time
|
||||
ExIn_type::Integer = 1 # 1 excitatory, -1 inhabitory. input neuron is always excitatory
|
||||
end
|
||||
|
||||
function passthrough_neuron(params::Dict)
|
||||
n = passthrough_neuron()
|
||||
field_names = fieldnames(typeof(n))
|
||||
for i in field_names
|
||||
if i in keys(params)
|
||||
if i == :optimiser
|
||||
opt_type = string(split(params[i], ".")[end])
|
||||
n.:($i) = load_optimiser(opt_type)
|
||||
else
|
||||
n.:($i) = params[i] # assign params to n struct fields
|
||||
end
|
||||
end
|
||||
end
|
||||
return n
|
||||
end
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
""" lif_neuron struct
|
||||
"""
|
||||
Base.@kwdef mutable struct lif_neuron <: compute_neuron
|
||||
id::Union{Int64,Nothing} = nothing # this neuron ID i.e. position of this neuron in knowledgeFn
|
||||
type::String = "lif_neuron"
|
||||
ExIn_type::Integer = 1 # 1 excitatory, -1 inhabitory
|
||||
# Bn::Union{Float64,Nothing} = Random.rand() # Bias for neuron error
|
||||
knowledgefn_name::Union{String,Nothing} = nothing # knowledgeFn that this neuron belongs to
|
||||
subscription_list::Union{Array{Int64},Nothing} = nothing # list of other neuron that this neuron synapse subscribed to
|
||||
sub_ExIn_type::Array{Int64} = Vector{Int64}() # store ExIn type of subscribed neurons
|
||||
time_stamp::Number = 0.0 # current time
|
||||
w_rec::Union{Array{Float64},Nothing} = nothing # synaptic weight (for receiving signal from other neuron)
|
||||
v_t::Float64 = 0.0 # vᵗ, postsynaptic neuron membrane potential of previous timestep
|
||||
v_t1::Float64 = 0.0 # vᵗ⁺¹, postsynaptic neuron membrane potential at current timestep
|
||||
v_t_default::Union{Float64,Nothing} = 0.0 # default membrane potential voltage
|
||||
v_th::Float64 = 1.0 # vᵗʰ, neuron firing threshold
|
||||
z_t::Bool = false # zᵗ, neuron postsynaptic firing of previous timestep
|
||||
# zᵗ⁺¹, neuron firing status at time = t+1. I need this because the way I calculate all
|
||||
# neurons forward function at each timestep-by-timestep is to do every neuron
|
||||
# forward calculation. Each neuron requires access to other neuron's firing status
|
||||
# during v_t1 calculation hence I need a variable to hold z_t1 so that I'm not replacing z_t
|
||||
z_t1::Bool = false # neuron postsynaptic firing at current timestep (after neuron's calculation)
|
||||
z_i_t::Union{Array{Bool},Nothing} = nothing # neuron presynaptic firing at current timestep (which is other neuron postsynaptic firing of previous timestep)
|
||||
# Bn_wout_decay::Union{Float64,Nothing} = 0.01 # use to balance Bn and w_out
|
||||
|
||||
gamma_pd::Union{Float64,Nothing} = 0.3 # γ_pd, discount factor, value from paper
|
||||
alpha::Union{Float64,Nothing} = nothing # α, neuron membrane potential decay factor
|
||||
phi::Union{Float64,Nothing} = nothing # ϕ, psuedo derivative
|
||||
epsilon_rec::Union{Array{Float64},Nothing} = nothing # ϵ_rec, eligibility vector for neuron spike
|
||||
decayed_epsilon_rec::Union{Array{Float64},Nothing} = nothing # α * epsilon_rec
|
||||
e_rec::Union{Array{Float64},Nothing} = nothing # eligibility trace for neuron spike
|
||||
delta::Union{Float64,Nothing} = 1.0 # δ, discreate timestep size in millisecond
|
||||
last_firing_time::Union{Float64,Nothing} = 0.0 # the last time neuron fires
|
||||
refractory_duration::Union{Float64,Nothing} = 3 # neuron's refratory period in millisecond
|
||||
# refractory_state_active::Union{Bool,Nothing} = false # if true, neuron is in refractory state and cannot process new information
|
||||
refractory_counter::Integer = 0
|
||||
tau_m::Union{Float64,Nothing} = nothing # τ_m, membrane time constant in millisecond
|
||||
eta::Union{Float64,Nothing} = 0.01 # η, learning rate
|
||||
w_rec_change::Union{Array{Float64},Nothing} = nothing # Δw_rec, cumulated w_rec change
|
||||
recurrent_signal::Union{Float64,Nothing} = nothing # incoming recurrent signal
|
||||
alpha_v_t::Union{Float64,Nothing} = nothing # alpha * v_t
|
||||
voltage_drop_percentage::Union{Float64,Nothing} = 1.0 # voltage drop as a percentage of v_th
|
||||
error::Union{Float64,Nothing} = nothing # local neuron error
|
||||
optimiser::Union{Any,Nothing} = load_optimiser("AdaBelief") # Flux optimizer
|
||||
|
||||
firing_counter::Float64 = 0.0 # store how many times neuron fires
|
||||
firing_rate_target::Float64 = 20.0 # neuron's target firing rate in Hz
|
||||
firing_diff::Float64 = 0.0 # e-prop supplement paper equation 5
|
||||
firing_rate_error::Float64 = 0.0 # local neuron error w.r.t. firing regularization
|
||||
firing_rate::Float64 = 0.0 # running average of firing rate in Hz
|
||||
|
||||
current_error::Union{Float64,Nothing} = 0.0
|
||||
previous_error::Union{Float64,Nothing} = 0.0
|
||||
error_diff::Union{Array{Float64},Nothing} = Vector{Float64}()
|
||||
|
||||
""" "inference" = no learning params will be collected.
|
||||
"learning" = neuron will accumulate epsilon_j, compute Δw_rec_change each time
|
||||
correct answer is available then merge Δw_rec_change into w_rec_change then
|
||||
reset epsilon_j.
|
||||
"reflect" = neuron will merge w_rec_change into w_rec then reset w_rec_change. """
|
||||
learning_stage::String = "inference"
|
||||
end
|
||||
|
||||
""" lif neuron outer constructor
|
||||
|
||||
# Example
|
||||
|
||||
lif_neuron_params = Dict(
|
||||
:type => "lif_neuron",
|
||||
:v_th => 1.2, # neuron firing threshold (this value is treated as maximum bound if I use auto generate)
|
||||
:z_t => false, # neuron firing status at time = t
|
||||
:gamma_pd => 0.3, # discount factor. The value is from the paper
|
||||
:refractory_duration => 2.0, # neuron refractory period in tick
|
||||
:delta => 1.0,
|
||||
:tau_m => 5.0, # membrane time constant in millisecond. It should equals to time use for 1 sequence
|
||||
)
|
||||
|
||||
neuron1 = lif_neuron(lif_neuron_params)
|
||||
"""
|
||||
function lif_neuron(params::Dict)
|
||||
n = lif_neuron()
|
||||
field_names = fieldnames(typeof(n))
|
||||
for i in field_names
|
||||
if i in keys(params)
|
||||
if i == :optimiser
|
||||
opt_type = string(split(params[i], ".")[end])
|
||||
n.:($i) = load_optimiser(opt_type)
|
||||
else
|
||||
n.:($i) = params[i] # assign params to n struct fields
|
||||
end
|
||||
end
|
||||
end
|
||||
return n
|
||||
end
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
""" alif_neuron struct
|
||||
"""
|
||||
Base.@kwdef mutable struct alif_neuron <: compute_neuron
|
||||
id::Union{Int64,Nothing} = nothing # this neuron ID i.e. position of this neuron in knowledgeFn
|
||||
type::String = "alif_neuron"
|
||||
ExIn_type::Integer = -1 # 1 excitatory, -1 inhabitory
|
||||
# Bn::Union{Float64,Nothing} = Random.rand() # Bias for neuron error
|
||||
knowledgefn_name::Union{String,Nothing} = nothing # knowledgeFn that this neuron belongs to
|
||||
subscription_list::Union{Array{Int64},Nothing} = nothing # list of other neuron that this neuron synapse subscribed to
|
||||
sub_ExIn_type::Array{Int64} = Vector{Int64}() # store ExIn type of subscribed neurons
|
||||
time_stamp::Union{Number,Nothing} = nothing # current time
|
||||
w_rec::Union{Array{Float64},Nothing} = nothing # synaptic weight (for receiving signal from other neuron)
|
||||
v_t::Float64 = 0.0 # vᵗ, postsynaptic neuron membrane potential of previous timestep
|
||||
v_t1::Float64 = 0.0 # vᵗ⁺¹, postsynaptic neuron membrane potential at current timestep
|
||||
v_t_default::Union{Float64,Nothing} = 0.0
|
||||
v_th::Float64 = 1.0 # vᵗʰ, neuron firing threshold
|
||||
z_t::Bool = false # zᵗ, neuron postsynaptic firing of previous timestep
|
||||
# zᵗ⁺¹, neuron firing status at time = t+1. I need this because the way I calculate all
|
||||
# neurons forward function at each timestep-by-timestep is to do every neuron
|
||||
# forward calculation. Each neuron requires access to other neuron's firing status
|
||||
# during v_t1 calculation hence I need a variable to hold z_t1 so that I'm not replacing z_t
|
||||
z_t1::Bool = false # neuron postsynaptic firing at current timestep (after neuron's calculation)
|
||||
z_i_t::Union{Array{Bool},Nothing} = nothing # neuron presynaptic firing at current timestep (which is other neuron postsynaptic firing of previous timestep)
|
||||
# Bn_wout_decay::Union{Float64,Nothing} = 0.01 # use to balance Bn and w_out
|
||||
|
||||
alpha::Union{Float64,Nothing} = nothing # α, neuron membrane potential decay factor
|
||||
delta::Union{Float64,Nothing} = 1.0 # δ, discreate timestep size in millisecond
|
||||
epsilon_rec::Union{Array{Float64},Nothing} = nothing # ϵ_rec(v), eligibility vector for neuron i spike
|
||||
epsilon_rec_a::Union{Array{Float64},Nothing} = nothing # ϵ_rec(a)
|
||||
decayed_epsilon_rec::Union{Array{Float64},Nothing} = nothing # α * epsilon_rec
|
||||
e_rec_v::Union{Array{Float64},Nothing} = nothing # a component of neuron's eligibility trace resulted from v_t
|
||||
e_rec_a::Union{Array{Float64},Nothing} = nothing # a component of neuron's eligibility trace resulted from av_th
|
||||
e_rec::Union{Array{Float64},Nothing} = nothing # neuron's eligibility trace
|
||||
eta::Union{Float64,Nothing} = 0.01 # eta, learning rate
|
||||
gamma_pd::Union{Float64,Nothing} = 0.3 # γ_pd, discount factor, value from paper
|
||||
last_firing_time::Union{Float64,Nothing} = 0.0 # the last time neuron fires
|
||||
phi::Union{Float64,Nothing} = nothing # ϕ, psuedo derivative
|
||||
refractory_duration::Union{Float64,Nothing} = 3 # neuron's refractory period in millisecond
|
||||
# refractory_state_active::Union{Bool,Nothing} = false # if true, neuron is in refractory state and cannot process new information
|
||||
refractory_counter::Integer = 0
|
||||
tau_m::Union{Float64,Nothing} = nothing # τ_m, membrane time constant in millisecond
|
||||
w_rec_change::Union{Array{Float64},Nothing} = nothing # Δw_rec, cumulated w_rec change
|
||||
recurrent_signal::Union{Float64,Nothing} = nothing # incoming recurrent signal
|
||||
alpha_v_t::Union{Float64,Nothing} = nothing # alpha * v_t
|
||||
voltage_drop_percentage::Union{Float64,Nothing} = 1.0 # voltage drop as a percentage of v_th
|
||||
error::Union{Float64,Nothing} = nothing # local neuron error
|
||||
optimiser::Union{Any,Nothing} = load_optimiser("AdaBelief") # Flux optimizer
|
||||
|
||||
firing_counter::Float64 = 0.0 # store how many times neuron fires
|
||||
firing_rate_target::Float64 = 20.0 # neuron's target firing rate in Hz
|
||||
firing_diff::Float64 = 0.0 # e-prop supplement paper equation 5
|
||||
firing_rate_error::Float64 = 0.0 # local neuron error w.r.t. firing regularization
|
||||
firing_rate::Float64 = 0.0 # running average of firing rate, Hz
|
||||
|
||||
|
||||
current_error::Union{Float64,Nothing} = 0.0
|
||||
previous_error::Union{Float64,Nothing} = 0.0
|
||||
error_diff::Union{Array{Float64},Nothing} = Vector{Float64}()
|
||||
|
||||
tau_a::Union{Float64,Nothing} = nothing # τ_a, adaption time constant in millisecond
|
||||
beta::Union{Float64,Nothing} = 0.15 # β, constant, value from paper
|
||||
rho::Union{Float64,Nothing} = nothing # ρ, threshold adaptation decay factor
|
||||
a::Union{Float64,Nothing} = 0.0 # threshold adaptation
|
||||
av_th::Union{Float64,Nothing} = nothing # adjusted neuron firing threshold
|
||||
|
||||
""" "inference" = no learning params will be collected.
|
||||
"learning" = neuron will accumulate epsilon_j, compute Δw_rec_change each time
|
||||
correct answer is available then merge Δw_rec_change into w_rec_change then
|
||||
reset epsilon_j.
|
||||
"reflect" = neuron will merge w_rec_change into w_rec then reset w_rec_change. """
|
||||
learning_stage::String = "inference"
|
||||
|
||||
end
|
||||
""" alif neuron outer constructor
|
||||
|
||||
# Example
|
||||
|
||||
alif_neuron_params = Dict(
|
||||
:type => "alif_neuron",
|
||||
:v_th => 1.2, # neuron firing threshold (this value is treated as maximum bound if I
|
||||
use auto generate)
|
||||
:z_t => false, # neuron firing status at time = t
|
||||
:gamma_pd => 0.3, # discount factor. The value is from the paper
|
||||
:refractory_duration => 2.0, # neuron refractory period in millisecond
|
||||
:delta => 1.0,
|
||||
:tau_m => 5.0, # membrane time constant in millisecond. It should equals to time use
|
||||
for 1 sequence
|
||||
|
||||
# adaptation time constant in millisecond. It should equals to total time SNN takes to
|
||||
# perform a task i.e. equals to episode length
|
||||
:tau_a => 10.0,
|
||||
:beta => 0.15, # constant.
|
||||
:a => 0.0,
|
||||
)
|
||||
|
||||
neuron1 = alif_neuron(alif_neuron_params)
|
||||
"""
|
||||
function alif_neuron(params::Dict)
|
||||
n = alif_neuron()
|
||||
field_names = fieldnames(typeof(n))
|
||||
for i in field_names
|
||||
if i in keys(params)
|
||||
if i == :optimiser
|
||||
opt_type = string(split(params[i], ".")[end])
|
||||
n.:($i) = load_optimiser(opt_type)
|
||||
else
|
||||
n.:($i) = params[i] # assign params to n struct fields
|
||||
end
|
||||
end
|
||||
end
|
||||
return n
|
||||
end
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
""" linear_neuron struct
|
||||
"""
|
||||
Base.@kwdef mutable struct linear_neuron <: output_neuron
|
||||
id::Union{Int64,Nothing} = nothing # ID of this neuron which is it position in knowledgeFn array
|
||||
type::String = "linear_neuron"
|
||||
knowledgefn_name::Union{String,Nothing} = nothing # knowledgeFn that this neuron belongs to
|
||||
subscription_list::Union{Array{Int64},Nothing} = nothing # list of other neuron that this neuron synapse subscribed to
|
||||
time_stamp::Union{Number,Nothing} = nothing # current time
|
||||
delta::Union{Float64,Nothing} = 1.0 # δ, discreate timestep size in millisecond
|
||||
out_t::Bool = false # output of linear neuron BEFORE forward()
|
||||
out_t1::Bool = false # output of linear neuron AFTER forward()
|
||||
end
|
||||
|
||||
""" linear neuron outer constructor
|
||||
|
||||
# Example
|
||||
|
||||
linear_neuron_params = Dict(
|
||||
:type => "linear_neuron",
|
||||
:k => 0.9, # output leakink coefficient
|
||||
:tau_out => 5.0, # output time constant in millisecond. It should equals to time use for 1 sequence
|
||||
:out => 0.0, # neuron's output value store here
|
||||
)
|
||||
|
||||
neuron1 = linear_neuron(linear_neuron_params)
|
||||
"""
|
||||
function linear_neuron(params::Dict)
|
||||
n = linear_neuron()
|
||||
field_names = fieldnames(typeof(n))
|
||||
for i in field_names
|
||||
if i in keys(params)
|
||||
if i == :optimiser
|
||||
opt_type = string(split(params[i], ".")[end])
|
||||
n.:($i) = load_optimiser(opt_type)
|
||||
else
|
||||
n.:($i) = params[i] # assign params to n struct fields
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return n
|
||||
end
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
function load_optimiser(optimiser_name::String; params::Union{Dict,Nothing} = nothing)
|
||||
if optimiser_name == "AdaBelief"
|
||||
params = (0.01, (0.9, 0.8))
|
||||
return Flux.Optimise.AdaBelief(params...)
|
||||
elseif optimiser_name == "AdaBelief2"
|
||||
# output neuron requires slower change pace so η is lower than compute neuron at 0.007
|
||||
# because if w_out change too fast, compute neuron will not able to
|
||||
# grapse output neuron moving direction i.e. both compute neuron's direction and
|
||||
# output neuron direction are out of sync.
|
||||
params = (0.007, (0.9, 0.8))
|
||||
return Flux.Optimise.AdaBelief(params...)
|
||||
else
|
||||
error("optimiser is not defined yet in load_optimiser()")
|
||||
end
|
||||
end
|
||||
|
||||
function init_neuron!(id::Int64, n::passthrough_neuron, n_params::Dict, kfn_params::Dict)
|
||||
n.id = id
|
||||
n.knowledgefn_name = kfn_params[:knowledgefn_name]
|
||||
end
|
||||
|
||||
# function init_neuron!(id::Int64, n::lif_neuron, kfn_params::Dict)
|
||||
# n.id = id
|
||||
# n.knowledgefn_name = kfn_params[:knowledgefn_name]
|
||||
# subscription_options = shuffle!([1:(kfn_params[:input_neuron_number]+kfn_params[:compute_neuron_number])...])
|
||||
# if typeof(kfn_params[:synaptic_connection_number]) == String
|
||||
# percent = parse(Int, kfn_params[:synaptic_connection_number][1:end-1]) / 100
|
||||
# synaptic_connection_number = floor(length(subscription_options) * percent)
|
||||
# n.subscription_list = [pop!(subscription_options) for i = 1:synaptic_connection_number]
|
||||
# end
|
||||
# filter!(x -> x != n.id, n.subscription_list)
|
||||
# n.epsilon_rec = zeros(length(n.subscription_list))
|
||||
# n.w_rec = Random.rand(length(n.subscription_list))
|
||||
# n.w_rec_change = zeros(length(n.subscription_list))
|
||||
# n.reg_voltage_b = zeros(length(n.subscription_list))
|
||||
# n.alpha = calculate_α(n)
|
||||
# end
|
||||
|
||||
function init_neuron!(id::Int64, n::lif_neuron, n_params::Dict, kfn_params::Dict)
|
||||
n.id = id
|
||||
n.knowledgefn_name = kfn_params[:knowledgefn_name]
|
||||
subscription_options = shuffle!([1:kfn_params[:total_neurons]...])
|
||||
subscription_numbers = Int(floor(n_params[:synaptic_connection_number] *
|
||||
kfn_params[:total_neurons] / 100.0))
|
||||
n.subscription_list = [pop!(subscription_options) for i = 1:subscription_numbers]
|
||||
|
||||
# prevent subscription to itself by removing this neuron id
|
||||
filter!(x -> x != n.id, n.subscription_list)
|
||||
|
||||
n.epsilon_rec = zeros(length(n.subscription_list))
|
||||
n.w_rec = Random.rand(length(n.subscription_list))
|
||||
n.w_rec_change = zeros(length(n.subscription_list))
|
||||
# n.reg_voltage_b = zeros(length(n.subscription_list))
|
||||
n.alpha = calculate_α(n)
|
||||
end
|
||||
|
||||
function init_neuron!(id::Int64, n::alif_neuron, n_params::Dict,
|
||||
kfn_params::Dict)
|
||||
n.id = id
|
||||
n.knowledgefn_name = kfn_params[:knowledgefn_name]
|
||||
subscription_options = shuffle!([1:kfn_params[:total_neurons]...])
|
||||
subscription_numbers = Int(floor(n_params[:synaptic_connection_number] *
|
||||
kfn_params[:total_neurons] / 100.0))
|
||||
n.subscription_list = [pop!(subscription_options) for i = 1:subscription_numbers]
|
||||
|
||||
# prevent subscription to itself by removing this neuron id
|
||||
filter!(x -> x != n.id, n.subscription_list)
|
||||
|
||||
n.epsilon_rec = zeros(length(n.subscription_list))
|
||||
n.w_rec = Random.rand(length(n.subscription_list))
|
||||
n.w_rec_change = zeros(length(n.subscription_list))
|
||||
# n.reg_voltage_b = zeros(length(n.subscription_list))
|
||||
n.alpha = calculate_α(n) # the more time has passed from the last time neuron was
|
||||
# activated, the more neuron membrane potential is reduced
|
||||
n.rho = calculate_ρ(n)
|
||||
n.epsilon_rec_a = zeros(length(n.subscription_list))
|
||||
end
|
||||
|
||||
# function init_neuron!(id::Int64, n::linear_neuron, kfn_params::Dict)
|
||||
# n.id = id
|
||||
# n.knowledgefn_name = kfn_params[:knowledgefn_name]
|
||||
# start_id = kfn_params[:input_neuron_number] + 1 # don't readout from input neurons
|
||||
# n.subscription_list = [start_id:(start_id+kfn_params[:compute_neuron_number]-1)...]
|
||||
# n.epsilon_j = zeros(length(n.subscription_list))
|
||||
# n.w_out = Random.randn(length(n.subscription_list))
|
||||
# n.w_out_change = zeros(length(n.subscription_list))
|
||||
# n.b = Random.randn()
|
||||
# n.b_change = 0.0
|
||||
# n.k = calculate_k(n)
|
||||
# end
|
||||
#WORKING
|
||||
function init_neuron!(id::Int64, n::linear_neuron, n_params::Dict, kfn_params::Dict)
|
||||
n.id = id
|
||||
n.knowledgefn_name = kfn_params[:knowledgefn_name]
|
||||
# start_id = kfn_params[:total_input_port] + 1 # don't readout from input neurons
|
||||
# subscription_options = [start_id:(start_id+kfn_params[:total_compute_neuron]-1)...]
|
||||
# n.subscription_list = [rand(subscription_options)]
|
||||
|
||||
# n.epsilon_j = zeros(length(n.subscription_list))
|
||||
# n.w_out = Random.randn(length(n.subscription_list))
|
||||
# n.w_out_change = zeros(length(n.subscription_list))
|
||||
# n.b = Random.randn()
|
||||
# n.b_change = 0.0
|
||||
# n.k = calculate_k(n)
|
||||
end
|
||||
|
||||
""" Make a neuron intended for use with knowledgeFn
|
||||
"""
|
||||
function init_neuron(id::Int64, n_params::Dict, kfn_params::Dict)
|
||||
n = instantiate_custom_types(n_params)
|
||||
init_neuron!(id, n, n_params, kfn_params)
|
||||
|
||||
return n
|
||||
end
|
||||
|
||||
""" This function instantiate Ironpen type.
|
||||
|
||||
# Example
|
||||
|
||||
new_model = instantiate_custom_types("model")
|
||||
"""
|
||||
function instantiate_custom_types(params::Union{Dict,Nothing} = nothing)
|
||||
type = string(split(params[:type], ".")[end])
|
||||
|
||||
if type == "model"
|
||||
return model()
|
||||
elseif type == "knowledgeFn"
|
||||
return knowledgeFn()
|
||||
elseif type == "passthrough_neuron"
|
||||
return passthrough_neuron(params)
|
||||
elseif type == "lif_neuron"
|
||||
return lif_neuron(params)
|
||||
elseif type == "alif_neuron"
|
||||
return alif_neuron(params)
|
||||
elseif type == "linear_neuron"
|
||||
return linear_neuron(params)
|
||||
else
|
||||
return nothing
|
||||
end
|
||||
end
|
||||
|
||||
""" Add a new neuron into a knowledgeFn
|
||||
|
||||
# Example
|
||||
add_neuron!(kfn.kfn_params[:lif_neuron_params], kfn)
|
||||
"""
|
||||
# function add_neuron!(neuron_Dict::Dict, kfn::knowledgeFn)
|
||||
# id = length(kfn.neurons_array) + 1
|
||||
# neuron = init_neuron(id, neuron_Dict, kfn.kfn_params,
|
||||
# total_neurons = (length(kfn.neurons_array) + 1))
|
||||
# push!(kfn.neurons_array, neuron)
|
||||
|
||||
# # Randomly select an output neuron to add a new neuron to
|
||||
# add_n_output_n!(Random.rand(kfn.output_neurons_array), id)
|
||||
# end
|
||||
|
||||
""" Add a new neuron to output neuron's subscription_list
|
||||
"""
|
||||
function add_n_output_n!(o_n::linear_neuron, id::Int64)
|
||||
push!(o_n.subscription_list, id)
|
||||
push!(o_n.epsilon_j, 0.0)
|
||||
push!(o_n.w_out, Random.randn(1)[1])
|
||||
push!(o_n.w_out_change, 0.0)
|
||||
end
|
||||
|
||||
calculate_α(neuron::lif_neuron) = exp(-neuron.delta / neuron.tau_m)
|
||||
calculate_α(neuron::alif_neuron) = exp(-neuron.delta / neuron.tau_m)
|
||||
calculate_ρ(neuron::alif_neuron) = exp(-neuron.delta / neuron.tau_a)
|
||||
calculate_k(neuron::linear_neuron) = exp(-neuron.delta / neuron.tau_out)
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
end # module end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user