spikingjelly.activation_based.quantize module#
- class spikingjelly.activation_based.quantize.round_atgf(*args, **kwargs)[源代码]#
基类:
Function
中文
中文
round的自动梯度函数。前向传播执行y = torch.round(x),反向传播将梯度原样传递(即梯度直通估计器 Straight-Through Estimator)。- 参数:
x (torch.Tensor) -- 输入张量
grad_output (torch.Tensor) -- 上游梯度
- 返回:
输出张量
- 返回类型:
English
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).- 参数:
x (torch.Tensor) -- the input tensor
grad_output (torch.Tensor) -- the upstream gradient
- 返回:
the output tensor
- 返回类型:
- spikingjelly.activation_based.quantize.round(x: Tensor)[源代码]#
-
中文
对输入张量应用
y = torch.round(x)操作,并重新定义梯度为 \(\frac{\partial y}{\partial x} = 1\)。- 参数:
x (torch.Tensor) -- 输入张量
- 返回:
输出张量
- 返回类型:
English
Apply
y = torch.round(x)with re-defining gradient as \(\frac{\partial y}{\partial x} = 1\).- 参数:
x (torch.Tensor) -- the input tensor
- 返回:
the output tensor
- 返回类型:
- class spikingjelly.activation_based.quantize.ceil_atgf(*args, **kwargs)[源代码]#
基类:
Function
中文
中文
ceil的自动梯度函数。前向传播执行y = torch.ceil(x),反向传播将梯度原样传递(即梯度直通估计器 Straight-Through Estimator)。- 参数:
x (torch.Tensor) -- 输入张量
grad_output (torch.Tensor) -- 上游梯度
- 返回:
输出张量
- 返回类型:
English
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).- 参数:
x (torch.Tensor) -- the input tensor
grad_output (torch.Tensor) -- the upstream gradient
- 返回:
the output tensor
- 返回类型:
- spikingjelly.activation_based.quantize.ceil(x: Tensor)[源代码]#
-
中文
对输入张量应用
y = torch.ceil(x)操作,并重新定义梯度为 \(\frac{\partial y}{\partial x} = 1\)。- 参数:
x (torch.Tensor) -- 输入张量
- 返回:
输出张量
- 返回类型:
English
Apply
y = torch.ceil(x)with re-defining gradient as \(\frac{\partial y}{\partial x} = 1\).- 参数:
x (torch.Tensor) -- the input tensor
- 返回:
the output tensor
- 返回类型:
- class spikingjelly.activation_based.quantize.floor_atgf(*args, **kwargs)[源代码]#
基类:
Function
中文
中文
floor的自动梯度函数。前向传播执行y = torch.floor(x),反向传播将梯度原样传递(即梯度直通估计器 Straight-Through Estimator)。- 参数:
x (torch.Tensor) -- 输入张量
grad_output (torch.Tensor) -- 上游梯度
- 返回:
输出张量
- 返回类型:
English
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).- 参数:
x (torch.Tensor) -- the input tensor
grad_output (torch.Tensor) -- the upstream gradient
- 返回:
the output tensor
- 返回类型:
- spikingjelly.activation_based.quantize.floor(x: Tensor)[源代码]#
-
中文
对输入张量应用
y = torch.floor(x)操作,并重新定义梯度为 \(\frac{\partial y}{\partial x} = 1\)。- 参数:
x (torch.Tensor) -- 输入张量
- 返回:
输出张量
- 返回类型:
English
Apply
y = torch.floor(x)with re-defining gradient as \(\frac{\partial y}{\partial x} = 1\).- 参数:
x (torch.Tensor) -- the input tensor
- 返回:
the output tensor
- 返回类型:
- spikingjelly.activation_based.quantize.clamp_backward(grad_output: Tensor, x: Tensor, min_value: float, max_value: float)[源代码]#
-
中文
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}\]- 参数:
grad_output (torch.Tensor) -- 上游梯度
x (torch.Tensor) -- 前向传播时的输入张量
min_value (float) -- 夹紧范围的下界
max_value (float) -- 夹紧范围的上界
- 返回:
施加掩码后的梯度
- 返回类型:
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}\]- 参数:
grad_output (torch.Tensor) -- the upstream gradient
x (torch.Tensor) -- the input tensor from the forward pass
min_value (float) -- the lower-bound of the clamping range
max_value (float) -- the upper-bound of the clamping range
- 返回:
the masked gradient
- 返回类型:
- 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。- 参数:
x (torch.Tensor) -- 输入张量
min_value (float) -- 夹紧范围的下界
max_value (float) -- 夹紧范围的上界
grad_output (torch.Tensor) -- 上游梯度
- 返回:
夹紧后的张量及
None占位- 返回类型:
torch.Tensor / tuple
English
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.- 参数:
x (torch.Tensor) -- the input tensor
min_value (float) -- the lower-bound of the clamping range
max_value (float) -- the upper-bound of the clamping range
grad_output (torch.Tensor) -- the upstream gradient
- 返回:
the clamped tensor and
Noneplaceholders- 返回类型:
torch.Tensor / tuple
- spikingjelly.activation_based.quantize.clamp(x: Tensor, min_value: float, max_value: float)[源代码]#
-
中文
中文
应用
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}\]- 参数:
x (torch.Tensor) -- 输入张量
min_value (float) -- 要夹紧到的范围的下界
max_value (float) -- 要夹紧到的范围的上界
- 返回:
输出张量
- 返回类型:
English
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}\]- 参数:
x (torch.Tensor) -- the input tensor
min_value (float) -- lower-bound of the range to be clamped to
max_value (float) -- upper-bound of the range to be clamped to
- 返回:
the output tensor
- 返回类型:
- spikingjelly.activation_based.quantize.step_quantize_forward(x: Tensor, step: float)[源代码]#
-
中文
中文
step_quantize_atgf的前向传播辅助函数。将x量化到最近的i * step,其中i是整数。\[\begin{split}y = \\mathrm{round}(x / \\mathrm{step}) \\times \\mathrm{step}\end{split}\]- 参数:
x (torch.Tensor) -- 输入张量
step (float) -- 量化步长
- 返回:
量化后的张量
- 返回类型:
English
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}\]- 参数:
x (torch.Tensor) -- the input tensor
step (float) -- the quantization step size
- 返回:
the quantized tensor
- 返回类型:
- class spikingjelly.activation_based.quantize.step_quantize_atgf(*args, **kwargs)[源代码]#
基类:
Function
中文
中文
step_quantize的自动梯度函数。前向传播调用step_quantize_forward()执行步长量化, 反向传播将梯度原样传递(即梯度直通估计器 Straight-Through Estimator)。- 参数:
x (torch.Tensor) -- 输入张量
step (float) -- 量化步长
grad_output (torch.Tensor) -- 上游梯度
- 返回:
量化结果及
None占位- 返回类型:
torch.Tensor / tuple
English
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).- 参数:
x (torch.Tensor) -- the input tensor
step (float) -- the quantization step size
grad_output (torch.Tensor) -- the upstream gradient
- 返回:
the quantized result and
Noneplaceholders- 返回类型:
torch.Tensor / tuple
- spikingjelly.activation_based.quantize.step_quantize(x: Tensor, step: float)[源代码]#
-
中文
中文
将
x量化到最近的i * step,其中i是整数。注意梯度定义为 \(\frac{\partial y}{\partial x} = 1\)。
- 参数:
x (torch.Tensor) -- 输入张量
step (float) -- 量化步长
- 返回:
量化后的张量
- 返回类型:
English
English
Quantize
xto the nearesti * step, whereiis an integer.Note that the gradient is defined by \(\frac{\partial y}{\partial x} = 1\).
- 参数:
x (torch.Tensor) -- the input tensor
step (float) -- the quantize step
- 返回:
the quantized tensor
- 返回类型:
- spikingjelly.activation_based.quantize.k_bit_quantize_forward(x: Tensor, k: int)[源代码]#
-
中文
中文
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}\]- 参数:
x (torch.Tensor) -- 范围为
[0, 1]的浮点张量k (int) -- 输出位数
- 返回:
量化后的张量
- 返回类型:
English
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}\]- 参数:
x (torch.Tensor) -- a float tensor whose range is
[0, 1]k (int) -- the bit number of output
- 返回:
the quantized tensor
- 返回类型:
- 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)。- 参数:
x (torch.Tensor) -- 范围为
[0, 1]的浮点张量k (int) -- 输出位数
grad_output (torch.Tensor) -- 上游梯度
- 返回:
量化结果及
None占位- 返回类型:
torch.Tensor / tuple
English
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).- 参数:
x (torch.Tensor) -- a float tensor whose range is
[0, 1]k (int) -- the bit number of output
grad_output (torch.Tensor) -- the upstream gradient
- 返回:
the quantized result and
Noneplaceholders- 返回类型:
torch.Tensor / tuple
- spikingjelly.activation_based.quantize.k_bit_quantize(x: Tensor, k: int)[源代码]#
-
中文
在 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)。- 参数:
x (torch.Tensor) -- 范围为
[0, 1]的浮点张量k (int) -- 输出的位数
- 返回:
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.- 参数:
x (torch.Tensor) -- a float tensor whose range is
[0, 1].k (int) -- the bit number of output
- 返回:
y = round((2 ** k - 1) * x) / (2 ** k - 1)- 返回类型:
- spikingjelly.activation_based.quantize.affine_k_bit_quantize(x: Tensor, k: int, w: Tensor, b: Tensor)[源代码]#
-
中文
应用仿射量化
y = w * round((2 ** k - 1) * x) / (2 ** k - 1) + b。- 参数:
x (torch.Tensor) -- 范围为
[0, 1]的浮点张量k (int) -- 输出的位数
w (torch.Tensor) -- 仿射变换的权重
b (torch.Tensor) -- 仿射变换的偏置
- 返回:
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.- 参数:
x (torch.Tensor) -- a float tensor whose range is
[0, 1].k (int) -- the bit number of output
w (torch.Tensor) -- the weight of the affine transform
b (torch.Tensor) -- the bias of the affine transform
- 返回:
y = w * round((2 ** k - 1) * x) / (2 ** k - 1) + b- 返回类型:
- spikingjelly.activation_based.quantize.clamp_by_linear(x: Tensor, eps: float = 1e-05)[源代码]#
-
中文
使用线性变换将输入范围从
(-inf, inf)夹紧到[0., 1.]:\[y = \frac{x - \mathrm{min}(x)}{\mathrm{max}(x) - \mathrm{min}(x) + eps}\]- 参数:
x (torch.Tensor) -- 要归一化的输入张量,其范围为
(-inf, inf)eps (float) -- 添加到分母的小值以保证数值稳定性,默认值为
1e-5
- 返回:
归一化后的张量,其范围为
[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}\]- 参数:
x (torch.Tensor) -- the input tensor to be normed, whose range is
(-inf, inf)eps (float) -- a value added to the denominator for numerical stability. The default value is
1e-5
- 返回:
the normed tensor, whose range is
[0., 1.]- 返回类型: