Dropout Variants#

SpikingJelly 提供了适用于 SNN 的 Dropout 实现,支持步进模式。


SpikingJelly provides dropout implementations suitable for SNNs with step mode support.

class spikingjelly.activation_based.layer.dropout.Dropout(p=0.5, step_mode='s')[源代码]#

基类:MemoryModule

API Language: 中文 | English


  • 中文

torch.nn.Dropout 的几乎相同。区别在于,在每一轮的仿真中,被设置成0的位置不会发生改变;直到下一轮运行,即网络调用reset()函数后,才会按照概率去重新决定,哪些位置被置0。

小技巧

这种Dropout最早由 Enabling Spike-based Backpropagation for Training Deep Neural Network Architectures 一文进行详细论述:

There is a subtle difference in the way dropout is applied in SNNs compared to ANNs. In ANNs, each epoch of training has several iterations of mini-batches. In each iteration, randomly selected units (with dropout ratio of \(p\)) are disconnected from the network while weighting by its posterior probability (\(1-p\)). However, in SNNs, each iteration has more than one forward propagation depending on the time length of the spike train. We back-propagate the output error and modify the network parameters only at the last time step. For dropout to be effective in our training method, it has to be ensured that the set of connected units within an iteration of mini-batch data is not changed, such that the neural network is constituted by the same random subset of units during each forward propagation within a single iteration. On the other hand, if the units are randomly connected at each time-step, the effect of dropout will be averaged out over the entire forward propagation time within an iteration. Then, the dropout effect would fade-out once the output error is propagated backward and the parameters are updated at the last time step. Therefore, we need to keep the set of randomly connected units for the entire time window within an iteration.

参数:
  • p (float) -- 每个元素被设置为0的概率

  • step_mode (str) -- 步进模式,可以为 's' (单步) 或 'm' (多步)


  • English

This layer is almost same with torch.nn.Dropout. The difference is that elements have been zeroed at first step during a simulation will always be zero. The indexes of zeroed elements will be update only after reset() has been called and a new simulation is started.

Tip

This kind of Dropout is firstly described in Enabling Spike-based Backpropagation for Training Deep Neural Network Architectures:

There is a subtle difference in the way dropout is applied in SNNs compared to ANNs. In ANNs, each epoch of training has several iterations of mini-batches. In each iteration, randomly selected units (with dropout ratio of \(p\)) are disconnected from the network while weighting by its posterior probability (\(1-p\)). However, in SNNs, each iteration has more than one forward propagation depending on the time length of the spike train. We back-propagate the output error and modify the network parameters only at the last time step. For dropout to be effective in our training method, it has to be ensured that the set of connected units within an iteration of mini-batch data is not changed, such that the neural network is constituted by the same random subset of units during each forward propagation within a single iteration. On the other hand, if the units are randomly connected at each time-step, the effect of dropout will be averaged out over the entire forward propagation time within an iteration. Then, the dropout effect would fade-out once the output error is propagated backward and the parameters are updated at the last time step. Therefore, we need to keep the set of randomly connected units for the entire time window within an iteration.

参数:
  • p (float) -- probability of an element to be zeroed

  • step_mode (str) -- the step mode, which can be s (single-step) or m (multi-step)

返回:

None

返回类型:

None

create_mask(x: Tensor)[源代码]#
single_step_forward(x: Tensor)[源代码]#
multi_step_forward(x_seq: Tensor)[源代码]#
class spikingjelly.activation_based.layer.dropout.Dropout2d(p=0.2, step_mode='s')[源代码]#

基类:Dropout

API Language: 中文 | English


  • 中文

torch.nn.Dropout2d 的几乎相同。区别在于,在每一轮的仿真中,被设置成0的位置不会发生改变;直到下一轮运行,即网络调用reset()函数后,才会按照概率去重新决定,哪些位置被置0。

关于SNN中Dropout的更多信息,参见 layer.Dropout

参数:
  • p (float) -- 每个元素被设置为0的概率

  • step_mode (str) -- 步进模式,可以为 's' (单步) 或 'm' (多步)


  • English

This layer is almost same with torch.nn.Dropout2d. The difference is that elements have been zeroed at first step during a simulation will always be zero. The indexes of zeroed elements will be update only after reset() has been called and a new simulation is started.

