コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
テンソルフロー::作戦::クオンタイズV2
#include <array_ops.h>
float 型の '入力' テンソルを 'T' 型の '出力' テンソルに量子化します。
まとめ
[min_range, max_range] は、「入力」データの範囲を指定するスカラー浮動小数点数です。 「mode」属性は、float 値を量子化された同等の値に変換するためにどの計算が使用されるかを正確に制御します。 「round_mode」属性は、float 値を量子化された値に丸めるときにどの丸めタイブレーク アルゴリズムを使用するかを制御します。
「MIN_COMBINED」モードでは、テンソルの各値は次の処理を受けます。
out[i] = (in[i] - min_range) * range(T) / (max_range - min_range)
if T == qint8: out[i] -= (range(T) + 1) / 2.0
ここでrange(T) = numeric_limits ::max() - numeric_limits ::min()
range(T) = numeric_limits ::max() - numeric_limits ::min()
range(T) = numeric_limits ::max() - numeric_limits ::min()
MIN_COMBINED モードの例
入力は float 型で、可能な範囲は [0.0, 6.0]、出力型は quint8 ([0, 255]) であると仮定します。 min_range 値と max_range 値は 0.0 と 6.0 として指定する必要があります。 float から quint8 に量子化すると、入力の各値が 255/6 で乗算され、quint8 にキャストされます。
出力タイプが qint8 ([-128, 127]) の場合、この演算では、値の範囲が qint8 の範囲と一致するように、キャスト前にさらに各値を 128 減算します。
モードが 'MIN_FIRST' の場合、次のアプローチが使用されます。
num_discrete_values = 1 << (# of bits in T)
range_adjust = num_discrete_values / (num_discrete_values - 1)
range = (range_max - range_min) * range_adjust
range_scale = num_discrete_values / range
quantized = round(input * range_scale) - round(range_min * range_scale) +
numeric_limits::min()
quantized = max(quantized, numeric_limits::min())
quantized = min(quantized, numeric_limits::max())
これと MIN_COMBINED の最大の違いは、最小範囲が丸められた値から減算される前に、最初に丸められることです。 MIN_COMBINED を使用すると、量子化と逆量子化を繰り返すと、小さなバイアスが発生し、誤差がますます大きくなります。
SCALEモードの例
SCALED
モードはQuantizeAndDequantize{V2|V3}
で使用される量子化アプローチと一致します。
モードがSCALED
の場合、出力タイプの全範囲を使用せず、対称性の可能な最小値を無視することを選択します (たとえば、符号付き 8 ビット量子化の出力範囲は -128 ~ 127 ではなく、-127 ~ 127 です)。 0.0 が 0 にマッピングされるようにします。
まず、テンソル内の値の範囲を見つけます。使用する範囲は常に 0 を中心とするため、次のような m を見つけます。
m = max(abs(input_min), abs(input_max))
入力テンソル範囲は[-m, m]
になります。
次に、固定小数点量子化バケット[min_fixed, max_fixed]
を選択します。 T が符号付きの場合、これは次のようになります。
num_bits = sizeof(T) * 8
[min_fixed, max_fixed] =
[-(1 << (num_bits - 1) - 1), (1 << (num_bits - 1)) - 1]
それ以外の場合、T が符号なしの場合、固定小数点範囲は次のようになります。
[min_fixed, max_fixed] = [0, (1 << num_bits) - 1]
これから、スケーリング係数 s を計算します。
s = (max_fixed - min_fixed) / (2 * m)
これで、テンソルの要素を量子化できます。
result = round(input * s)
注意すべき点の 1 つは、オペレータが量子化プロセス中に要求された最小値と最大値をわずかに調整することを選択する可能性があるため、以降の計算の範囲として常に出力ポートを使用する必要があることです。たとえば、要求された最小値と最大値が等しい場合、不正な形式の量子化バッファが作成されるのを防ぐために、小さなイプシロン値で区切られます。そうしないと、すべての量子化値が同じ浮動小数点値にマップされるバッファーが作成され、バッファーに対してさらに計算を実行する必要がある操作で問題が発生する可能性があります。
引数:
- スコープ:スコープオブジェクト
- min_range: 入力に対して生成される可能性のある最小スカラー値。
- max_range: 入力に対して生成される可能性のある最大スカラー値。
戻り値:
-
Output
出力: float 入力から生成された量子化データ。 -
Output
Output_min: 出力に使用される実際の最小スカラー値。 -
Output
Output_max: 出力に使用される実際の最大スカラー値。
パブリック属性
公共機能
パブリック静的関数
モード
Attrs Mode(
StringPiece x
)
ラウンドモード
Attrs RoundMode(
StringPiece x
)
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-07-25 UTC。
[null,null,["最終更新日 2025-07-25 UTC。"],[],[],null,["# tensorflow::ops::QuantizeV2 Class Reference\n\ntensorflow::ops::QuantizeV2\n===========================\n\n`#include \u003carray_ops.h\u003e`\n\nQuantize the 'input' tensor of type float to 'output' tensor of type 'T'.\n\nSummary\n-------\n\n\\[min_range, max_range\\] are scalar floats that specify the range for the 'input' data. The 'mode' attribute controls exactly which calculations are used to convert the float values to their quantized equivalents. The 'round_mode' attribute controls which rounding tie-breaking algorithm is used when rounding float values to their quantized equivalents.\n\nIn 'MIN_COMBINED' mode, each value of the tensor will undergo the following:\n\n\n```transact-sql\nout[i] = (in[i] - min_range) * range(T) / (max_range - min_range)\nif T == qint8: out[i] -= (range(T) + 1) / 2.0\n```\n\n\u003cbr /\u003e\n\nhere `range(T) = numeric_limits`::max() - numeric_limits::min()\n\n\n*MIN_COMBINED Mode Example*\n\nAssume the input is type float and has a possible range of \\[0.0, 6.0\\] and the output type is quint8 (\\[0, 255\\]). The min_range and max_range values should be specified as 0.0 and 6.0. Quantizing from float to quint8 will multiply each value of the input by 255/6 and cast to quint8.\n\nIf the output type was qint8 (\\[-128, 127\\]), the operation will additionally subtract each value by 128 prior to casting, so that the range of values aligns with the range of qint8.\n\nIf the mode is 'MIN_FIRST', then this approach is used:\n\n\n```scdoc\nnum_discrete_values = 1 \u003c\u003c (# of bits in T)\nrange_adjust = num_discrete_values / (num_discrete_values - 1)\nrange = (range_max - range_min) * range_adjust\nrange_scale = num_discrete_values / range\nquantized = round(input * range_scale) - round(range_min * range_scale) +\n numeric_limits::min()\nquantized = max(quantized, numeric_limits::min())\nquantized = min(quantized, numeric_limits::max())\n```\n\n\u003cbr /\u003e\n\nThe biggest difference between this and MIN_COMBINED is that the minimum range is rounded first, before it's subtracted from the rounded value. With MIN_COMBINED, a small bias is introduced where repeated iterations of quantizing and dequantizing will introduce a larger and larger error.\n\n\n*SCALED mode Example*\n\n`SCALED` mode matches the quantization approach used in `QuantizeAndDequantize{V2|V3}`.\n\nIf the mode is `SCALED`, we do not use the full range of the output type, choosing to elide the lowest possible value for symmetry (e.g., output range is -127 to 127, not -128 to 127 for signed 8 bit quantization), so that 0.0 maps to 0.\n\nWe first find the range of values in our tensor. The range we use is always centered on 0, so we find m such that\n\n\n```scdoc\n m = max(abs(input_min), abs(input_max))\n```\n\n\u003cbr /\u003e\n\nOur input tensor range is then `[-m, m]`.\n\nNext, we choose our fixed-point quantization buckets, `[min_fixed, max_fixed]`. If T is signed, this is\n\n\n```scdoc\n num_bits = sizeof(T) * 8\n [min_fixed, max_fixed] =\n [-(1 \u003c\u003c (num_bits - 1) - 1), (1 \u003c\u003c (num_bits - 1)) - 1]\n```\n\n\u003cbr /\u003e\n\nOtherwise, if T is unsigned, the fixed-point range is\n\n\n```scdoc\n [min_fixed, max_fixed] = [0, (1 \u003c\u003c num_bits) - 1]\n```\n\n\u003cbr /\u003e\n\nFrom this we compute our scaling factor, s:\n\n\n```scdoc\n s = (max_fixed - min_fixed) / (2 * m)\n```\n\n\u003cbr /\u003e\n\nNow we can quantize the elements of our tensor:\n\n\n```scdoc\nresult = round(input * s)\n```\n\n\u003cbr /\u003e\n\nOne thing to watch out for is that the operator may choose to adjust the requested minimum and maximum values slightly during the quantization process, so you should always use the output ports as the range for further calculations. For example, if the requested minimum and maximum values are close to equal, they will be separated by a small epsilon value to prevent ill-formed quantized buffers from being created. Otherwise, you can end up with buffers where all the quantized values map to the same float value, which causes problems for operations that have to perform further calculations on them.\n\nArguments:\n\n- scope: A [Scope](/versions/r1.15/api_docs/cc/class/tensorflow/scope#classtensorflow_1_1_scope) object\n- min_range: The minimum scalar value possibly produced for the input.\n- max_range: The maximum scalar value possibly produced for the input.\n\n\u003cbr /\u003e\n\nReturns:\n\n- [Output](/versions/r1.15/api_docs/cc/class/tensorflow/output#classtensorflow_1_1_output) output: The quantized data produced from the float input.\n- [Output](/versions/r1.15/api_docs/cc/class/tensorflow/output#classtensorflow_1_1_output) output_min: The actual minimum scalar value used for the output.\n- [Output](/versions/r1.15/api_docs/cc/class/tensorflow/output#classtensorflow_1_1_output) output_max: The actual maximum scalar value used for the output.\n\n\u003cbr /\u003e\n\n| ### Constructors and Destructors ||\n|---|---|\n| [QuantizeV2](#classtensorflow_1_1ops_1_1_quantize_v2_1aac3aa6f1389108bdf11da28fbe9aef92)`(const ::`[tensorflow::Scope](/versions/r1.15/api_docs/cc/class/tensorflow/scope#classtensorflow_1_1_scope)` & scope, ::`[tensorflow::Input](/versions/r1.15/api_docs/cc/class/tensorflow/input#classtensorflow_1_1_input)` input, ::`[tensorflow::Input](/versions/r1.15/api_docs/cc/class/tensorflow/input#classtensorflow_1_1_input)` min_range, ::`[tensorflow::Input](/versions/r1.15/api_docs/cc/class/tensorflow/input#classtensorflow_1_1_input)` max_range, DataType T)` ||\n| [QuantizeV2](#classtensorflow_1_1ops_1_1_quantize_v2_1a7881efba11474e644c63d7540730053d)`(const ::`[tensorflow::Scope](/versions/r1.15/api_docs/cc/class/tensorflow/scope#classtensorflow_1_1_scope)` & scope, ::`[tensorflow::Input](/versions/r1.15/api_docs/cc/class/tensorflow/input#classtensorflow_1_1_input)` input, ::`[tensorflow::Input](/versions/r1.15/api_docs/cc/class/tensorflow/input#classtensorflow_1_1_input)` min_range, ::`[tensorflow::Input](/versions/r1.15/api_docs/cc/class/tensorflow/input#classtensorflow_1_1_input)` max_range, DataType T, const `[QuantizeV2::Attrs](/versions/r1.15/api_docs/cc/struct/tensorflow/ops/quantize-v2/attrs#structtensorflow_1_1ops_1_1_quantize_v2_1_1_attrs)` & attrs)` ||\n\n| ### Public attributes ||\n|------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------|\n| [operation](#classtensorflow_1_1ops_1_1_quantize_v2_1ad0de103146d140ed0b4bf277d2dd9af2) | [Operation](/versions/r1.15/api_docs/cc/class/tensorflow/operation#classtensorflow_1_1_operation) |\n| [output](#classtensorflow_1_1ops_1_1_quantize_v2_1ada6c67087a12267be45aac6a9120de55) | `::`[tensorflow::Output](/versions/r1.15/api_docs/cc/class/tensorflow/output#classtensorflow_1_1_output) |\n| [output_max](#classtensorflow_1_1ops_1_1_quantize_v2_1a79cc3f023f74de524cd442b0492e4dae) | `::`[tensorflow::Output](/versions/r1.15/api_docs/cc/class/tensorflow/output#classtensorflow_1_1_output) |\n| [output_min](#classtensorflow_1_1ops_1_1_quantize_v2_1a7690769886d844fc55484420e1c43b65) | `::`[tensorflow::Output](/versions/r1.15/api_docs/cc/class/tensorflow/output#classtensorflow_1_1_output) |\n\n| ### Public static functions ||\n|----------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------|\n| [Mode](#classtensorflow_1_1ops_1_1_quantize_v2_1aad3b22239c9881921a6553cef91d9e9a)`(StringPiece x)` | [Attrs](/versions/r1.15/api_docs/cc/struct/tensorflow/ops/quantize-v2/attrs#structtensorflow_1_1ops_1_1_quantize_v2_1_1_attrs) |\n| [RoundMode](#classtensorflow_1_1ops_1_1_quantize_v2_1af537bced0682ba25e43c0d5856fd38a5)`(StringPiece x)` | [Attrs](/versions/r1.15/api_docs/cc/struct/tensorflow/ops/quantize-v2/attrs#structtensorflow_1_1ops_1_1_quantize_v2_1_1_attrs) |\n\n| ### Structs ||\n|-----------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------|\n| [tensorflow::ops::QuantizeV2::Attrs](/versions/r1.15/api_docs/cc/struct/tensorflow/ops/quantize-v2/attrs) | Optional attribute setters for [QuantizeV2](/versions/r1.15/api_docs/cc/class/tensorflow/ops/quantize-v2#classtensorflow_1_1ops_1_1_quantize_v2). |\n\nPublic attributes\n-----------------\n\n### operation\n\n```text\nOperation operation\n``` \n\n### output\n\n```text\n::tensorflow::Output output\n``` \n\n### output_max\n\n```scdoc\n::tensorflow::Output output_max\n``` \n\n### output_min\n\n```scdoc\n::tensorflow::Output output_min\n``` \n\nPublic functions\n----------------\n\n### QuantizeV2\n\n```gdscript\n QuantizeV2(\n const ::tensorflow::Scope & scope,\n ::tensorflow::Input input,\n ::tensorflow::Input min_range,\n ::tensorflow::Input max_range,\n DataType T\n)\n``` \n\n### QuantizeV2\n\n```gdscript\n QuantizeV2(\n const ::tensorflow::Scope & scope,\n ::tensorflow::Input input,\n ::tensorflow::Input min_range,\n ::tensorflow::Input max_range,\n DataType T,\n const QuantizeV2::Attrs & attrs\n)\n``` \n\nPublic static functions\n-----------------------\n\n### Mode\n\n```text\nAttrs Mode(\n StringPiece x\n)\n``` \n\n### RoundMode\n\n```text\nAttrs RoundMode(\n StringPiece x\n)\n```"]]