spaic.Learning package

Submodules

spaic.Learning.Learner module

Created on 2020/8/12 @project: SPAIC @filename: Learner @author: Hong Chaofei @contact: hongchf@gmail.com

@description: 定义学习模块,包括各种学习算法对仿真计算过程中插入的各种计算模块,以及记录需要学习连接的接口

class spaic.Learning.Learner.Learner(trainable=None, pathway=None, algorithm=('STCA', 'STBP', 'RSTDP', '...'), **kwargs)[source]

Bases: BaseModule, ABC

Base learner model for all the learner model.

Parameters:
  • parameters (dict) – The parameters for learner.

  • super_parameters (dict) – Super parameters for future use.

  • backend_functions (dict) – Contains all the learner model we can choose.

  • name (str) – The typical name for the learner.

  • preferred_backend (str) – Choose which kind of backend to use. Like “Pytorch”, “Tensorflow” or “Jax”.

  • trainable_groups (dict) – Trainable container, includes nodes and layers to train.

  • trainable_connections (dict) – Trainable container, includes connections to train.

  • init_trainable – The initial state of this learner of whether it is trainable.

add_trainable(self, trainable)[source]

Add target object (Network, Assembly, Connection, or list of them) to the trainable container

build(self, backend)[source]

Build Learner, choose the backend as user wish, if we have already finished the api.

learning_algorithms = {'bp_rstdp': <class 'spaic.Learning.Backprop_RSTDP.Backprop_RSTDP'>, 'bp_rstdpet': <class 'spaic.Learning.Backprop_RSTDP.Backprop_RSTDP'>, 'conv2d_rstdp': <class 'spaic.Learning.Conv_RSTDP.Conv2d_RSTDP'>, 'conv2d_stdp': <class 'spaic.Learning.Conv_STDP.Conv2d_STDP'>, 'full_online_stdp': <class 'spaic.Learning.STDP_Learner.full_online_STDP'>, 'meta_nearest_online_stdp': <class 'spaic.Learning.STDP_Learner.Meta_nearest_online_STDP'>, 'nearest_online_stdp': <class 'spaic.Learning.STDP_Learner.nearest_online_STDP'>, 'postpreintstdp': <class 'spaic.Learning.STDP_Learner.PostPreIntSTDP'>, 'rate_modulate': <class 'spaic.Learning.Rate_Modulation.Rate_Modulate'>, 'rstdp': <class 'spaic.Learning.RSTDP.RSTDP'>, 'rstdpet': <class 'spaic.Learning.RSTDP.RSTDPET'>, 'sbp': <class 'spaic.Learning.SurrogateBP_Learner.SurrogateBP'>, 'stbp': <class 'spaic.Learning.STBP_Learner.STBP'>, 'stca': <class 'spaic.Learning.STCA_Learner.STCA'>, 'switch_rstdp': <class 'spaic.Learning.RSTDP.RewardSwitchSTDP'>, 'tempotron': <class 'spaic.Learning.Tempotron_Learner.Tempotron'>}
learning_optims = {}
optim_dict = {'ASGD': <class 'torch.optim.asgd.ASGD'>, 'Adadelta': <class 'torch.optim.adadelta.Adadelta'>, 'Adagrad': <class 'torch.optim.adagrad.Adagrad'>, 'Adam': <class 'torch.optim.adam.Adam'>, 'AdamW': <class 'torch.optim.adamw.AdamW'>, 'Adamax': <class 'torch.optim.adamax.Adamax'>, 'LBFGS': <class 'torch.optim.lbfgs.LBFGS'>, 'RMSprop': <class 'torch.optim.rmsprop.RMSprop'>, 'Rpop': <class 'torch.optim.rprop.Rprop'>, 'SGD': <class 'torch.optim.sgd.SGD'>, 'SparseAdam': <class 'torch.optim.sparse_adam.SparseAdam'>}
lr_schedule_dict = {'CosineAnnealingLR': <class 'torch.optim.lr_scheduler.CosineAnnealingLR'>, 'CosineAnnealingWarmRestarts': <class 'torch.optim.lr_scheduler.CosineAnnealingWarmRestarts'>, 'CyclicLR': <class 'torch.optim.lr_scheduler.CyclicLR'>, 'ExponentialLR': <class 'torch.optim.lr_scheduler.ExponentialLR'>, 'LambdaLR': <class 'torch.optim.lr_scheduler.LambdaLR'>, 'MultiStepLR': <class 'torch.optim.lr_scheduler.MultiStepLR'>, 'ReduceLROnPlateau': <class 'torch.optim.lr_scheduler.ReduceLROnPlateau'>, 'StepLR': <class 'torch.optim.lr_scheduler.StepLR'>}
add_trainable(trainable: list)[source]

