spikingjelly.activation_based.quantize module#

class spikingjelly.activation_based.quantize.round_atgf(*args, **kwargs)[源代码]#

基类:Function

API Language: 中文 | English


  • 中文

  • 中文

round 的自动梯度函数。前向传播执行 y = torch.round(x),反向传播将梯度原样传递(即梯度直通估计器 Straight-Through Estimator)。

参数:
返回:

输出张量

返回类型:

torch.Tensor


  • English

  • English

The autograd function for round. The forward executes y = torch.round(x), and the backward passes the gradient through unchanged (i.e., a Straight-Through Estimator).

参数:
返回:

the output tensor

返回类型:

torch.Tensor

spikingjelly.activation_based.quantize.round(x: Tensor)[源代码]#

API Language: 中文 | English


  • 中文

对输入张量应用 y = torch.round(x) 操作,并重新定义梯度为 \(\frac{\partial y}{\partial x} = 1\)

参数:

x (torch.Tensor) -- 输入张量

返回:

输出张量

返回类型:

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

返回类型:

torch.Tensor

class spikingjelly.activation_based.quantize.ceil_atgf(*args, **kwargs)[源代码]#

基类:Function

API Language: 中文 | English


  • 中文

  • 中文

ceil 的自动梯度函数。前向传播执行 y = torch.ceil(x),反向传播将梯度原样传递(即梯度直通估计器 Straight-Through Estimator)。

参数:
返回:

输出张量

返回类型:

torch.Tensor


  • English

  • English

The autograd function for ceil. The forward executes y = torch.ceil(x), and the backward passes the gradient through unchanged (i.e., a Straight-Through Estimator).

参数:
返回:

the output tensor

返回类型:

torch.Tensor

spikingjelly.activation_based.quantize.ceil(x: Tensor)[源代码]#

API Language: 中文 | English


  • 中文

对输入张量应用 y = torch.ceil(x) 操作,并重新定义梯度为 \(\frac{\partial y}{\partial x} = 1\)

参数:

x (torch.Tensor) -- 输入张量

返回:

输出张量

返回类型:

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

返回类型:

torch.Tensor

class spikingjelly.activation_based.quantize.floor_atgf(*args, **kwargs)[源代码]#

基类:Function

API Language: 中文 | English


  • 中文

  • 中文

floor 的自动梯度函数。前向传播执行 y = torch.floor(x),反向传播将梯度原样传递(即梯度直通估计器 Straight-Through Estimator)。

参数:
返回:

输出张量

返回类型:

torch.Tensor


  • English

  • English

The autograd function for floor. The forward executes y = torch.floor(x), and the backward passes the gradient through unchanged (i.e., a Straight-Through Estimator).

参数:
返回:

the output tensor

返回类型:

torch.Tensor

spikingjelly.activation_based.quantize.floor(x: Tensor)[源代码]#

API Language: 中文 | English


  • 中文

对输入张量应用 y = torch.floor(x) 操作,并重新定义梯度为 \(\frac{\partial y}{\partial x} = 1\)

参数:

x (torch.Tensor) -- 输入张量

返回:

输出张量

返回类型:

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

返回类型:

torch.Tensor

spikingjelly.activation_based.quantize.clamp_backward(grad_output: Tensor, x: Tensor, min_value: float, max_value: float)[源代码]#

API Language: 中文 | English


  • 中文

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) -- 夹紧范围的上界

返回:

施加掩码后的梯度

返回类型:

torch.Tensor


  • English

The backward helper function for clamp_atgf. Computes a gradient mask: for elements within min_value <= x <= max_value the 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

返回类型:

torch.Tensor

class spikingjelly.activation_based.quantize.clamp_atgf(*args, **kwargs)[源代码]#

基类:Function

API Language: 中文 | English


  • 中文

  • 中文

clamp 的自动梯度函数。前向传播执行 y = torch.clamp(x, min_value, max_value), 反向传播使用 clamp_backward() 对在 [min_value, max_value] 范围内的元素传递梯度,范围外的元素梯度为 0。

参数:
返回:

夹紧后的张量及 None 占位

返回类型:

torch.Tensor / tuple


  • English

  • English

The autograd function for clamp. The forward executes y = torch.clamp(x, min_value, max_value), and the backward uses clamp_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 None placeholders

返回类型:

torch.Tensor / tuple

spikingjelly.activation_based.quantize.clamp(x: Tensor, min_value: float, max_value: float)[源代码]#

API Language: 中文 | English


  • 中文

  • 中文

应用 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) -- 要夹紧到的范围的上界

返回:

输出张量

返回类型:

torch.Tensor


  • 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

返回类型:

torch.Tensor

spikingjelly.activation_based.quantize.step_quantize_forward(x: Tensor, step: float)[源代码]#

API Language: 中文 | English


  • 中文

  • 中文

step_quantize_atgf 的前向传播辅助函数。将 x 量化到最近的 i * step,其中 i 是整数。

