spikingjelly.activation_based.learning module#

spikingjelly.activation_based.learning.stdp_linear_single_step(fc: ~torch.nn.modules.linear.Linear, in_spike: ~torch.Tensor, out_spike: ~torch.Tensor, trace_pre: float | ~torch.Tensor | None, trace_post: float | ~torch.Tensor | None, tau_pre: float, tau_post: float, f_pre: ~typing.Callable = <function <lambda>>, f_post: ~typing.Callable = <function <lambda>>)[源代码]#

API Language: 中文 | English


  • 中文

对单步脉冲输入执行全连接层的 STDP 更新,返回更新后的 pre/post trace 与权重增量。

参数:
  • fc (torch.nn.Linear) -- 需要执行 STDP 的全连接层

  • in_spike (torch.Tensor) -- 输入脉冲,shape = [batch_size, in_features]

  • out_spike (torch.Tensor) -- 输出脉冲,shape = [batch_size, out_features]

  • trace_pre (Union[float, torch.Tensor, None]) -- pre-synaptic trace。若为 None,则使用标量 0.0 初始化

  • trace_post (Union[float, torch.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 相同

返回类型:

tuple[Union[float, torch.Tensor], Union[float, torch.Tensor], torch.Tensor]


  • 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 (torch.nn.Linear) -- Linear layer updated by STDP

  • in_spike (torch.Tensor) -- Input spikes with shape = [batch_size, in_features]

  • out_spike (torch.Tensor) -- Output spikes with shape = [batch_size, out_features]

  • trace_pre (Union[float, torch.Tensor, None]) -- Pre-synaptic trace. If None, it is initialized as scalar 0.0

  • trace_post (Union[float, torch.Tensor, None]) -- Post-synaptic trace. If None, it is initialized as scalar 0.0

  • tau_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), where delta_w has the same shape as fc.weight

返回类型:

tuple[Union[float, torch.Tensor], Union[float, torch.Tensor], torch.Tensor]

spikingjelly.activation_based.learning.mstdp_linear_single_step(fc: ~torch.nn.modules.linear.Linear, in_spike: ~torch.Tensor, out_spike: ~torch.Tensor, trace_pre: float | ~torch.Tensor | None, trace_post: float | ~torch.Tensor | None, tau_pre: float, tau_post: float, f_pre: ~typing.Callable = <function <lambda>>, f_post: ~typing.Callable = <function <lambda>>)[源代码]#

API Language: 中文 | English


  • 中文

对单步脉冲输入执行全连接层的 reward-modulated STDP eligibility 计算。

参数:
  • fc (torch.nn.Linear) -- 需要执行 mSTDP 的全连接层

  • in_spike (torch.Tensor) -- 输入脉冲,shape = [batch_size, in_features]

  • out_spike (torch.Tensor) -- 输出脉冲,shape = [batch_size, out_features]

  • trace_pre (Union[float, torch.Tensor, None]) -- pre-synaptic trace。若为 None,则使用标量 0.0 初始化

  • trace_post (Union[float, torch.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]

返回类型:

tuple[Union[float, torch.Tensor], Union[float, torch.Tensor], torch.Tensor]


  • English

Compute the reward-modulated STDP eligibility tensor for a linear layer under single-step spike inputs.

参数:
  • fc (torch.nn.Linear) -- Linear layer updated by mSTDP

  • in_spike (torch.Tensor) -- Input spikes with shape = [batch_size, in_features]

  • out_spike (torch.Tensor) -- Output spikes with shape = [batch_size, out_features]

  • trace_pre (Union[float, torch.Tensor, None]) -- Pre-synaptic trace. If None, it is initialized as scalar 0.0

  • trace_post (Union[float, torch.Tensor, None]) -- Post-synaptic trace. If None, it is initialized as scalar 0.0

  • 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, eligibility), where eligibility has shape [batch_size, out_features, in_features]

返回类型:

tuple[Union[float, torch.Tensor], Union[float, torch.Tensor], torch.Tensor]

spikingjelly.activation_based.learning.mstdpet_linear_single_step(fc: ~torch.nn.modules.linear.Linear, in_spike: ~torch.Tensor, out_spike: ~torch.Tensor, trace_pre: float | ~torch.Tensor | None, trace_post: float | ~torch.Tensor | None, tau_pre: float, tau_post: float, tau_trace: float, f_pre: ~typing.Callable = <function <lambda>>, f_post: ~typing.Callable = <function <lambda>>)[源代码]#

API Language: 中文 | English


  • 中文

对单步脉冲输入执行全连接层的 mSTDP-ET eligibility 计算。

参数:
  • fc (torch.nn.Linear) -- 需要执行 mSTDP-ET 的全连接层

  • in_spike (torch.Tensor) -- 输入脉冲,shape = [in_features]

  • out_spike (torch.Tensor) -- 输出脉冲,shape = [out_features]

  • trace_pre (Union[float, torch.Tensor, None]) -- pre-synaptic trace。若为 None,则使用标量 0.0 初始化

  • trace_post (Union[float, torch.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 相同

返回类型:

tuple[Union[float, torch.Tensor], Union[float, torch.Tensor], torch.Tensor]


  • English

Compute the mSTDP-ET eligibility update for a linear layer under single-step spike inputs.

参数:
  • fc (torch.nn.Linear) -- Linear layer updated by mSTDP-ET

  • in_spike (torch.Tensor) -- Input spikes with shape = [in_features]

  • out_spike (torch.Tensor) -- Output spikes with shape = [out_features]

  • trace_pre (Union[float, torch.Tensor, None]) -- Pre-synaptic trace. If None, it is initialized as scalar 0.0

  • trace_post (Union[float, torch.Tensor, None]) -- Post-synaptic trace. If None, it is initialized as scalar 0.0

  • 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

返回:

(trace_pre, trace_post, eligibility), where eligibility has the same shape as fc.weight

返回类型:

tuple[Union[float, torch.Tensor], Union[float, torch.Tensor], torch.Tensor]

spikingjelly.activation_based.learning.stdp_conv2d_single_step(conv: ~torch.nn.modules.conv.Conv2d, in_spike: ~torch.Tensor, out_spike: ~torch.Tensor, trace_pre: ~torch.Tensor | None, trace_post: ~torch.Tensor | None, tau_pre: float, tau_post: float, f_pre: ~typing.Callable = <function <lambda>>, f_post: ~typing.Callable = <function <lambda>>)[源代码]#

API Language: 中文 | English


  • 中文

对单步脉冲输入执行二维卷积层的 STDP 更新。

当前仅支持 dilation == (1, 1)groups == 1 的卷积。

参数:
  • conv (torch.nn.Conv2d) -- 需要执行 STDP 的二维卷积层

  • in_spike (torch.Tensor) -- 输入脉冲,shape = [batch_size, C_in, H_in, W_in]

  • out_spike (torch.Tensor) -- 输出脉冲,shape = [batch_size, C_out, H_out, W_out]

  • trace_pre (Union[torch.Tensor, None]) -- pre-synaptic trace。若为 None,则初始化为与 in_spike 同形状零张量

  • trace_post (Union[torch.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 相同

返回类型:

tuple[torch.Tensor, torch.Tensor, torch.Tensor]

抛出:

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) and groups == 1 are currently supported.

参数:
  • conv (torch.nn.Conv2d) -- 2D convolution layer updated by STDP

  • in_spike (torch.Tensor) -- Input spikes with shape = [batch_size, C_in, H_in, W_in]

  • out_spike (torch.Tensor) -- Output spikes with shape = [batch_size, C_out, H_out, W_out]

  • trace_pre (Union[torch.Tensor, None]) -- Pre-synaptic trace. If None, initialized as zeros with the same shape as in_spike

  • trace_post (Union[torch.Tensor, None]) -- Post-synaptic trace. If None, initialized as zeros with the same shape as out_spike

  • 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), where delta_w has the same shape as conv.weight

返回类型:

tuple[torch.Tensor, torch.Tensor, torch.Tensor]

抛出:

NotImplementedError -- Raised when conv.dilation != (1, 1) or conv.groups != 1

spikingjelly.activation_based.learning.stdp_conv1d_single_step(conv: ~torch.nn.modules.conv.Conv1d, in_spike: ~torch.Tensor, out_spike: ~torch.Tensor, trace_pre: ~torch.Tensor | None, trace_post: ~torch.Tensor | None, tau_pre: float, tau_post: float, f_pre: ~typing.Callable = <function <lambda>>, f_post: ~typing.Callable = <function <lambda>>)[源代码]#

API Language: 中文 | English


  • 中文

对单步脉冲输入执行一维卷积层的 STDP 更新。

当前仅支持 dilation == (1,)groups == 1 的卷积。

参数:
  • conv (torch.nn.Conv1d) -- 需要执行 STDP 的一维卷积层

  • in_spike (torch.Tensor) -- 输入脉冲,shape = [batch_size, C_in, L_in]

  • out_spike (torch.Tensor) -- 输出脉冲,shape = [batch_size, C_out, L_out]

  • trace_pre (Union[torch.Tensor, None]) -- pre-synaptic trace。若为 None,则初始化为与 in_spike 同形状零张量

  • trace_post (Union[torch.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 相同

返回类型:

tuple[torch.Tensor, torch.Tensor, torch.Tensor]

抛出:

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,) and groups == 1 are currently supported.

参数:
  • conv (torch.nn.Conv1d) -- 1D convolution layer updated by STDP

  • in_spike (torch.Tensor) -- Input spikes with shape = [batch_size, C_in, L_in]

  • out_spike (torch.Tensor) -- Output spikes with shape = [batch_size, C_out, L_out]

  • trace_pre (Union[torch.Tensor, None]) -- Pre-synaptic trace. If None, initialized as zeros with the same shape as in_spike

  • trace_post (Union[torch.Tensor, None]) -- Post-synaptic trace. If None, initialized as zeros with the same shape as out_spike

  • 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), where delta_w has the same shape as conv.weight

返回类型:

tuple[torch.Tensor, torch.Tensor, torch.Tensor]

抛出:

NotImplementedError -- Raised when conv.dilation != (1,) or conv.groups != 1

spikingjelly.activation_based.learning.stdp_multi_step(layer: ~torch.nn.modules.linear.Linear | ~torch.nn.modules.conv.Conv1d | ~torch.nn.modules.conv.Conv2d, in_spike: ~torch.Tensor, out_spike: ~torch.Tensor, trace_pre: float | ~torch.Tensor | None, trace_post: float | ~torch.Tensor | None, tau_pre: float, tau_post: float, f_pre: ~typing.Callable = <function <lambda>>, f_post: ~typing.Callable = <function <lambda>>)[源代码]#

API Language: 中文 | English


  • 中文

对线性层、一维卷积层或二维卷积层执行多步 STDP 更新。

该函数沿时间维遍历 in_spikeout_spike,并在每个时间步调用对应的 单步 STDP 规则,最终累加得到整段序列的权重增量。

参数:
  • layer (Union[torch.nn.Linear, torch.nn.Conv1d, torch.nn.Conv2d]) -- 支持的突触层,目前为 nn.Linearnn.Conv1dnn.Conv2d

  • in_spike (torch.Tensor) -- 输入脉冲序列,时间维位于第 0 维

  • out_spike (torch.Tensor) -- 输出脉冲序列,时间维位于第 0 维

  • trace_pre (Union[float, torch.Tensor, None]) -- pre-synaptic trace 的初始值

  • trace_post (Union[float, torch.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 相同

返回类型:

tuple[Union[float, torch.Tensor], Union[float, torch.Tensor], torch.Tensor]


  • 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_spike and out_spike, applies the matching single-step STDP rule at each time step, and accumulates the weight increment over the whole sequence.

参数:
  • layer (Union[torch.nn.Linear, torch.nn.Conv1d, torch.nn.Conv2d]) -- Supported synaptic layer. Currently nn.Linear, nn.Conv1d or nn.Conv2d

  • in_spike (torch.Tensor) -- Input spike sequence with the time axis at dimension 0

  • out_spike (torch.Tensor) -- Output spike sequence with the time axis at dimension 0

  • trace_pre (Union[float, torch.Tensor, None]) -- Initial value of the pre-synaptic trace

  • trace_post (Union[float, torch.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), where delta_w has the same shape as layer.weight

返回类型:

tuple[Union[float, torch.Tensor], Union[float, torch.Tensor], torch.Tensor]

class spikingjelly.activation_based.learning.STDPLearner(step_mode: str, synapse: ~torch.nn.modules.conv.Conv2d | ~torch.nn.modules.linear.Linear, sn: ~spikingjelly.activation_based.neuron.base_node.BaseNode, tau_pre: float, tau_post: float, f_pre: ~typing.Callable = <function STDPLearner.<lambda>>, f_post: ~typing.Callable = <function STDPLearner.<lambda>>)[源代码]#

基类:MemoryModule

API Language: 中文 | English


  • 中文

基于监视器的 STDP 学习器。

该学习器通过 InputMonitorOutputMonitor 自动记录突触层输入脉冲与神经元输出脉冲,并在调用 step() 时根据 step_mode 选择单步或多步 STDP 规则。

参数:
  • step_mode (str) -- 's' 表示单步 STDP,'m' 表示多步 STDP

  • synapse (Union[torch.nn.Conv2d, torch.nn.Linear]) -- 需要执行 STDP 的突触层,目前支持 nn.Linearnn.Conv1dnn.Conv2d

  • sn (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 InputMonitor and OutputMonitor. When step() is called, it selects the single-step or multi-step STDP rule according to step_mode.

参数:
  • step_mode (str) -- 's' for single-step STDP and 'm' for multi-step STDP

  • synapse (Union[torch.nn.Conv2d, torch.nn.Linear]) -- Synaptic layer updated by STDP. Currently supports nn.Linear, nn.Conv1d, and nn.Conv2d

  • 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

reset()[源代码]#

API Language: 中文 | English


  • 中文

重置学习器内部状态,并清空输入/输出脉冲监视器中已记录的数据。

返回:

None

返回类型:

None


  • English

Reset the learner state and clear all recorded data in the input/output spike monitors.

返回:

None

返回类型:

None

disable()[源代码]#

API Language: 中文 | English


  • 中文

禁用输入脉冲与输出脉冲监视器,使其停止记录新数据。

返回:

None

返回类型:

None


  • English

Disable the input and output spike monitors so they stop recording new data.

返回:

None

返回类型:

None

enable()[源代码]#

API Language: 中文 | English


  • 中文

启用输入脉冲与输出脉冲监视器,使其恢复记录。

返回:

None

返回类型:

None


  • English

Enable the input and output spike monitors so they resume recording.

返回:

None

返回类型:

None

step(on_grad: bool = True, scale: float = 1.0)[源代码]#

API Language: 中文 | English


  • 中文

使用当前监视器中缓存的脉冲记录执行一次 STDP 权重更新。

on_grad=True 时,函数会把 -delta_w 累加到 self.synapse.weight.grad; 当 on_grad=False 时,返回累计的 delta_w 而不写入梯度。

参数:
  • on_grad (bool) -- 是否将权重增量写入 self.synapse.weight.grad

  • scale (float) -- 对累计权重增量施加的缩放因子

返回:

on_grad=False 时返回累计的权重增量;否则返回 None

返回类型:

Optional[torch.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_w into self.synapse.weight.grad. When on_grad=False, it returns the accumulated delta_w without writing gradients.

参数:
  • on_grad (bool) -- Whether to write the weight increment into self.synapse.weight.grad

  • scale (float) -- Scaling factor applied to the accumulated weight increment

返回:

The accumulated weight increment when on_grad=False; otherwise None

返回类型:

Optional[torch.Tensor]

抛出:
  • NotImplementedError -- Raised when the current step_mode / synapse-type combination is unsupported

  • ValueError -- Raised when self.step_mode is neither 's' nor 'm'

class spikingjelly.activation_based.learning.MSTDPLearner(step_mode: str, batch_size: float, synapse: ~torch.nn.modules.conv.Conv2d | ~torch.nn.modules.linear.Linear, sn: ~spikingjelly.activation_based.neuron.base_node.BaseNode, tau_pre: float, tau_post: float, f_pre: ~typing.Callable = <function MSTDPLearner.<lambda>>, f_post: ~typing.Callable = <function MSTDPLearner.<lambda>>)[源代码]#

基类:MemoryModule

API Language: 中文 | English


  • 中文

reward-modulated STDP(mSTDP)学习器。

该学习器维护每个样本对应的 eligibility,并在 step() 中结合外部奖励 reward 把 eligibility 转换为权重更新。

参数:
  • step_mode (str) -- 's' 表示单步 mSTDP

  • batch_size (float) -- 每次奖励调制时使用的 batch 大小

  • synapse (Union[torch.nn.Conv2d, torch.nn.Linear]) -- 需要执行 mSTDP 的突触层

  • sn (spikingjelly.activation_based.neuron.BaseNode) -- 产生输出脉冲的脉冲神经元模块

  • tau_pre (float) -- pre trace 的时间常数

  • tau_post (float) -- post trace 的时间常数

  • f_pre (Callable) -- pre 分支的权重调制函数

  • f_post (Callable) -- post 分支的权重调制函数


  • English

Reward-modulated STDP (mSTDP) learner.

The learner maintains per-sample eligibility tensors and converts them into weight updates inside step() using the external reward reward.

参数:
  • step_mode (str) -- 's' for single-step mSTDP

  • batch_size (float) -- Batch size used when modulating eligibility with rewards

  • synapse (Union[torch.nn.Conv2d, torch.nn.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

reset()[源代码]#

API Language: 中文 | English


  • 中文

重置学习器内部状态,并清空监视器缓存。

返回:

None

返回类型:

None


  • English

Reset the learner state and clear the monitor buffers.

返回:

None

返回类型:

None

disable()[源代码]#

API Language: 中文 | English


  • 中文

禁用输入/输出监视器。

返回:

None

返回类型:

None


  • English

Disable the input/output monitors.

返回:

None

返回类型:

None

enable()[源代码]#

API Language: 中文 | English


  • 中文

启用输入/输出监视器。

返回:

None

返回类型:

None


  • English

Enable the input/output monitors.

返回:

None

返回类型:

None

step(reward, on_grad: bool = True, scale: float = 1.0)[源代码]#

API Language: 中文 | English


  • 中文

使用外部奖励 reward 对当前 eligibility 进行调制,并生成一次 mSTDP 权重更新。

参数:
  • reward (torch.Tensor) -- 每个样本对应的奖励,通常 shape = [batch_size]

  • on_grad (bool) -- 是否将结果写入 self.synapse.weight.grad

  • scale (float) -- 权重增量的缩放因子

返回:

on_grad=False 时返回累计的权重增量;否则返回 None

返回类型:

Optional[torch.Tensor]

抛出:

  • English

Modulate the current eligibility with the external reward reward and generate one mSTDP weight update.

参数:
  • reward (torch.Tensor) -- Reward for each sample, typically with shape = [batch_size]

  • on_grad (bool) -- Whether to write the result into self.synapse.weight.grad

  • scale (float) -- Scaling factor applied to the weight increment

返回:

The accumulated weight increment when on_grad=False; otherwise None

返回类型:

Optional[torch.Tensor]

抛出:
  • NotImplementedError -- Only some step_mode / synapse-type combinations are currently supported

  • ValueError -- Raised when self.step_mode is invalid

class spikingjelly.activation_based.learning.MSTDPETLearner(step_mode: str, synapse: ~torch.nn.modules.conv.Conv2d | ~torch.nn.modules.linear.Linear, sn: ~spikingjelly.activation_based.neuron.base_node.BaseNode, tau_pre: float, tau_post: float, tau_trace: float, f_pre: ~typing.Callable = <function MSTDPETLearner.<lambda>>, f_post: ~typing.Callable = <function MSTDPETLearner.<lambda>>)[源代码]#

基类:MemoryModule

API Language: 中文 | English


  • 中文

mSTDP-ET 学习器。

MSTDPLearner 相比,该学习器额外维护随时间衰减的 eligibility trace trace_e,并在 step() 中结合奖励生成最终权重更新。

参数:
  • step_mode (str) -- 's' 表示单步 mSTDP-ET

  • synapse (Union[torch.nn.Conv2d, torch.nn.Linear]) -- 需要执行 mSTDP-ET 的突触层

  • sn (spikingjelly.activation_based.neuron.BaseNode) -- 产生输出脉冲的脉冲神经元模块

  • tau_pre (float) -- pre trace 的时间常数

  • tau_post (float) -- post trace 的时间常数

  • tau_trace (float) -- eligibility trace 的时间常数

  • f_pre (Callable) -- pre 分支的权重调制函数

  • f_post (Callable) -- post 分支的权重调制函数


  • English

mSTDP-ET learner.

Compared with MSTDPLearner, this learner additionally maintains a temporally decaying eligibility trace trace_e and combines it with rewards inside step() to produce the final weight update.

参数:
  • step_mode (str) -- 's' for single-step mSTDP-ET

  • synapse (Union[torch.nn.Conv2d, torch.nn.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

reset()[源代码]#

API Language: 中文 | English


  • 中文

重置学习器内部状态,并清空监视器缓存。

返回:

None

返回类型:

None


  • English

Reset the learner state and clear the monitor buffers.

返回:

None

返回类型:

None

disable()[源代码]#

API Language: 中文 | English


  • 中文

禁用输入/输出监视器。

返回:

None

返回类型:

None


  • English

Disable the input/output monitors.

返回:

None

返回类型:

None

enable()[源代码]#

API Language: 中文 | English


  • 中文

启用输入/输出监视器。

返回:

None

返回类型:

None


  • English

Enable the input/output monitors.

返回:

None

返回类型:

None

step(reward, on_grad: bool = True, scale: float = 1.0)[源代码]#

API Language: 中文 | English


  • 中文

使用外部奖励 reward 对 eligibility trace 进行调制,并生成一次 mSTDP-ET 权重更新。

参数:
  • reward (torch.Tensor) -- 奖励信号,通常为标量或 shape = [batch_size] 的张量

  • on_grad (bool) -- 是否将结果写入 self.synapse.weight.grad

  • scale (float) -- 权重增量的缩放因子

返回:

on_grad=False 时返回累计的权重增量;否则返回 None

返回类型:

Optional[torch.Tensor]

抛出:

  • English

Modulate the eligibility trace with the external reward reward and generate one mSTDP-ET weight update.

参数:
  • reward (torch.Tensor) -- Reward signal, typically a scalar or a tensor with shape = [batch_size]

  • on_grad (bool) -- Whether to write the result into self.synapse.weight.grad

  • scale (float) -- Scaling factor applied to the weight increment

返回:

The accumulated weight increment when on_grad=False; otherwise None

返回类型:

Optional[torch.Tensor]

抛出:
  • NotImplementedError -- Only some step_mode / synapse-type combinations are currently supported

  • ValueError -- Raised when self.step_mode is invalid