spikingjelly.activation_based.surrogate module#
- spikingjelly.activation_based.surrogate.heaviside(x: Tensor)[源代码]#
-
中文
heaviside阶跃函数,定义为
\[\begin{split}g(x) = \begin{cases} 1, & x \geq 0 \\ 0, & x < 0 \\ \end{cases}\end{split}\]阅读 HeavisideStepFunction 以获得更多信息。
- 参数:
x (torch.Tensor) -- 输入tensor
- 返回:
输出tensor
- 返回类型:
English
The heaviside function, which is defined by
\[\begin{split}g(x) = \begin{cases} 1, & x \geq 0 \\ 0, & x < 0 \\ \end{cases}\end{split}\]For more information, see HeavisideStepFunction.
- 参数:
x (torch.Tensor) -- the input tensor
- 返回:
the output tensor
- 返回类型:
- spikingjelly.activation_based.surrogate.check_manual_grad(primitive_function, spiking_function, *args, **kwargs)[源代码]#
-
中文
梯度替代函数的反向传播一般是手写的,可以用此函数去检查手写梯度是否正确。
此函数检查梯度替代函数spiking_function的反向传播,与原函数primitive_function的反向传播结果是否一致。 "一致"被定义为,两者的误差不超过eps。
- 参数:
primitive_function (callable) -- 梯度替代函数的原函数
spiking_function (callable) -- 梯度替代函数
English
The manual gradient of surrogate gradient functions is usually written by hand, and this function can be used to check if the manual gradient is correct.
This function checks if the backward pass of surrogate function spiking_function is consistent with the backward pass of the primitive function primitive_function. "Consistency" is defined as the error between the two not exceeding eps.
- 参数:
primitive_function (callable) -- the primitive function of surrogate gradient
spiking_function (callable) -- the surrogate function
代码示例 | Example
def s2nn_apply(x, alpha, beta): return surrogate.s2nn.apply(x, alpha, beta) surrogate.check_manual_grad( surrogate.S2NN.primitive_function, s2nn_apply, alpha=4.0, beta=1.0 )
- 返回:
无返回值,直接打印对比结果
- 返回类型:
None
- spikingjelly.activation_based.surrogate.check_cuda_grad(neu, surrogate_function, device, *args, **kwargs)[源代码]#
-
中文
检查CUDA(CuPy)后端的梯度是否正确。将CuPy后端的梯度与PyTorch后端的梯度进行比较。
- 参数:
- 返回:
无返回值,直接打印对比结果
- 返回类型:
None
Example
check_cuda_grad( neuron.IFNode, surrogate.S2NN, device="cuda:1", alpha=4.0, beta=1.0 )
English
Check whether the CUDA (CuPy) backend gradient is correct, by comparing the gradient of CuPy backend and the PyTorch backend.
- 参数:
- 返回:
no return value, directly prints comparison results
- 返回类型:
None
Example
check_cuda_grad( neuron.IFNode, surrogate.S2NN, device="cuda:1", alpha=4.0, beta=1.0 )
- spikingjelly.activation_based.surrogate.plot_surrogate_function(surrogate_function)[源代码]#
-
中文
绘制替代函数的原函数和梯度图像。同时绘制Heaviside阶跃函数作为参考。
- 参数:
surrogate_function (SurrogateFunctionBase) -- 替代函数模块的实例
- 返回:
无返回值,直接保存并显示图像
- 返回类型:
None
English
Plot the primitive function and gradient of the surrogate function. Also plots the Heaviside step function as a reference.
- 参数:
surrogate_function (SurrogateFunctionBase) -- an instance of surrogate function module
- 返回:
no return value, directly saves and shows the figure
- 返回类型:
None
- class spikingjelly.activation_based.surrogate.SurrogateFunctionBase(spiking=True, **kwargs)[源代码]#
基类:
Module
中文
所有替代函数模块的基类。提供脉冲发放模式(spiking mode)和常规模式之间的切换, 以及CUDA代码生成的基础框架。
- 参数:
spiking (bool) -- 是否输出脉冲。默认为
True,在前向传播时使用heaviside而在反向传播使用替代梯度。 若为False则不使用替代梯度,前向传播时使用原函数。- 返回:
SurrogateFunctionBase实例- 返回类型:
English
Base class for all surrogate function modules. Provides switching between spiking mode and regular mode, as well as the basic framework for CUDA code generation.
- 参数:
spiking (bool) -- whether to output spikes. Default is
Truewhich means usingheavisidein forward propagation and using surrogate gradient in backward propagation. IfFalse, no surrogate gradient is used, and the primitive function is used in forward propagation.- 返回:
a
SurrogateFunctionBaseinstance- 返回类型:
- class spikingjelly.activation_based.surrogate.PiecewiseQuadratic(alpha=1.0, spiking=True)[源代码]#
-
中文
反向传播时使用分段二次函数的梯度(三角形函数)的脉冲发放函数。反向传播为
\[\begin{split}g'(x) = \begin{cases} 0, & |x| > \frac{1}{\alpha} \\ -\alpha^2|x|+\alpha, & |x| \leq \frac{1}{\alpha} \end{cases}\end{split}\]对应的原函数为
\[\begin{split}g(x) = \begin{cases} 0, & x < -\frac{1}{\alpha} \\ -\frac{1}{2}\alpha^2|x|x + \alpha x + \frac{1}{2}, & |x| \leq \frac{1}{\alpha} \\ 1, & x > \frac{1}{\alpha} \\ \end{cases}\end{split}\]该函数在文章 [2] [4] [7] [11] [13] 中使用。
- 参数:
English
The piecewise quadratic surrogate spiking function. The gradient is defined by
\[\begin{split}g'(x) = \begin{cases} 0, & |x| > \frac{1}{\alpha} \\ -\alpha^2|x|+\alpha, & |x| \leq \frac{1}{\alpha} \end{cases}\end{split}\]The primitive function is defined by
\[\begin{split}g(x) = \begin{cases} 0, & x < -\frac{1}{\alpha} \\ -\frac{1}{2}\alpha^2|x|x + \alpha x + \frac{1}{2}, & |x| \leq \frac{1}{\alpha} \\ 1, & x > \frac{1}{\alpha} \\ \end{cases}\end{split}\]The function is used in [2] [4] [7] [11] [13].
- 参数:
alpha (float) -- parameter to control smoothness of gradient
spiking (bool) -- whether output spikes. The default is
Truewhich means that usingheavisidein forward propagation and using surrogate gradient in backward propagation. IfFalse, in forward propagation, using the primitive function of the surrogate gradient function used in backward propagation
- 返回:
heaviside(x)或原函数的结果,形状与x相同- 返回类型:
- class spikingjelly.activation_based.surrogate.PiecewiseExp(alpha=1.0, spiking=True)[源代码]#
-
中文
反向传播时使用分段指数函数的梯度的脉冲发放函数。反向传播为
\[g'(x) = \frac{\alpha}{2}e^{-\alpha |x|}\]对应的原函数为
\[\begin{split}g(x) = \begin{cases} \frac{1}{2}e^{\alpha x}, & x < 0 \\ 1 - \frac{1}{2}e^{-\alpha x}, & x \geq 0 \end{cases}\end{split}\]- 参数:
English
The piecewise exponential surrogate spiking function. The gradient is defined by
\[g'(x) = \frac{\alpha}{2}e^{-\alpha |x|}\]The primitive function is defined by
\[\begin{split}g(x) = \begin{cases} \frac{1}{2}e^{\alpha x}, & x < 0 \\ 1 - \frac{1}{2}e^{-\alpha x}, & x \geq 0 \end{cases}\end{split}\]The function is used in [6] [11] .
- 参数:
alpha (float) -- parameter to control smoothness of gradient
spiking (bool) -- whether output spikes. The default is
Truewhich means that usingheavisidein forward propagation and using surrogate gradient in backward propagation. IfFalse, in forward propagation, using the primitive function of the surrogate gradient function used in backward propagation
- 返回:
heaviside(x)或原函数的结果,形状与x相同- 返回类型:
- class spikingjelly.activation_based.surrogate.Sigmoid(alpha=4.0, spiking=True)[源代码]#
-
中文
反向传播时使用sigmoid的梯度的脉冲发放函数。反向传播为
\[g'(x) = \alpha * (1 - \mathrm{sigmoid} (\alpha x)) \mathrm{sigmoid} (\alpha x)\]对应的原函数为
\[g(x) = \mathrm{sigmoid}(\alpha x) = \frac{1}{1+e^{-\alpha x}}\]该函数在文章 [4] [12] [14] [15] 中使用。
- 参数:
English
The sigmoid surrogate spiking function. The gradient is defined by
\[g'(x) = \alpha * (1 - \mathrm{sigmoid} (\alpha x)) \mathrm{sigmoid} (\alpha x)\]The primitive function is defined by
\[g(x) = \mathrm{sigmoid}(\alpha x) = \frac{1}{1+e^{-\alpha x}}\]The function is used in [4] [12] [14] [15] .
- 参数:
alpha (float) -- parameter to control smoothness of gradient
spiking (bool) -- whether output spikes. The default is
Truewhich means that usingheavisidein forward propagation and using surrogate gradient in backward propagation. IfFalse, in forward propagation, using the primitive function of the surrogate gradient function used in backward propagation
- 返回:
heaviside(x)或原函数的结果,形状与x相同- 返回类型:
- class spikingjelly.activation_based.surrogate.SoftSign(alpha=2.0, spiking=True)[源代码]#
-
中文
反向传播时使用soft sign的梯度的脉冲发放函数。反向传播为
\[g'(x) = \frac{\alpha}{2(1 + |\alpha x|)^{2}} = \frac{1}{2\alpha(\frac{1}{\alpha} + |x|)^{2}}\]对应的原函数为
\[g(x) = \frac{1}{2} (\frac{\alpha x}{1 + |\alpha x|} + 1) = \frac{1}{2} (\frac{x}{\frac{1}{\alpha} + |x|} + 1)\]- 参数:
English
The soft sign surrogate spiking function. The gradient is defined by
\[g'(x) = \frac{\alpha}{2(1 + |\alpha x|)^{2}}\]The primitive function is defined by
\[g(x) = \frac{1}{2} (\frac{\alpha x}{1 + |\alpha x|} + 1)\]The function is used in [8] [11].
- 参数:
alpha (float) -- parameter to control smoothness of gradient
spiking (bool) -- whether output spikes. The default is
Truewhich means that usingheavisidein forward propagation and using surrogate gradient in backward propagation. IfFalse, in forward propagation, using the primitive function of the surrogate gradient function used in backward propagation
- 返回:
heaviside(x)或原函数的结果,形状与x相同- 返回类型:
- class spikingjelly.activation_based.surrogate.SuperSpike(alpha=1.0, spiking=True)[源代码]#
-
中文
SuperSpike: Supervised learning in multi-layer spiking neural networks 提出的反向传播时使用SuperSpike的梯度的脉冲发放函数。反向传播为
\[g'(x) = \frac{\alpha}{(1 + (|x|))^{2}}\]- 参数:
English
The SuperSpike surrogate spiking function proposed by SuperSpike: Supervised learning in multi-layer spiking neural networks. The gradient is defined by
\[g'(x) = \frac{\alpha}{(1 + (|x|))^{2}}\]- 参数:
alpha (float) -- parameter to control smoothness of gradient
spiking (bool) -- whether output spikes. The default is
Truewhich means that usingheavisidein forward propagation and using surrogate gradient in backward propagation. IfFalse, in forward propagation, using the primitive function of the surrogate gradient function used in backward propagation
- 返回:
heaviside(x)或原函数的结果,形状与x相同- 返回类型:
- class spikingjelly.activation_based.surrogate.ATan(alpha=2.0, spiking=True)[源代码]#
-
中文
反向传播时使用反正切函数arc tangent的梯度的脉冲发放函数。反向传播为
\[g'(x) = \frac{\alpha}{2(1 + (\frac{\pi}{2}\alpha x)^{2})}\]对应的原函数为
\[g(x) = \frac{1}{\pi} \arctan(\frac{\pi}{2}\alpha x) + \frac{1}{2}\]该函数在文章 [#Huh2018]_ [5] 中使用。
- 参数:
English
The arc tangent surrogate spiking function. The gradient is defined by
\[g'(x) = \frac{\alpha}{2(1 + (\frac{\pi}{2}\alpha x)^{2})}\]The primitive function is defined by
\[g(x) = \frac{1}{\pi} \arctan(\frac{\pi}{2}\alpha x) + \frac{1}{2}\]The function is used in [#Huh2018]_ [5].
- 参数:
alpha (float) -- parameter to control smoothness of gradient
spiking (bool) -- whether output spikes. The default is
Truewhich means that usingheavisidein forward propagation and using surrogate gradient in backward propagation. IfFalse, in forward propagation, using the primitive function of the surrogate gradient function used in backward propagation
- 返回:
heaviside(x)或原函数的结果,形状与x相同- 返回类型:
- class spikingjelly.activation_based.surrogate.NonzeroSignLogAbs(alpha=1.0, spiking=True)[源代码]#
-
中文
警告
原函数的输出范围并不是(0, 1)。它的优势是反向传播的计算量特别小。
反向传播时使用NonzeroSignLogAbs的梯度的脉冲发放函数。反向传播为
\[g'(x) = \frac{\alpha}{1 + |\alpha x|} = \frac{1}{\frac{1}{\alpha} + |x|}\]对应的原函数为
\[g(x) = \mathrm{NonzeroSign}(x) \log (|\alpha x| + 1)\]其中
\[\begin{split}\mathrm{NonzeroSign}(x) = \begin{cases} 1, & x \geq 0 \\ -1, & x < 0 \\ \end{cases}\end{split}\]- 参数:
English
Warning
The output range the primitive function is not (0, 1). The advantage of this function is that computation cost is small when backward.
The NonzeroSignLogAbs surrogate spiking function. The gradient is defined by
\[g'(x) = \frac{\alpha}{1 + |\alpha x|} = \frac{1}{\frac{1}{\alpha} + |x|}\]The primitive function is defined by
\[g(x) = \mathrm{NonzeroSign}(x) \log (|\alpha x| + 1)\]where
\[\begin{split}\mathrm{NonzeroSign}(x) = \begin{cases} 1, & x \geq 0 \\ -1, & x < 0 \\ \end{cases}\end{split}\]The function is used in [3] [4] [8].
- 参数:
alpha (float) -- parameter to control smoothness of gradient
spiking (bool) -- whether output spikes. The default is
Truewhich means that usingheavisidein forward propagation and using surrogate gradient in backward propagation. IfFalse, in forward propagation, using the primitive function of the surrogate gradient function used in backward propagation
- 返回:
无返回值
- 返回类型:
None
- class spikingjelly.activation_based.surrogate.Erf(alpha=2.0, spiking=True)[源代码]#
-
中文
反向传播时使用高斯误差函数(erf)的梯度的脉冲发放函数。反向传播为
\[g'(x) = \frac{\alpha}{\sqrt{\pi}}e^{-\alpha^{2}x^{2}}\]对应的原函数为
\begin{split} g(x) &= \frac{1}{2}(1-\text{erf}(-\alpha x)) \\ &= \frac{1}{2} \text{erfc}(-\alpha x) \\ &= \frac{1}{\sqrt{\pi}}\int_{-\infty}^{\alpha x}e^{-t^{2}}dt \end{split}- 参数:
English
The Gaussian error (erf) surrogate spiking function. The gradient is defined by
\[g'(x) = \frac{\alpha}{\sqrt{\pi}}e^{-\alpha^{2}x^{2}}\]The primitive function is defined by
\begin{split} g(x) &= \frac{1}{2}(1-\text{erf}(-\alpha x)) \\ &= \frac{1}{2} \text{erfc}(-\alpha x) \\ &= \frac{1}{\sqrt{\pi}}\int_{-\infty}^{\alpha x}e^{-t^{2}}dt \end{split}The function is used in [1] [4] [18].
- 参数:
alpha (float) -- parameter to control smoothness of gradient
spiking (bool) -- whether output spikes. The default is
Truewhich means that usingheavisidein forward propagation and using surrogate gradient in backward propagation. IfFalse, in forward propagation, using the primitive function of the surrogate gradient function used in backward propagation
- 返回:
无返回值
- 返回类型:
None
- class spikingjelly.activation_based.surrogate.PiecewiseLeakyReLU(w=1.0, c=0.01, spiking=True)[源代码]#
-
中文
分段线性的近似脉冲发放函数。梯度为
\[\begin{split}g'(x) = \begin{cases} \frac{1}{2w}, & -w \leq x \leq w \\ c, & x < -w ~or~ x > w \end{cases}\end{split}\]对应的原函数为
\[\begin{split}g(x) = \begin{cases} cx + cw, & x < -w \\ \frac{1}{2w}x + \frac{1}{2}, & -w \leq x \leq w \\ cx - cw + 1, & x > w \\ \end{cases}\end{split}\]该函数在文章 [3] [4] [5] [9] [10] [12] [16] [17] 中使用。
- 参数:
English
The piecewise surrogate spiking function. The gradient is defined by
\[\begin{split}g'(x) = \begin{cases} \frac{1}{2w}, & -w \leq x \leq w \\ c, & x < -w ~or~ x > w \end{cases}\end{split}\]The primitive function is defined by
\[\begin{split}g(x) = \begin{cases} cx + cw, & x < -w \\ \frac{1}{2w}x + \frac{1}{2}, & -w \leq x \leq w \\ cx - cw + 1, & x > w \end{cases}\end{split}\]The function is used in [3] [4] [5] [9] [10] [12] [16] [17].
- 参数:
w (float) -- when
-w <= x <= wthe gradient is1 / 2wc (float) -- when
x > worx < -wthe gradient iscspiking (bool) -- whether output spikes. The default is
Truewhich means that usingheavisidein forward propagation and using surrogate gradient in backward propagation. IfFalse, in forward propagation, using the primitive function of the surrogate gradient function used in backward propagation
- 返回:
无返回值
- 返回类型:
None
- class spikingjelly.activation_based.surrogate.SquarewaveFourierSeries(n: int = 2, T_period: float = 8, spiking=True)[源代码]#
-
中文
使用傅里叶级数近似方波的脉冲发放函数。反向传播使用傅里叶级数展开计算梯度。
对应的原函数为
\[g(x) = \frac{1}{2} + \frac{2}{\pi} \sum_{i=1}^{n-1} \frac{\sin((2i-1)\omega x)}{2i-1}\]其中 \(\omega = \frac{2\pi}{T}\)。
- 参数:
English
The square-wave Fourier series surrogate spiking function. The gradient is computed via Fourier series expansion.
The primitive function is defined by
\[g(x) = \frac{1}{2} + \frac{2}{\pi} \sum_{i=1}^{n-1} \frac{\sin((2i-1)\omega x)}{2i-1}\]where \(\omega = \frac{2\pi}{T}\).
- 参数:
n (int) -- number of terms in Fourier series
T_period (float) -- period of the square wave
spiking (bool) -- whether output spikes. The default is
Truewhich means that usingheavisidein forward propagation and using surrogate gradient in backward propagation. IfFalse, in forward propagation, using the primitive function of the surrogate gradient function used in backward propagation
- 返回:
无返回值
- 返回类型:
None
- class spikingjelly.activation_based.surrogate.S2NN(alpha=4.0, beta=1.0, spiking=True)[源代码]#
-
中文
S2NN: Time Step Reduction of Spiking Surrogate Gradients for Training Energy Efficient Single-Step Neural Networks 提出的S2NN替代函数。反向传播为
\[\begin{split}g'(x) = \begin{cases} \alpha * (1 - \mathrm{sigmoid} (\alpha x)) \mathrm{sigmoid} (\alpha x), x < 0 \\ \frac{\beta}{(x + 1)}, x \ge 0 \end{cases}\end{split}\]对应的原函数为
\[\begin{split}g(x) = \begin{cases} \mathrm{sigmoid} (\alpha x), x < 0 \\ \beta \mathrm{ln}(x + 1) + 1, x \ge 0 \end{cases}\end{split}\]- 参数:
English
The S2NN surrogate spiking function, which is proposed by S2NN: Time Step Reduction of Spiking Surrogate Gradients for Training Energy Efficient Single-Step Neural Networks. The gradient is defined by
\[\begin{split}g'(x) = \begin{cases} \alpha * (1 - \mathrm{sigmoid} (\alpha x)) \mathrm{sigmoid} (\alpha x), x < 0 \\ \frac{\beta}{x + 1}, x \ge 0 \end{cases}\end{split}\]The primitive function is defined by
\[\begin{split}g(x) = \begin{cases} \mathrm{sigmoid} (\alpha x), x < 0 \\ \beta \mathrm{ln}(x + 1) + 1, x \ge 0 \end{cases}\end{split}\]- 参数:
alpha (float) -- the param that controls the gradient when
x < 0beta (float) -- the param that controls the gradient when
x >= 0spiking (bool) -- whether output spikes. The default is
Truewhich means that usingheavisidein forward propagation and using surrogate gradient in backward propagation. IfFalse, in forward propagation, using the primitive function of the surrogate gradient function used in backward propagation
- 返回:
无返回值
- 返回类型:
None
- class spikingjelly.activation_based.surrogate.QPseudoSpike(alpha=2.0, spiking=True)[源代码]#
-
中文
Surrogate Gradients Design 提出的 \(q\)-PseudoSpike替代函数。反向传播为
\[g'(x) = (1+\frac{2|x|}{\alpha-1})^{-\alpha}\]其中 \(\alpha>1\) 对应原文中的 \(q\)。
对应的原函数为
\[\begin{split}g(x) = \begin{cases} \frac{1}{2}(1-\frac{2x}{\alpha-1})^{1-\alpha}, & x < 0 \\ 1 - \frac{1}{2}(1+\frac{2x}{\alpha-1})^{1-\alpha}, & x \geq 0. \end{cases}\end{split}\]- 参数:
English
The \(q\)-PseudoSpike surrogate spiking function, which is first proposed in Surrogate Gradients Design. The gradient is defined by
\[g'(x) = (1+\frac{2|x|}{\alpha-1})^{-\alpha}\]where \(\alpha>1\) corresponds to \(q\) in paper.
The primitive function is defined by
\[\begin{split}g(x) = \begin{cases} \frac{1}{2}(1-\frac{2x}{\alpha-1})^{1-\alpha}, & x < 0 \\ 1 - \frac{1}{2}(1+\frac{2x}{\alpha-1})^{1-\alpha}, & x \geq 0. \end{cases}\end{split}\]- 参数:
alpha (float) -- parameter to control tail fatness of gradient
spiking (bool) -- whether output spikes. The default is
Truewhich means that usingheavisidein forward propagation and using surrogate gradient in backward propagation. IfFalse, in forward propagation, using the primitive function of the surrogate gradient function used in backward propagation
- 返回:
无返回值
- 返回类型:
None
- class spikingjelly.activation_based.surrogate.LeakyKReLU(leak: float = 0.0, k: float = 1.0, spiking=True)[源代码]#
-
中文
反向传播时使用LeakyKReLU的梯度的脉冲发放函数。反向传播为
\[\begin{split}g'(x) = \begin{cases} k, & x \geq 0 \\ leak, & x < 0 \\ \end{cases}\end{split}\]对应的原函数为
\[\begin{split}g(x) = \begin{cases} k \cdot x, & x \geq 0 \\ leak \cdot x, & x < 0 \\ \end{cases}\end{split}\]- 参数:
- 返回:
无返回值
- 返回类型:
None
English
The LeakyKReLU surrogate spiking function. The gradient is defined by
\[\begin{split}g'(x) = \begin{cases} k, & x \geq 0 \\ leak, & x < 0 \\ \end{cases}\end{split}\]The primitive function is defined by
\[\begin{split}g(x) = \begin{cases} k \cdot x, & x \geq 0 \\ leak \cdot x, & x < 0 \\ \end{cases}\end{split}\]The function is used in [3] [4] [8].
- 参数:
spiking (bool) -- whether output spikes. The default is
Truewhich means that usingheavisidein forward propagation and using surrogate gradient in backward propagation. IfFalse, in forward propagation, using the primitive function of the surrogate gradient function used in backward propagationleak (float) -- gradient when
x < 0
- 返回:
No return value.
- 返回类型:
None
- class spikingjelly.activation_based.surrogate.FakeNumericalGradient(alpha=0.3)[源代码]#
-
中文
模拟数值梯度的脉冲发放函数,反向传播为
\[g'(x) = \mathrm{clip}(\frac{\mathrm{sign}(x)}{x}, \alpha)\]- 参数:
alpha (float) -- 梯度裁剪阈值
- 返回:
无返回值
- 返回类型:
None
English
The fake numerical gradient surrogate spiking function. The gradient is defined by
\[g'(x) = \mathrm{clip}(\frac{\mathrm{sign}(x)}{x}, \alpha)\]- 参数:
alpha (float) -- gradient clip threshold
- 返回:
No return value.
- 返回类型:
None
- class spikingjelly.activation_based.surrogate.LogTailedReLU(alpha=0.0, spiking=True)[源代码]#
-
中文
Deep Learning with Low Precision by Half-wave Gaussian Quantization 提出的 Log-tailed ReLU替代函数。反向传播为
\[\begin{split}g'(x) = \begin{cases} \alpha, & x \leq 0 \\ 1, & 0 < x \leq 0 \\ \frac{1}{x}, x > 1 \\ \end{cases}\end{split}\]对应的原函数为
\[\begin{split}g(x) = \begin{cases} \alpha x, & x \leq 0 \\ x, & 0 < x \leq 0 \\ log(x), x > 1 \\ \end{cases}\end{split}\]- 参数:
English
The Log-tailed ReLU surrogate spiking function, which is first proposed in Deep Learning with Low Precision by Half-wave Gaussian Quantization. The gradient is defined by
\[\begin{split}g'(x) = \begin{cases} \alpha, & x \leq 0 \\ 1, & 0 < x \leq 0 \\ \frac{1}{x}, x > 1 \\ \end{cases}\end{split}\]The primitive function is defined by
\[\begin{split}g(x) = \begin{cases} \alpha x, & x \leq 0 \\ x, & 0 < x \leq 0 \\ log(x), x > 1 \\ \end{cases}\end{split}\]The function is used in [4] [5] [11].
- 参数:
alpha (float) -- parameter to control gradient
spiking (bool) -- whether output spikes. The default is
Truewhich means that usingheavisidein forward propagation and using surrogate gradient in backward propagation. IfFalse, in forward propagation, using the primitive function of the surrogate gradient function used in backward propagation
- 返回:
无返回值
- 返回类型:
None
- class spikingjelly.activation_based.surrogate.DeterministicPass(spiking=True)[源代码]#
-
中文
直通估计器(Straight-Through Estimator, STE)替代函数。前向传播使用
heaviside阶跃函数,反向传播直接传递梯度。- 参数:
spiking (bool) -- 是否输出脉冲,默认为
True,在前向传播时使用heaviside而在反向传播使用替代梯度。 若为False则直接返回输入。- 返回:
无返回值
- 返回类型:
None
English
The straight-through estimator (STE) surrogate function. Forward uses
heavisidestep function, backward directly passes the gradient through.- 参数:
spiking (bool) -- whether output spikes. The default is
Truewhich means that usingheavisidein forward propagation and using surrogate gradient in backward propagation. IfFalse, returns the input directly.- 返回:
No return value.
- 返回类型:
None
- class spikingjelly.activation_based.surrogate.PoissonPass(spiking=True)[源代码]#
-
中文
泊松发放替代函数。前向传播使用伯努利采样生成随机脉冲(泊松过程),反向传播直接传递梯度。
- 参数:
spiking (bool) -- 是否输出脉冲,默认为
True,在前向传播时使用伯努利采样生成脉冲而在反向传播使用替代梯度。 若为False则直接返回输入。- 返回:
无返回值
- 返回类型:
None
English
The Poisson firing surrogate function. Forward uses Bernoulli sampling to generate stochastic spikes (Poisson process), backward directly passes the gradient through.
- 参数:
spiking (bool) -- whether output spikes. The default is
Truewhich means that using Bernoulli sampling in forward propagation and using surrogate gradient in backward propagation. IfFalse, returns the input directly.- 返回:
No return value.
- 返回类型:
None
- class spikingjelly.activation_based.surrogate.Rect(alpha=1.0, spiking=True)[源代码]#
-
中文
矩形窗替代函数。反向传播为
\[\begin{split}g'(x) = \begin{cases} \alpha, & |x| < \frac{0.5}{\alpha} \\\\ 0, & |x| \geq \frac{0.5}{\alpha} \\end{cases}\end{split}\]对应的原函数为
\[g(x) = \mathrm{clip}(\alpha x + 0.5, 0, 1)\]- 参数:
- 返回:
无返回值
- 返回类型:
None
English
The rectangular window surrogate spiking function. The gradient is defined by
\[\begin{split}g'(x) = \begin{cases} \alpha, & |x| < \frac{0.5}{\alpha} \\\\ 0, & |x| \geq \frac{0.5}{\alpha} \\end{cases}\end{split}\]The primitive function is defined by
\[g(x) = \mathrm{clip}(\alpha x + 0.5, 0, 1)\]- 参数:
alpha (float) -- parameter to control the width of the rectangular window
spiking (bool) -- whether output spikes. The default is
Truewhich means that usingheavisidein forward propagation and using surrogate gradient in backward propagation. IfFalse, in forward propagation, using the primitive function of the surrogate gradient function used in backward propagation
- 返回:
No return value.
- 返回类型:
None
References