spikingjelly.activation_based.quantize module#
- class spikingjelly.activation_based.quantize.round_atgf(*args, **kwargs)[源代码]#
基类:
Function
中文
round的自动梯度函数。前向传播执行y = torch.round(x),反向传播将梯度原样传递(即梯度直通估计器 Straight-Through Estimator)。
English
The autograd function for
round. The forward executesy = torch.round(x), and the backward passes the gradient through unchanged (i.e., a Straight-Through Estimator).
- spikingjelly.activation_based.quantize.round(x)[源代码]#
-
中文
对输入张量应用
y = torch.round(x)操作,并重新定义梯度为 \(\frac{\partial y}{\partial x} = 1\)。
English
Apply
y = torch.round(x)with re-defining gradient as \(\frac{\partial y}{\partial x} = 1\).
- class spikingjelly.activation_based.quantize.ceil_atgf(*args, **kwargs)[源代码]#
基类:
Function
中文
ceil的自动梯度函数。前向传播执行y = torch.ceil(x),反向传播将梯度原样传递(即梯度直通估计器 Straight-Through Estimator)。
English
The autograd function for
ceil. The forward executesy = torch.ceil(x), and the backward passes the gradient through unchanged (i.e., a Straight-Through Estimator).
- spikingjelly.activation_based.quantize.ceil(x)[源代码]#
-
中文
对输入张量应用
y = torch.ceil(x)操作,并重新定义梯度为 \(\frac{\partial y}{\partial x} = 1\)。
English
Apply
y = torch.ceil(x)with re-defining gradient as \(\frac{\partial y}{\partial x} = 1\).
- class spikingjelly.activation_based.quantize.floor_atgf(*args, **kwargs)[源代码]#
基类:
Function
中文
floor的自动梯度函数。前向传播执行y = torch.floor(x),反向传播将梯度原样传递(即梯度直通估计器 Straight-Through Estimator)。
English
The autograd function for
floor. The forward executesy = torch.floor(x), and the backward passes the gradient through unchanged (i.e., a Straight-Through Estimator).
- spikingjelly.activation_based.quantize.floor(x)[源代码]#
-
中文
对输入张量应用
y = torch.floor(x)操作,并重新定义梯度为 \(\frac{\partial y}{\partial x} = 1\)。
English
Apply
y = torch.floor(x)with re-defining gradient as \(\frac{\partial y}{\partial x} = 1\).
- spikingjelly.activation_based.quantize.clamp_backward(grad_output, x, min_value, max_value)[源代码]#
-
中文
clamp_atgf的反向传播辅助函数。计算梯度掩码:对于min_value <= x <= max_value范围内的元素梯度保持不变,否则梯度为 0。\[\begin{split}\frac{\partial y}{\partial x} = \begin{cases} 1, \mathrm{min\_value} \leq x \leq \mathrm{max\_value} \\ 0, \mathrm{otherwise} \end{cases}\end{split}\]- 参数:
- 返回:
施加掩码后的梯度
- 返回类型:
English
The backward helper function for
clamp_atgf. Computes a gradient mask: for elements withinmin_value <= x <= max_valuethe gradient passes through, otherwise it is zero.\[\begin{split}\frac{\partial y}{\partial x} = \begin{cases} 1, \mathrm{min\_value} \leq x \leq \mathrm{max\_value} \\ 0, \mathrm{otherwise} \end{cases}\end{split}\]
- class spikingjelly.activation_based.quantize.clamp_atgf(*args, **kwargs)[源代码]#
基类:
Function
中文
clamp的自动梯度函数。前向传播执行y = torch.clamp(x, min_value, max_value), 反向传播使用clamp_backward()对在[min_value, max_value]范围内的元素传递梯度,范围外的元素梯度为 0。- 参数:
- 返回:
夹紧后的张量及
None占位- 返回类型:
torch.Tensor / tuple
English
The autograd function for
clamp. The forward executesy = torch.clamp(x, min_value, max_value), and the backward usesclamp_backward()to pass gradients for elements within[min_value, max_value]and zero out gradients outside.
- spikingjelly.activation_based.quantize.clamp(x, min_value, max_value)[源代码]#
-
中文
应用
y = torch.clamp(x, min_value, max_value)操作,并重新定义梯度为:\[\begin{split}\frac{\partial y}{\partial x} = \begin{cases} 1, \mathrm{min\_value} \leq x \leq \mathrm{max\_value} \\ 0, \mathrm{otherwise} \end{cases}\end{split}\]- 参数:
- 返回:
输出张量
- 返回类型:
English
Apply
y = torch.clamp(x, min_value, max_value)with re-defining gradient as:\[\begin{split}\frac{\partial y}{\partial x} = \begin{cases} 1, \mathrm{min\_value} \leq x \leq \mathrm{max\_value} \\ 0, \mathrm{otherwise} \end{cases}\end{split}\]
- spikingjelly.activation_based.quantize.step_quantize_forward(x, step)[源代码]#
-
中文
step_quantize_atgf的前向传播辅助函数。将x量化到最近的i * step,其中i是整数。\[\begin{split}y = \\mathrm{round}(x / \\mathrm{step}) \\times \\mathrm{step}\end{split}\]
English
The forward helper function for
step_quantize_atgf. Quantizesxto the nearesti * step, whereiis an integer.\[\begin{split}y = \\mathrm{round}(x / \\mathrm{step}) \\times \\mathrm{step}\end{split}\]
- class spikingjelly.activation_based.quantize.step_quantize_atgf(*args, **kwargs)[源代码]#
基类:
Function
中文
step_quantize的自动梯度函数。前向传播调用step_quantize_forward()执行步长量化, 反向传播将梯度原样传递(即梯度直通估计器 Straight-Through Estimator)。- 参数:
- 返回:
量化结果及
None占位- 返回类型:
torch.Tensor / tuple
English
The autograd function for
step_quantize. The forward callsstep_quantize_forward()to perform step quantization, and the backward passes the gradient through unchanged (i.e., a Straight-Through Estimator).
- spikingjelly.activation_based.quantize.step_quantize(x, step)[源代码]#
-
中文
将
x量化到最近的i * step,其中i是整数。注意梯度定义为 \(\frac{\partial y}{\partial x} = 1\)。
English
Quantize
xto the nearesti * step, whereiis an integer.Note that the gradient is defined by \(\frac{\partial y}{\partial x} = 1\).
- spikingjelly.activation_based.quantize.k_bit_quantize_forward(x, k)[源代码]#
-
中文
k_bit_quantize_atgf的前向传播辅助函数。将范围为[0, 1]的输入量化到最近的i / (2 ** k - 1), 其中i = 0, 1, ..., (2 ** k - 1)。\[\begin{split}y = \\frac{\\mathrm{round}((2^k - 1) \\cdot x)}{2^k - 1}\end{split}\]
English
The forward helper function for
k_bit_quantize_atgf. Quantizes the input in range[0, 1]to the nearesti / (2 ** k - 1), wherei = 0, 1, ..., (2 ** k - 1).\[\begin{split}y = \\frac{\\mathrm{round}((2^k - 1) \\cdot x)}{2^k - 1}\end{split}\]
- class spikingjelly.activation_based.quantize.k_bit_quantize_atgf(*args, **kwargs)[源代码]#
基类:
Function
中文
k_bit_quantize的自动梯度函数。前向传播调用k_bit_quantize_forward()执行 DoReFa-Net 风格的 k 位量化, 反向传播将梯度原样传递(即梯度直通估计器 Straight-Through Estimator)。- 参数:
- 返回:
量化结果及
None占位- 返回类型:
torch.Tensor / tuple
English
The autograd function for
k_bit_quantize. The forward callsk_bit_quantize_forward()to perform DoReFa-Net style k-bit quantization, and the backward passes the gradient through unchanged (i.e., a Straight-Through Estimator).
- spikingjelly.activation_based.quantize.k_bit_quantize(x, k)[源代码]#
-
中文
在 DoReFa-Net: Training Low Bitwidth Convolutional Neural Networks with Low Bitwidth Gradients 中定义的k位量化器。
范围为
[0, 1]的输入将被量化到最近的i / (2 ** k - 1),其中i = 0, 1, ..., (2 ** k - 1)。注意梯度定义为 \(\frac{\partial y}{\partial x} = 1\)。
要将范围为
(-inf, inf)的输入夹紧到范围(0, 1),可以使用torch.sigmoid、torch.nn.Hardtanh或spikingjelly.activation_based.quantize中的clamp_*函数(例如spikingjelly.activation_based.quantize.clamp_by_linear)。- 参数:
- 返回:
y = round((2 ** k - 1) * x) / (2 ** k - 1)- 返回类型:
English
The k-bit quantizer defined in DoReFa-Net: Training Low Bitwidth Convolutional Neural Networks with Low Bitwidth Gradients.
The input whose range is
[0, 1]will be quantized to the nearesti / (2 ** k - 1), wherei = 0, 1, ..., (2 ** k - 1).Note that the gradient is defined by \(\frac{\partial y}{\partial x} = 1\).
To clamp the input whose range is
(-inf, inf)to range(0, 1), usingtorch.sigmoid,torch.nn.Hardtanhorclamp_*functions (e.g.,spikingjelly.activation_based.quantize.clamp_by_linear) inspikingjelly.activation_based.quantize.- 参数:
- 返回:
y = round((2 ** k - 1) * x) / (2 ** k - 1)- 返回类型:
- 抛出:
ValueError -- if
k < 1
- spikingjelly.activation_based.quantize.affine_k_bit_quantize(x, k, w, b)[源代码]#
-
中文
应用仿射量化
y = w * round((2 ** k - 1) * x) / (2 ** k - 1) + b。- 参数:
- 返回:
y = w * round((2 ** k - 1) * x) / (2 ** k - 1) + b- 返回类型:
English
Apply an affine quantization with
y = w * round((2 ** k - 1) * x) / (2 ** k - 1) + b.
- spikingjelly.activation_based.quantize.clamp_by_linear(x, eps=1e-05)[源代码]#
-
中文
使用线性变换将输入范围从
(-inf, inf)夹紧到[0., 1.]:\[y = \frac{x - \mathrm{min}(x)}{\mathrm{max}(x) - \mathrm{min}(x) + eps}\]- 参数:
- 返回:
归一化后的张量,其范围为
[0., 1.]- 返回类型:
English
Using the linear transform to clamp the input range from
(-inf, inf)to[0., 1.]:\[y = \frac{x - \mathrm{min}(x)}{\mathrm{max}(x) - \mathrm{min}(x) + eps}\]