For more information about Dropout in SNN, refer to layer.Dropout.

参数:
  • p (float) -- probability of an element to be zeroed

  • step_mode (str) -- the step mode, which can be s (single-step) or m (multi-step)

返回:

None

返回类型:

None

create_mask(x: Tensor)[源代码]#
class spikingjelly.activation_based.layer.dropout.DropConnectLinear(in_features: int, out_features: int, bias: bool = True, p: float = 0.5, samples_num: int = 1024, invariant: bool = False, activation: Module | None = ReLU(), step_mode='s')[源代码]#

基类:MemoryModule

API Language: 中文 | English


  • 中文

DropConnect,由 Regularization of Neural Networks using DropConnect 一文提出。DropConnect与Dropout非常类似,区别在于DropConnect是以概率 p 断开连接,而Dropout是将输入以概率置0。

备注

在使用DropConnect进行推理时,输出的tensor中的每个元素,都是先从高斯分布中采样,通过激活层激活,再在采样数量上进行平均得到的。 详细的流程可以在 Regularization of Neural Networks using DropConnect 一文中的 Algorithm 2 找到。激活层 activation 在中间的步骤起作用,因此我们将其作为模块的成员。

参数:
  • in_features (int) -- 每个输入样本的特征数

  • out_features (int) -- 每个输出样本的特征数

  • bias (bool) -- 若为 False,则本层不会有可学习的偏置项。默认为 True

  • p (float) -- 每个连接被断开的概率。默认为0.5

  • samples_num (int) -- 在推理时,从高斯分布中采样的数据数量。默认为1024

  • invariant (bool) -- 若为 True,线性层会在第一次执行前向传播时被按概率断开,断开后的线性层会保持不变,直到 reset() 函数 被调用,线性层恢复为完全连接的状态。完全连接的线性层,调用 reset() 函数后的第一次前向传播时被重新按概率断开。 若为 False,在每一次前向传播时线性层都会被重新完全连接再按概率断开。 阅读 layer.Dropout 以 获得更多关于此参数的信息。默认为 False

  • activation (Optional[torch.nn.Module]) -- 在线性层后的激活层

  • step_mode (str) -- 步进模式,可以为 's' (单步) 或 'm' (多步)


  • English

DropConnect, which is proposed by Regularization of Neural Networks using DropConnect, is similar with Dropout but drop connections of a linear layer rather than the elements of the input tensor with probability p.

Note

When inference with DropConnect, every elements of the output tensor are sampled from a Gaussian distribution, activated by the activation layer and averaged over the sample number samples_num. See Algorithm 2 in Regularization of Neural Networks using DropConnect for more details. Note that activation is an intermediate process. This is the reason why we include activation as a member variable of this module.

参数:
  • in_features (int) -- size of each input sample

  • out_features (int) -- size of each output sample

  • bias (bool) -- If set to False, the layer will not learn an additive bias. Default: True

  • p (float) -- probability of an connection to be zeroed. Default: 0.5

  • samples_num (int) -- number of samples drawn from the Gaussian during inference. Default: 1024

  • invariant (bool) -- If set to True, the connections will be dropped at the first time of forward and the dropped connections will remain unchanged until reset() is called and the connections recovery to fully-connected status. Then the connections will be re-dropped at the first time of forward after reset(). If set to False, the connections will be re-dropped at every forward. See layer.Dropout for more information to understand this parameter. Default: False

  • activation (Optional[nn.Module]) -- the activation layer after the linear layer

  • step_mode (str) -- the step mode, which can be s (single-step) or m (multi-step)

返回:

None

返回类型:

None

reset_parameters() None[源代码]#

API Language: 中文 | English


  • 中文

返回:

None

返回类型:

None

初始化模型中的可学习参数。


  • English

返回:

None

返回类型:

None

Initialize the learnable parameters of this module.

reset()[源代码]#

API Language: 中文 | English


  • 中文

将线性层重置为完全连接的状态,若 self.activation 也是一个有状态的层,则将其也重置。

返回:

None

返回类型:

None


  • English

Reset the linear layer to fully-connected status. If self.activation is also stateful, this function will also reset it.

返回:

None

返回类型:

None

drop(batch_size: int)[源代码]#
single_step_forward(input: Tensor) Tensor[源代码]#