تدفق التوتر:: العمليات:: QuantizeV2
#include <array_ops.h>
قم بقياس موتر "الإدخال" من النوع float إلى موتر "الإخراج" من النوع "T".
ملخص
[min_range, max_range] عبارة عن عوامات عددية تحدد نطاق بيانات "الإدخال". تتحكم السمة "mode" بالضبط في الحسابات المستخدمة لتحويل القيم العائمة إلى معادلاتها الكمية. تتحكم السمة 'round_mode' في خوارزمية تقريب التعادل المستخدمة عند تقريب القيم العائمة إلى مكافئاتها الكمية.
في الوضع "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. سيؤدي القياس الكمي من التعويم إلى quint8 إلى مضاعفة كل قيمة من المدخلات بمقدار 255/6 وإلقاءها على quint8.
إذا كان نوع الإخراج هو qint8 ([-128، 127])، فستعمل العملية أيضًا على طرح كل قيمة بمقدار 128 قبل الإرسال، بحيث يتماشى نطاق القيم مع نطاق qint8.
إذا كان الوضع هو "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، يتم تقديم انحياز صغير حيث تؤدي التكرارات المتكررة للتكميم والتكميم إلى حدوث خطأ أكبر وأكبر.
مثال على وضع مقياس الحجم
يتطابق وضع SCALED
مع أسلوب التكميم المستخدم في QuantizeAndDequantize{V2|V3}
.
إذا كان الوضع SCALED
، فإننا لا نستخدم النطاق الكامل لنوع الإخراج، ونختار حذف أقل قيمة ممكنة للتناظر (على سبيل المثال، نطاق الإخراج هو -127 إلى 127، وليس -128 إلى 127 لتكميم 8 بت الموقع)، بحيث يتم تعيين 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)
هناك شيء واحد يجب الانتباه إليه وهو أن المشغل قد يختار ضبط الحد الأدنى والحد الأقصى للقيم المطلوبة قليلاً أثناء عملية التكميم، لذلك يجب عليك دائمًا استخدام منافذ الإخراج كنطاق لإجراء المزيد من الحسابات. على سبيل المثال، إذا كانت القيم الدنيا والحد الأقصى المطلوبة قريبة من المساواة، فسيتم فصلهما بقيمة إبسيلون صغيرة لمنع إنشاء المخازن المؤقتة الكمية غير الصحيحة. بخلاف ذلك، يمكن أن ينتهي بك الأمر إلى وجود مخازن مؤقتة حيث يتم تعيين جميع القيم الكمية إلى نفس القيمة العائمة، مما يسبب مشاكل للعمليات التي يتعين عليها إجراء المزيد من الحسابات عليها.
الحجج:
- النطاق: كائن النطاق
- min_range: الحد الأدنى للقيمة العددية التي يمكن إنتاجها للإدخال.
- max_range: الحد الأقصى للقيمة العددية التي يمكن إنتاجها للإدخال.
العوائد:
- مخرجات
Output
: البيانات الكمية المنتجة من المدخلات العائمة. -
Output
: القيمة العددية الدنيا الفعلية المستخدمة للإخراج. -
Output
: القيمة العددية القصوى الفعلية المستخدمة للإخراج.
وظائف ثابتة العامة |
---|
Mode (StringPiece x) | |
RoundMode (StringPiece x) | |
الصفات العامة
عملية
Operation operation
الإخراج
::tensorflow::Output output
input_max
::tensorflow::Output output_max
input_min
::tensorflow::Output output_min
الوظائف العامة
كوانتزV2
QuantizeV2(
const ::tensorflow::Scope & scope,
::tensorflow::Input input,
::tensorflow::Input min_range,
::tensorflow::Input max_range,
DataType T
)
كوانتزV2
QuantizeV2(
const ::tensorflow::Scope & scope,
::tensorflow::Input input,
::tensorflow::Input min_range,
::tensorflow::Input max_range,
DataType T,
const QuantizeV2::Attrs & attrs
)
وظائف ثابتة العامة
وضع
Attrs Mode(
StringPiece x
)
وضع مستدير
Attrs RoundMode(
StringPiece x
)
إنّ محتوى هذه الصفحة مرخّص بموجب ترخيص Creative Commons Attribution 4.0 ما لم يُنصّ على خلاف ذلك، ونماذج الرموز مرخّصة بموجب ترخيص Apache 2.0. للاطّلاع على التفاصيل، يُرجى مراجعة سياسات موقع Google Developers. إنّ Java هي علامة تجارية مسجَّلة لشركة Oracle و/أو شركائها التابعين.
تاريخ التعديل الأخير: 2025-07-25 (حسب التوقيت العالمي المتفَّق عليه)
[null,null,["تاريخ التعديل الأخير: 2025-07-25 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],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```"]]