評價此頁

torch.quantize_per_tensor#

torch.quantize_per_tensor(input, scale, zero_point, dtype) Tensor#

將浮點張量轉換為具有給定縮放和零點的量化張量。

引數
  • input (Tensor) – 要量化的浮點張量或張量列表

  • scale (floatTensor) – 量化公式中應用的縮放因子

  • zero_point (intTensor) – 對映到浮點零的整數偏移量

  • dtype (torch.dtype) – 返回張表的期望資料型別。必須是以下量化資料型別之一:torch.quint8torch.qint8torch.qint32

返回

一個新量化的張量或量化張量列表。

返回型別

張量

示例

>>> torch.quantize_per_tensor(torch.tensor([-1.0, 0.0, 1.0, 2.0]), 0.1, 10, torch.quint8)
tensor([-1.,  0.,  1.,  2.], size=(4,), dtype=torch.quint8,
       quantization_scheme=torch.per_tensor_affine, scale=0.1, zero_point=10)
>>> torch.quantize_per_tensor(torch.tensor([-1.0, 0.0, 1.0, 2.0]), 0.1, 10, torch.quint8).int_repr()
tensor([ 0, 10, 20, 30], dtype=torch.uint8)
>>> torch.quantize_per_tensor([torch.tensor([-1.0, 0.0]), torch.tensor([-2.0, 2.0])],
>>> torch.tensor([0.1, 0.2]), torch.tensor([10, 20]), torch.quint8)
(tensor([-1.,  0.], size=(2,), dtype=torch.quint8,
    quantization_scheme=torch.per_tensor_affine, scale=0.1, zero_point=10),
    tensor([-2.,  2.], size=(2,), dtype=torch.quint8,
    quantization_scheme=torch.per_tensor_affine, scale=0.2, zero_point=20))
>>> torch.quantize_per_tensor(torch.tensor([-1.0, 0.0, 1.0, 2.0]), torch.tensor(0.1), torch.tensor(10), torch.quint8)
tensor([-1.,  0.,  1.,  2.], size=(4,), dtype=torch.quint8,
   quantization_scheme=torch.per_tensor_affine, scale=0.10, zero_point=10)