Add target object (Assembly, Connection, or list of them) to the trainable container :param trainable: The trainable target waiting for added. :type trainable: list

add_pathway(pathway: list)[source]

Add target object (Assembly, Connection, or list of them) to the pathway container :param pathway: The pathway target waiting for added. :type pathway: list

build(backend: Backend)[source]

Build Learner, choose the backend as user wish, if we have already finished the api. :param backend: Backend we have. :type backend: backend

active()[source]
deactive()[source]
set_optimizer(optim_name, optim_lr, **kwargs)[source]
set_schedule(lr_schedule_name, **kwargs)[source]
get_param()[source]
get_varname(key)[source]
get_var_names()[source]
build_optimizer()[source]
build_lr_schedule()[source]
optim_step()[source]
optim_zero_grad()[source]
optim_schedule()[source]
static register(name, algorithm)[source]
custom_rule(backend: Backend)[source]
connection_rule(con: Connection, backend: Backend, obj_type='trainable')[source]
neuron_rule(neuron: NeuronGroup, backend: Backend, obj_type='trainable')[source]
is_overridden(func)[source]
class spaic.Learning.Learner.ReSuMe(trainable=None, pathway=None, algorithm=('STCA', 'STBP', 'RSTDP', '...'), **kwargs)[source]

Bases: Learner

class spaic.Learning.Learner.FORCE(trainable=None, pathway=None, algorithm=('STCA', 'STBP', 'RSTDP', '...'), **kwargs)[source]

Bases: Learner

spaic.Learning.RSTDP module

@author: Yuan Mengwen @contact: mwyuan94@gmail.com @project: PyCharm @filename: RSTDP.py @time:2021/4/8 10:46 @description:

class spaic.Learning.RSTDP.RSTDP(trainable=None, pathway=None, algorithm=('STCA', 'STBP', 'RSTDP', '...'), **kwargs)[source]

Bases: Learner

Reward-modulated STDP. Adapted from (Florian 2007) <https://florian.io/papers/2007_Florian_Modulated_STDP.pdf>. :param lr: learning rate :type lr: int or float :param trainable: It can be network or neurongroups

Variables:
  • tau_plus (int or float) – Time constant for pre -synaptic firing trace determines the range of interspike intervals over which synaptic occur.

  • tau_minus (int or float) – Time constant for post-synaptic firing trace.

  • a_plus (float) – Learning rate for post-synaptic.

  • a_minus (flaot) – Learning rate for pre-synaptic.

weight_update(weight, eligibility, reward)[source]

RSTDP learning rule for Connection subclass of AbstractConnection class. :param weight: weight between pre and post neurongroup :param eligibility: a decaying memory of the relationships between the recent pairs of pre and postsynaptic spike pairs :param reward: reward signal

build(backend)[source]
class spaic.Learning.RSTDP.RSTDPET(trainable=None, pathway=None, algorithm=('STCA', 'STBP', 'RSTDP', '...'), **kwargs)[source]

Bases: Learner

Reward-modulated STDP with eligibility trace. Adapted from (Florian 2007) <https://florian.io/papers/2007_Florian_Modulated_STDP.pdf>. :param lr: learning rate :type lr: int or float :param trainable: It can be network or neurongroups

Variables:
  • tau_plus (int or float) – Time constant for pre -synaptic firing trace determines the range of interspike intervals over which synaptic occur.

  • tau_minus (int or float) – Time constant for post-synaptic firing trace.

  • a_plus (float) – Learning rate for post-synaptic.

  • a_minus (flaot) – Learning rate for pre-synaptic.

Notes

Batch_size for network using RSTDPET as learning algorithm must be 1.

weight_update(weight, eligibility_trace, reward)[source]

RSTDPET learning rule for Connection subclass of AbstractConnection class. .. rubric:: Notes

The batch of pre and post should be 1

Parameters:
  • weight – weight between pre and post neurongroup

  • eligibility_trace – a decaying memory of the relationships between the recent pairs of pre and postsynaptic spike pairs

  • reward – reward signal