\[\begin{split}y = \\mathrm{round}(x / \\mathrm{step}) \\times \\mathrm{step}\end{split}\]
参数:
返回:

量化后的张量

返回类型:

torch.Tensor


  • English

  • English

The forward helper function for step_quantize_atgf. Quantizes x to the nearest i * step, where i is an integer.

\[\begin{split}y = \\mathrm{round}(x / \\mathrm{step}) \\times \\mathrm{step}\end{split}\]
参数:
返回:

the quantized tensor

返回类型:

torch.Tensor

class spikingjelly.activation_based.quantize.step_quantize_atgf(*args, **kwargs)[源代码]#

基类:Function

API Language: 中文 | English


  • 中文

  • 中文

step_quantize 的自动梯度函数。前向传播调用 step_quantize_forward() 执行步长量化, 反向传播将梯度原样传递(即梯度直通估计器 Straight-Through Estimator)。

参数:
返回:

量化结果及 None 占位

返回类型:

torch.Tensor / tuple


  • English

  • English

The autograd function for step_quantize. The forward calls step_quantize_forward() to perform step quantization, and the backward passes the gradient through unchanged (i.e., a Straight-Through Estimator).

参数:
返回:

the quantized result and None placeholders

返回类型:

torch.Tensor / tuple

spikingjelly.activation_based.quantize.step_quantize(x: Tensor, step: float)[源代码]#

API Language: 中文 | English


  • 中文

  • 中文

x 量化到最近的 i * step,其中 i 是整数。

注意梯度定义为 \(\frac{\partial y}{\partial x} = 1\)

../_images/step_quantize1.svg
参数:
返回:

量化后的张量

返回类型:

torch.Tensor


  • English

  • English

Quantize x to the nearest i * step, where i is an integer.

Note that the gradient is defined by \(\frac{\partial y}{\partial x} = 1\).

../_images/step_quantize1.svg
参数:
返回:

the quantized tensor

返回类型:

torch.Tensor

spikingjelly.activation_based.quantize.k_bit_quantize_forward(x: Tensor, k: int)[源代码]#

API Language: 中文 | English


  • 中文

  • 中文

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) -- 输出位数

返回:

量化后的张量

返回类型:

torch.Tensor


  • English

  • English

The forward helper function for k_bit_quantize_atgf. Quantizes the input in range [0, 1] to the nearest i / (2 ** k - 1), where 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) -- a float tensor whose range is [0, 1]

  • k (int) -- the bit number of output

返回:

the quantized tensor

返回类型:

torch.Tensor

class spikingjelly.activation_based.quantize.k_bit_quantize_atgf(*args, **kwargs)[源代码]#

基类:Function

API Language: 中文 | English


  • 中文

  • 中文

k_bit_quantize 的自动梯度函数。前向传播调用 k_bit_quantize_forward() 执行 DoReFa-Net 风格的 k 位量化, 反向传播将梯度原样传递(即梯度直通估计器 Straight-Through Estimator)。

参数:
返回:

量化结果及 None 占位

返回类型:

torch.Tensor / tuple


  • English

  • English

The autograd function for k_bit_quantize. The forward calls k_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 None placeholders

返回类型:

torch.Tensor / tuple

spikingjelly.activation_based.quantize.k_bit_quantize(x: Tensor, k: int)[源代码]#

API Language: 中文 | English


  • 中文

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.sigmoidtorch.nn.Hardtanhspikingjelly.activation_based.quantize 中的 clamp_* 函数(例如 spikingjelly.activation_based.quantize.clamp_by_linear)。

../_images/k_bit_quantize.svg
参数:
  • x (torch.Tensor) -- 范围为 [0, 1] 的浮点张量

  • k (int) -- 输出的位数

返回:

y = round((2 ** k - 1) * x) / (2 ** k - 1)

返回类型:

torch.Tensor


  • 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 nearest i / (2 ** k - 1), where i = 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), using torch.sigmoid, torch.nn.Hardtanh or clamp_* functions (e.g., spikingjelly.activation_based.quantize.clamp_by_linear) in spikingjelly.activation_based.quantize.

../_images/k_bit_quantize.svg
参数:
  • 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)

返回类型:

torch.Tensor

spikingjelly.activation_based.quantize.affine_k_bit_quantize(x: Tensor, k: int, w: Tensor, b: Tensor)[源代码]#

API Language: 中文 | English


  • 中文

应用仿射量化 y = w * round((2 ** k - 1) * x) / (2 ** k - 1) + b

参数:
返回:

y = w * round((2 ** k - 1) * x) / (2 ** k - 1) + b

返回类型:

torch.Tensor


  • 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

返回类型:

torch.Tensor

spikingjelly.activation_based.quantize.clamp_by_linear(x: Tensor, eps: float = 1e-05)[源代码]#

API Language: 中文 | English


  • 中文

使用线性变换将输入范围从 (-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.]

返回类型:

torch.Tensor


  • 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.]

返回类型:

torch.Tensor