spikingjelly.activation_based.learning module#
- spikingjelly.activation_based.learning.stdp_linear_single_step(fc, in_spike, out_spike, trace_pre, trace_post, tau_pre, tau_post, f_pre=<function <lambda>>, f_post=<function <lambda>>)[源代码]#
-
中文
对单步脉冲输入执行全连接层的 STDP 更新,返回更新后的 pre/post trace 与权重增量。
- 参数:
fc (Linear) -- 需要执行 STDP 的全连接层
in_spike (Tensor) -- 输入脉冲,
shape = [batch_size, in_features]out_spike (Tensor) -- 输出脉冲,
shape = [batch_size, out_features]trace_pre (Union[float, Tensor, None]) -- pre-synaptic trace。若为
None,则使用标量0.0初始化trace_post (Union[float, Tensor, None]) -- post-synaptic trace。若为
None,则使用标量0.0初始化tau_pre (float) -- pre trace 的时间常数,要求为正数
tau_post (float) -- post trace 的时间常数,要求为正数
f_pre (Callable) -- 作用在当前权重上的 pre-update 调制函数
f_post (Callable) -- 作用在当前权重上的 post-update 调制函数
- 返回:
(trace_pre, trace_post, delta_w),其中delta_w的形状与fc.weight相同- 返回类型:
English
Apply one STDP update step to a linear layer driven by single-step spike inputs, and return the updated pre/post traces together with the weight increment.
- 参数:
fc (Linear) -- Linear layer updated by STDP
in_spike (Tensor) -- Input spikes with
shape = [batch_size, in_features]out_spike (Tensor) -- Output spikes with
shape = [batch_size, out_features]trace_pre (Union[float, Tensor, None]) -- Pre-synaptic trace. If
None, it is initialized as scalar0.0trace_post (Union[float, Tensor, None]) -- Post-synaptic trace. If
None, it is initialized as scalar0.0tau_pre (float) -- Time constant of the pre trace. Expected to be positive
tau_post (float) -- Time constant of the post trace. Expected to be positive
f_pre (Callable) -- Pre-update modulation function applied to the current weight
f_post (Callable) -- Post-update modulation function applied to the current weight
- 返回:
(trace_pre, trace_post, delta_w), wheredelta_whas the same shape asfc.weight- 返回类型:
- spikingjelly.activation_based.learning.mstdp_linear_single_step(fc, in_spike, out_spike, trace_pre, trace_post, tau_pre, tau_post, f_pre=<function <lambda>>, f_post=<function <lambda>>)[源代码]#
-
中文
对单步脉冲输入执行全连接层的 reward-modulated STDP eligibility 计算。
- 参数:
fc (Linear) -- 需要执行 mSTDP 的全连接层
in_spike (Tensor) -- 输入脉冲,
shape = [batch_size, in_features]out_spike (Tensor) -- 输出脉冲,
shape = [batch_size, out_features]trace_pre (Union[float, Tensor, None]) -- pre-synaptic trace。若为
None,则使用标量0.0初始化trace_post (Union[float, Tensor, None]) -- post-synaptic trace。若为
None,则使用标量0.0初始化tau_pre (float) -- pre trace 的时间常数
tau_post (float) -- post trace 的时间常数
f_pre (Callable) -- pre 分支的权重调制函数
f_post (Callable) -- post 分支的权重调制函数
- 返回:
(trace_pre, trace_post, eligibility),其中eligibility的形状为[batch_size, out_features, in_features]- 返回类型:
English
Compute the reward-modulated STDP eligibility tensor for a linear layer under single-step spike inputs.
- 参数:
fc (Linear) -- Linear layer updated by mSTDP
in_spike (Tensor) -- Input spikes with
shape = [batch_size, in_features]out_spike (Tensor) -- Output spikes with
shape = [batch_size, out_features]trace_pre (Union[float, Tensor, None]) -- Pre-synaptic trace. If
None, it is initialized as scalar0.0trace_post (Union[float, Tensor, None]) -- Post-synaptic trace. If
None, it is initialized as scalar0.0tau_pre (float) -- Time constant of the pre trace
tau_post (float) -- Time constant of the post trace
f_pre (Callable) -- Weight modulation function for the pre branch
f_post (Callable) -- Weight modulation function for the post branch
- 返回:
(trace_pre, trace_post, eligibility), whereeligibilityhas shape[batch_size, out_features, in_features]- 返回类型:
- spikingjelly.activation_based.learning.mstdpet_linear_single_step(fc, in_spike, out_spike, trace_pre, trace_post, tau_pre, tau_post, tau_trace, f_pre=<function <lambda>>, f_post=<function <lambda>>)[源代码]#
-
中文
对单步脉冲输入执行全连接层的 mSTDP-ET eligibility 计算。
- 参数:
fc (Linear) -- 需要执行 mSTDP-ET 的全连接层
in_spike (Tensor) -- 输入脉冲,
shape = [in_features]out_spike (Tensor) -- 输出脉冲,
shape = [out_features]trace_pre (Union[float, Tensor, None]) -- pre-synaptic trace。若为
None,则使用标量0.0初始化trace_post (Union[float, Tensor, None]) -- post-synaptic trace。若为
None,则使用标量0.0初始化tau_pre (float) -- pre trace 的时间常数
tau_post (float) -- post trace 的时间常数
tau_trace (float) -- eligibility trace 的时间常数
f_pre (Callable) -- pre 分支的权重调制函数
f_post (Callable) -- post 分支的权重调制函数
- 返回:
(trace_pre, trace_post, eligibility),其中eligibility的形状与fc.weight相同- 返回类型:
English
Compute the mSTDP-ET eligibility update for a linear layer under single-step spike inputs.
- 参数:
fc (Linear) -- Linear layer updated by mSTDP-ET
in_spike (Tensor) -- Input spikes with
shape = [in_features]out_spike (Tensor) -- Output spikes with
shape = [out_features]trace_pre (Union[float, Tensor, None]) -- Pre-synaptic trace. If
None, it is initialized as scalar0.0trace_post (Union[float, Tensor, None]) -- Post-synaptic trace. If
None, it is initialized as scalar0.0tau_pre (float) -- Time constant of the pre trace
tau_post (float) -- Time constant of the post trace
tau_trace (float) -- Time constant of the eligibility trace
f_pre (Callable) -- Weight modulation function for the pre branch
f_post (Callable) -- Weight modulation function for the post branch
- 返回:
(trace_pre, trace_post, eligibility), whereeligibilityhas the same shape asfc.weight- 返回类型:
- spikingjelly.activation_based.learning.stdp_conv2d_single_step(conv, in_spike, out_spike, trace_pre, trace_post, tau_pre, tau_post, f_pre=<function <lambda>>, f_post=<function <lambda>>)[源代码]#
-
中文
对单步脉冲输入执行二维卷积层的 STDP 更新。
当前仅支持
dilation == (1, 1)且groups == 1的卷积。- 参数:
conv (Conv2d) -- 需要执行 STDP 的二维卷积层
in_spike (Tensor) -- 输入脉冲,
shape = [batch_size, C_in, H_in, W_in]out_spike (Tensor) -- 输出脉冲,
shape = [batch_size, C_out, H_out, W_out]trace_pre (Union[Tensor, None]) -- pre-synaptic trace。若为
None,则初始化为与in_spike同形状零张量trace_post (Union[Tensor, None]) -- post-synaptic trace。若为
None,则初始化为与out_spike同形状零张量tau_pre (float) -- pre trace 的时间常数
tau_post (float) -- post trace 的时间常数
f_pre (Callable) -- pre 分支的权重调制函数
f_post (Callable) -- post 分支的权重调制函数
- 返回:
(trace_pre, trace_post, delta_w),其中delta_w的形状与conv.weight相同- 返回类型:
- 抛出:
NotImplementedError -- 当
conv.dilation != (1, 1)或conv.groups != 1时抛出
English
Apply one STDP update step to a 2D convolution layer driven by single-step spike inputs.
Only convolutions with
dilation == (1, 1)andgroups == 1are currently supported.- 参数:
conv (Conv2d) -- 2D convolution layer updated by STDP
in_spike (Tensor) -- Input spikes with
shape = [batch_size, C_in, H_in, W_in]out_spike (Tensor) -- Output spikes with
shape = [batch_size, C_out, H_out, W_out]trace_pre (Union[Tensor, None]) -- Pre-synaptic trace. If
None, initialized as zeros with the same shape asin_spiketrace_post (Union[Tensor, None]) -- Post-synaptic trace. If
None, initialized as zeros with the same shape asout_spiketau_pre (float) -- Time constant of the pre trace
tau_post (float) -- Time constant of the post trace
f_pre (Callable) -- Weight modulation function for the pre branch
f_post (Callable) -- Weight modulation function for the post branch
- 返回:
(trace_pre, trace_post, delta_w), wheredelta_whas the same shape asconv.weight- 返回类型:
- 抛出:
NotImplementedError -- Raised when
conv.dilation != (1, 1)orconv.groups != 1
- spikingjelly.activation_based.learning.stdp_conv1d_single_step(conv, in_spike, out_spike, trace_pre, trace_post, tau_pre, tau_post, f_pre=<function <lambda>>, f_post=<function <lambda>>)[源代码]#
-
中文
对单步脉冲输入执行一维卷积层的 STDP 更新。
当前仅支持
dilation == (1,)且groups == 1的卷积。- 参数:
conv (Conv1d) -- 需要执行 STDP 的一维卷积层
in_spike (Tensor) -- 输入脉冲,
shape = [batch_size, C_in, L_in]out_spike (Tensor) -- 输出脉冲,
shape = [batch_size, C_out, L_out]trace_pre (Union[Tensor, None]) -- pre-synaptic trace。若为
None,则初始化为与in_spike同形状零张量trace_post (Union[Tensor, None]) -- post-synaptic trace。若为
None,则初始化为与out_spike同形状零张量tau_pre (float) -- pre trace 的时间常数
tau_post (float) -- post trace 的时间常数
f_pre (Callable) -- pre 分支的权重调制函数
f_post (Callable) -- post 分支的权重调制函数
- 返回:
(trace_pre, trace_post, delta_w),其中delta_w的形状与conv.weight相同- 返回类型:
- 抛出:
NotImplementedError -- 当
conv.dilation != (1,)或conv.groups != 1时抛出
English
Apply one STDP update step to a 1D convolution layer driven by single-step spike inputs.
Only convolutions with
dilation == (1,)andgroups == 1are currently supported.- 参数:
conv (Conv1d) -- 1D convolution layer updated by STDP
in_spike (Tensor) -- Input spikes with
shape = [batch_size, C_in, L_in]out_spike (Tensor) -- Output spikes with
shape = [batch_size, C_out, L_out]trace_pre (Union[Tensor, None]) -- Pre-synaptic trace. If
None, initialized as zeros with the same shape asin_spiketrace_post (Union[Tensor, None]) -- Post-synaptic trace. If
None, initialized as zeros with the same shape asout_spiketau_pre (float) -- Time constant of the pre trace
tau_post (float) -- Time constant of the post trace
f_pre (Callable) -- Weight modulation function for the pre branch
f_post (Callable) -- Weight modulation function for the post branch
- 返回:
(trace_pre, trace_post, delta_w), wheredelta_whas the same shape asconv.weight- 返回类型:
- 抛出:
NotImplementedError -- Raised when
conv.dilation != (1,)orconv.groups != 1
- spikingjelly.activation_based.learning.stdp_multi_step(layer, in_spike, out_spike, trace_pre, trace_post, tau_pre, tau_post, f_pre=<function <lambda>>, f_post=<function <lambda>>)[源代码]#
-
中文
对线性层、一维卷积层或二维卷积层执行多步 STDP 更新。
该函数沿时间维遍历
in_spike与out_spike,并在每个时间步调用对应的 单步 STDP 规则,最终累加得到整段序列的权重增量。- 参数:
layer (Union[Linear, Conv1d, Conv2d]) -- 支持的突触层,目前为
nn.Linear、nn.Conv1d或nn.Conv2din_spike (Tensor) -- 输入脉冲序列,时间维位于第 0 维
out_spike (Tensor) -- 输出脉冲序列,时间维位于第 0 维
trace_pre (Union[float, Tensor, None]) -- pre-synaptic trace 的初始值
trace_post (Union[float, Tensor, None]) -- post-synaptic trace 的初始值
tau_pre (float) -- pre trace 的时间常数
tau_post (float) -- post trace 的时间常数
f_pre (Callable) -- pre 分支的权重调制函数
f_post (Callable) -- post 分支的权重调制函数
- 返回:
(trace_pre, trace_post, delta_w),其中delta_w的形状与layer.weight相同- 返回类型:
English
Apply multi-step STDP updates to a linear layer, a 1D convolution layer, or a 2D convolution layer.
The function iterates over the time dimension of
in_spikeandout_spike, applies the matching single-step STDP rule at each time step, and accumulates the weight increment over the whole sequence.- 参数:
layer (Union[Linear, Conv1d, Conv2d]) -- Supported synaptic layer. Currently
nn.Linear,nn.Conv1dornn.Conv2din_spike (Tensor) -- Input spike sequence with the time axis at dimension 0
out_spike (Tensor) -- Output spike sequence with the time axis at dimension 0
trace_pre (Union[float, Tensor, None]) -- Initial value of the pre-synaptic trace
trace_post (Union[float, Tensor, None]) -- Initial value of the post-synaptic trace
tau_pre (float) -- Time constant of the pre trace
tau_post (float) -- Time constant of the post trace
f_pre (Callable) -- Weight modulation function for the pre branch
f_post (Callable) -- Weight modulation function for the post branch
- 返回:
(trace_pre, trace_post, delta_w), wheredelta_whas the same shape aslayer.weight- 返回类型:
- class spikingjelly.activation_based.learning.STDPLearner(step_mode, synapse, sn, tau_pre, tau_post, f_pre=<function STDPLearner.<lambda>>, f_post=<function STDPLearner.<lambda>>)[源代码]#
基类:
MemoryModule
中文
基于监视器的 STDP 学习器。
该学习器通过
InputMonitor和OutputMonitor自动记录突触层输入脉冲与神经元输出脉冲,并在调用step()时根据step_mode选择单步或多步 STDP 规则。- 参数:
step_mode (str) --
's'表示单步 STDP,'m'表示多步 STDPsynapse (Union[Conv2d, Linear]) -- 需要执行 STDP 的突触层,目前支持
nn.Linear、nn.Conv1d、nn.Conv2dsn (spikingjelly.activation_based.neuron.BaseNode) -- 产生输出脉冲的脉冲神经元模块
tau_pre (float) -- pre trace 的时间常数
tau_post (float) -- post trace 的时间常数
f_pre (Callable) -- pre 分支的权重调制函数
f_post (Callable) -- post 分支的权重调制函数
English
Monitor-based STDP learner.
The learner automatically records synaptic input spikes and neuronal output spikes with
InputMonitorandOutputMonitor. Whenstep()is called, it selects the single-step or multi-step STDP rule according tostep_mode.- 参数:
step_mode (str) --
's'for single-step STDP and'm'for multi-step STDPsynapse (Union[Conv2d, Linear]) -- Synaptic layer updated by STDP. Currently supports
nn.Linear,nn.Conv1d, andnn.Conv2dsn (spikingjelly.activation_based.neuron.BaseNode) -- Spiking neuron module that generates output spikes
tau_pre (float) -- Time constant of the pre trace
tau_post (float) -- Time constant of the post trace
f_pre (Callable) -- Weight modulation function for the pre branch
f_post (Callable) -- Weight modulation function for the post branch
- reset()[源代码]#
-
中文
重置学习器内部状态,并清空输入/输出脉冲监视器中已记录的数据。
English
Reset the learner state and clear all recorded data in the input/output spike monitors.
- disable()[源代码]#
-
中文
禁用输入脉冲与输出脉冲监视器,使其停止记录新数据。
English
Disable the input and output spike monitors so they stop recording new data.
- enable()[源代码]#
-
中文
启用输入脉冲与输出脉冲监视器,使其恢复记录。
English
Enable the input and output spike monitors so they resume recording.
- step(on_grad=True, scale=1.0)[源代码]#
-
中文
使用当前监视器中缓存的脉冲记录执行一次 STDP 权重更新。
当
on_grad=True时,函数会把-delta_w累加到self.synapse.weight.grad; 当on_grad=False时,返回累计的delta_w而不写入梯度。- 参数:
- 返回:
当
on_grad=False时返回累计的权重增量;否则返回None- 返回类型:
Optional[Tensor]
- 抛出:
NotImplementedError -- 当
self.step_mode与突触层类型组合当前不受支持时抛出ValueError -- 当
self.step_mode不是's'或'm'时抛出
English
Perform one STDP update using the spike records currently buffered in the monitors.
When
on_grad=True, the function accumulates-delta_wintoself.synapse.weight.grad. Whenon_grad=False, it returns the accumulateddelta_wwithout writing gradients.- 参数:
- 返回:
The accumulated weight increment when
on_grad=False; otherwiseNone- 返回类型:
Optional[Tensor]
- 抛出:
NotImplementedError -- Raised when the current
step_mode/ synapse-type combination is unsupportedValueError -- Raised when
self.step_modeis neither's'nor'm'
- class spikingjelly.activation_based.learning.MSTDPLearner(step_mode, batch_size, synapse, sn, tau_pre, tau_post, f_pre=<function MSTDPLearner.<lambda>>, f_post=<function MSTDPLearner.<lambda>>)[源代码]#
基类:
MemoryModule
中文
reward-modulated STDP(mSTDP)学习器。
该学习器维护每个样本对应的 eligibility,并在
step()中结合外部奖励reward把 eligibility 转换为权重更新。- 参数:
English
Reward-modulated STDP (mSTDP) learner.
The learner maintains per-sample eligibility tensors and converts them into weight updates inside
step()using the external rewardreward.- 参数:
step_mode (str) --
's'for single-step mSTDPbatch_size (float) -- Batch size used when modulating eligibility with rewards
synapse (Union[Conv2d, Linear]) -- Synaptic layer updated by mSTDP
sn (spikingjelly.activation_based.neuron.BaseNode) -- Spiking neuron module that generates output spikes
tau_pre (float) -- Time constant of the pre trace
tau_post (float) -- Time constant of the post trace
f_pre (Callable) -- Weight modulation function for the pre branch
f_post (Callable) -- Weight modulation function for the post branch
- step(reward, on_grad=True, scale=1.0)[源代码]#
-
中文
使用外部奖励
reward对当前 eligibility 进行调制,并生成一次 mSTDP 权重更新。 若reward为张量,则在计算前将其从 autograd 计算图中分离, 因此写入的weight.grad或返回的权重增量不会连接到reward的计算图。- 参数:
- 返回:
当
on_grad=False时返回累计的权重增量;否则返回None- 返回类型:
Optional[Tensor]
- 抛出:
NotImplementedError -- 当前仅部分支持特定
step_mode与突触层组合ValueError -- 当
self.step_mode不合法时抛出
English
Modulate the current eligibility with the external reward
rewardand generate one mSTDP weight update. Tensor rewards are detached from the autograd graph before computation, so neither the writtenweight.gradnor the returned weight increment is connected to the reward graph.- 参数:
- 返回:
The accumulated weight increment when
on_grad=False; otherwiseNone- 返回类型:
Optional[Tensor]
- 抛出:
NotImplementedError -- Only some
step_mode/ synapse-type combinations are currently supportedValueError -- Raised when
self.step_modeis invalid
- class spikingjelly.activation_based.learning.MSTDPETLearner(step_mode, synapse, sn, tau_pre, tau_post, tau_trace, f_pre=<function MSTDPETLearner.<lambda>>, f_post=<function MSTDPETLearner.<lambda>>)[源代码]#
基类:
MemoryModule
中文
mSTDP-ET 学习器。
与
MSTDPLearner相比,该学习器额外维护随时间衰减的 eligibility tracetrace_e,并在step()中结合奖励生成最终权重更新。- 参数:
English
mSTDP-ET learner.
Compared with
MSTDPLearner, this learner additionally maintains a temporally decaying eligibility tracetrace_eand combines it with rewards insidestep()to produce the final weight update.- 参数:
step_mode (str) --
's'for single-step mSTDP-ETsynapse (Union[Conv2d, Linear]) -- Synaptic layer updated by mSTDP-ET
sn (spikingjelly.activation_based.neuron.BaseNode) -- Spiking neuron module that generates output spikes
tau_pre (float) -- Time constant of the pre trace
tau_post (float) -- Time constant of the post trace
tau_trace (float) -- Time constant of the eligibility trace
f_pre (Callable) -- Weight modulation function for the pre branch
f_post (Callable) -- Weight modulation function for the post branch
- step(reward, on_grad=True, scale=1.0)[源代码]#
-
中文
使用外部奖励
reward对 eligibility trace 进行调制,并生成一次 mSTDP-ET 权重更新。 若reward为张量,则在计算前将其从 autograd 计算图中分离, 因此写入的weight.grad或返回的权重增量不会连接到reward的计算图。- 参数:
- 返回:
当
on_grad=False时返回累计的权重增量;否则返回None- 返回类型:
Optional[Tensor]
- 抛出:
NotImplementedError -- 当前仅部分支持特定
step_mode与突触层组合ValueError -- 当
self.step_mode不合法时抛出
English
Modulate the eligibility trace with the external reward
rewardand generate one mSTDP-ET weight update. Tensor rewards are detached from the autograd graph before computation, so neither the writtenweight.gradnor the returned weight increment is connected to the reward graph.- 参数:
- 返回:
The accumulated weight increment when
on_grad=False; otherwiseNone- 返回类型:
Optional[Tensor]
- 抛出:
NotImplementedError -- Only some
step_mode/ synapse-type combinations are currently supportedValueError -- Raised when
self.step_modeis invalid