build(backend)[source]
class spaic.Learning.RSTDP.RewardSwitchSTDP(trainable=None, pathway=None, algorithm=('STCA', 'STBP', 'RSTDP', '...'), **kwargs)[source]

Bases: Learner

update(input, output, reward, input_trace, output_trace, pre_decay, post_decay, Apost_pos, Apost_neg, Apre_pos, Apre_neg, m_rate, homoestatic, weight)[source]
build(backend)[source]

spaic.Learning.Rate_Modulation module

Created on 2021/6/1 @project: SPAIC @filename: Rate_Modulation @author: Hong Chaofei @contact: hongchf@gmail.com

@description:

class spaic.Learning.Rate_Modulation.Rate_Modulate(trainable=None, pathway=None, algorithm=('STCA', 'STBP', 'RSTDP', '...'), **kwargs)[source]

Bases: Learner

neuron spiking rate modulation

build(backend: Backend)[source]
property rate_loss
class spaic.Learning.Rate_Modulation.Modulation_method(weight, m_r, alpha, var_name)[source]

Bases: object

record_rate(out)[source]
modulate_step()[source]
property rate_loss

spaic.Learning.STBP_Learner module

Created on 2021/3/30 @project: SPAIC @filename: STBP_Learner @author: Mengxiao Zhang @contact: mxzhangice@gmail.com

@description:

class spaic.Learning.STBP_Learner.STBP(trainable=None, pathway=None, algorithm=('STCA', 'STBP', 'RSTDP', '...'), **kwargs)[source]

Bases: Learner

STBP learning rule.

Parameters:
  • alpha (num) – The parameter alpha of STBP learning model.

  • trainable – The parameter whether it can be trained.

build(self, backend)[source]

Build the backend, realize the algorithm of STBP model.

threshold(self, x, v_th)[source]

Get the threshold of the STBP model.

Example

Net._learner = STBP(0.5, Net)

Reference:

Yujie Wu et al. “Spatio-Temporal Backpropagation for Training High-performance Spiking Neural Networks” In: Frontiers in Neuroscience, 2018. Volume 12. pp. 331. doi:10.3389/fnins.2018.00331 url:ttps://www.frontiersin.org/article/10.3389/fnins.2018.00331

build(backend)[source]

Build the backend, realize the algorithm of STBP model. Args:

backend: The backend we used to compute.

threshold(x, v_th)[source]

Get the threshold of the STBP model.

Returns:

A method that use STBP model to compute the threshold.

spaic.Learning.STCA_Learner module

Created on 2020/11/9 @project: SPAIC @filename: STCA_Learner @author: Hong Chaofei @contact: hongchf@gmail.com

@description:

class spaic.Learning.STCA_Learner.ActFun(*args, **kwargs)[source]

Bases: Function

Approximate firing func.

static forward(ctx, input, thresh, alpha)[source]
static backward(ctx, grad_output)[source]
spaic.Learning.STCA_Learner.firing_func(x, v_th, alpha)[source]
class spaic.Learning.STCA_Learner.STCA(trainable=None, pathway=None, algorithm=('STCA', 'STBP', 'RSTDP', '...'), **kwargs)[source]

Bases: Learner

STCA learning rule.

Parameters:
  • alpha (num) – The parameter alpha of STCA learning model.

  • preferred_backend (list) – The backend prefer to use, should be a list.

  • name (str) – The name of this learning model. Should be ‘STCA’.

  • firing_func – The function of fire.

build(self, backend)[source]

Build the backend, realize the algorithm of STCA model.

threshold(self, x, v_th)

Get the threshold of the STCA model.

Example

Net._learner = STCA(0.5, Net)

Reference:

Pengjie Gu et al. “STCA: Spatio-Temporal Credit Assignment with Delayed Feedback in Deep SpikingNeural Networks.” In:Proceedings of the Twenty-Eighth International Joint Conference on Artificial Intelligence, IJCAI-19. International Joint Conferences on Artificial Intelligence Organization, July 2019,pp. 1366–1372. doi:10.24963/ijcai.2019/189. url:https://doi.org/10.24963/ijcai.2019/189.

build(backend)[source]

Build the backend, realize the algorithm of STCA model.

Args:

backend: The backend we used to compute.

torch_threshold(x, v_th)[source]

Get the threshold of the STCA model.

Returns:

A method that use STCA model to compute the threshold.

