spikingjelly.activation_based.quantize module#

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

API Language - 中文 | English


  • 中文

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

参数:

x (Tensor) -- 输入张量

返回:

输出张量

返回类型:

Tensor


  • English

Apply y = torch.round(x) with re-defining gradient as \(\frac{\partial y}{\partial x} = 1\).

参数:

x (Tensor) -- the input tensor

返回:

the output tensor

返回类型:

Tensor

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

API Language - 中文 | English


  • 中文

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

参数:

x (Tensor) -- 输入张量

返回:

输出张量

返回类型:

Tensor


  • English

Apply y = torch.ceil(x) with re-defining gradient as \(\frac{\partial y}{\partial x} = 1\).

参数:

x (Tensor) -- the input tensor

返回:

the output tensor

返回类型:

Tensor

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

API Language - 中文 | English


  • 中文

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

参数:

x (Tensor) -- 输入张量

返回:

输出张量

返回类型:

Tensor


  • English

Apply y = torch.floor(x) with re-defining gradient as \(\frac{\partial y}{\partial x} = 1\).

参数:

x (Tensor) -- the input tensor

返回:

the output tensor

返回类型:

Tensor

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

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 (Tensor) -- 输入张量

  • min_value (float) -- 要夹紧到的范围的下界

  • max_value (float) -- 要夹紧到的范围的上界

返回:

输出张量

返回类型:

Tensor


  • 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 (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

返回类型:

Tensor

spikingjelly.activation_based.quantize.step_quantize(x, step=1.0)[源代码]#

API Language - 中文 | English


  • 中文

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

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

../_images/step_quantize.svg
参数:
  • x (Tensor) -- 输入张量

  • step (float) -- 量化步长

返回:

量化后的张量

返回类型:

Tensor


  • 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_quantize.svg
参数:
  • x (Tensor) -- the input tensor

  • step (float) -- the quantize step

返回:

the quantized tensor

返回类型:

Tensor

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

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 (Tensor) -- 范围为 [0, 1] 的浮点张量

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

返回:

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

返回类型:

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 (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)

返回类型:

Tensor

抛出:

ValueError -- if k < 1

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

API Language - 中文 | English


  • 中文

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

参数:
  • x (Tensor) -- 范围为 [0, 1] 的浮点张量

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

  • w (Tensor) -- 仿射变换的权重

  • b (Tensor) -- 仿射变换的偏置

返回:

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

返回类型:

Tensor


  • English

Apply an affine quantization with y = w * round((2 ** k - 1) * x) / (2 ** k - 1) + b.

参数:
  • x (Tensor) -- a float tensor whose range is [0, 1].

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

  • w (Tensor) -- the weight of the affine transform

  • b (Tensor) -- the bias of the affine transform

返回:

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

返回类型:

Tensor

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

API Language - 中文 | English


  • 中文

使用线性变换将输入范围从 (-inf, inf) 夹紧到 [0., 1.]

\[y = \frac{x - \mathrm{min}(x)}{\mathrm{max}(x) - \mathrm{min}(x) + eps}\]
参数:
  • x (Tensor) -- 要归一化的输入张量,其范围为 (-inf, inf)

  • eps (float) -- 添加到分母的小值以保证数值稳定性,默认值为 1e-5

返回:

归一化后的张量,其范围为 [0., 1.]

返回类型:

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

返回类型:

Tensor