spaic.Learning.STDP_Learner module

class spaic.Learning.STDP_Learner.Base_STDP(trainable=None, pathway=None, algorithm=('STCA', 'STBP', 'RSTDP', '...'), **kwargs)[source]

Bases: Learner

build(backend)[source]
class spaic.Learning.STDP_Learner.nearest_online_STDP(trainable=None, pathway=None, algorithm=('STCA', 'STBP', 'RSTDP', '...'), **kwargs)[source]

Bases: Base_STDP

nearest_online STDP learning rule.

Parameters:
  • Apost (num) – The parameter Apost of nearest_online STDP learning model.

  • Apre (num) – The parameter Apre of nearest_online STDP learning model.

  • trace_decay (num) – The parameter trace_decay of nearest_online STDP learning model.

  • preferred_backend (list) – The backend prefer to use, should be a list.

  • name (str) – The name of this learning model. Should be ‘nearest_online_STDP’.

initial_param(self, input, output)

initialize the output_trace and the input_trace for each batch.

nearest_online_stdp_weightupdate(self, input, output, weight)[source]

calculate the update of weight

build(self, backend)[source]

Build the backend, realize the algorithm of nearest_online STDP learning model.

Example

self._learner = BaseLearner(algorithm=’nearest_online_STDP’, lr=0.5, trainable=self, conn=self.connection1)

Reference:

Unsupervised learning of digit recognition using spike-timing-dependent plasticity. doi: 10.3389/fncom.2015.00099. url: http://journal.frontiersin.org/article/10.3389/fncom.2015.00099/abstract

nearest_online_stdp_weightupdate(dw, weight)[source]
Parameters:
  • dw – the change of weight

  • weight – weight between pre and post neurongroup

Returns:

Updated weight.

build(backend)[source]
class spaic.Learning.STDP_Learner.full_online_STDP(trainable=None, pathway=None, algorithm=('STCA', 'STBP', 'RSTDP', '...'), **kwargs)[source]

Bases: Base_STDP

nearest_online STDP learning rule.

Parameters:
  • Apost (num) – The parameter Apost of full_online STDP learning model.

  • Apre (num) – The parameter Apre of full_online STDP learning model.

  • trace_decay (num) – The parameter trace_decay of full_online STDP learning model.

  • preferred_backend (list) – The backend prefer to use, should be a list.

  • name (str) – The name of this learning model. Should be ‘full_online_STDP’.

initial_param(self, input, output)

initialize the output_trace and the input_trace for each batch.

nearest_online_stdp_weightupdate(self, input, output, weight)

calculate the update of weight

build(self, backend)[source]

Build the backend, realize the algorithm of full_online STDP learning model.

Example

self._learner = BaseLearner(algorithm=’full_online_STDP’, lr=0.5, trainable=self, conn=self.connection1)

Reference:

Unsupervised learning of digit recognition using spike-timing-dependent plasticity. doi: 10.3389/fncom.2015.00099. url: http://journal.frontiersin.org/article/10.3389/fncom.2015.00099/abstract

full_online_stdp_weightupdate(dw, weight)[source]
Parameters:
  • dw – the change of weight

  • weight – weight between pre and post neurongroup

Returns:

Updated weight.

build(backend)[source]
class spaic.Learning.STDP_Learner.Meta_nearest_online_STDP(trainable=None, pathway=None, algorithm=('STCA', 'STBP', 'RSTDP', '...'), **kwargs)[source]

Bases: Base_STDP

update(input, output, input_trace, output_trace, pre_decay, post_decay, Apost, Apre, weight)[source]
build(backend)[source]
class spaic.Learning.STDP_Learner.PostPreIntSTDP(trainable=None, pathway=None, algorithm=('STCA', 'STBP', 'RSTDP', '...'), **kwargs)[source]

Bases: Base_STDP

Simple STDP rule involving both pre- and post-synaptic spiking activity, based on integer arithmetic. By default, pre-synaptic update is negative and the post-synaptic update is positive.

update(input, output, input_trace, output_trace, trace_decay, trace_scale, shift_spike_trace, max_threshold, post_thresh, weight)[source]
build(backend)[source]

spaic.Learning.SpikeProp_Learner module

spaic.Learning.TRUE_Learner module

Module contents

Created on 2020/8/11 @project: SPAIC @author: Hong Chaofei @contact: hongchf@gmail.com

@description: