جریان تنسور:: عملیات:: QuantizeV2
#include <array_ops.h>
تانسور «ورودی» از نوع شناور را به تانسور «خروجی» از نوع «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 مشخص شوند. کوانتیز کردن از float به 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 مثال
حالت 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)
یکی از مواردی که باید مراقب آن باشید این است که اپراتور ممکن است مقادیر حداقل و حداکثر درخواستی را کمی در طول فرآیند کوانتیزاسیون تنظیم کند، بنابراین شما باید همیشه از پورت های خروجی به عنوان محدوده برای محاسبات بیشتر استفاده کنید. به عنوان مثال، اگر مقادیر حداقل و حداکثر درخواستی نزدیک به مساوی باشند، آنها با یک مقدار اپسیلون کوچک از هم جدا می شوند تا از ایجاد بافرهای کوانتیزه نادرست جلوگیری شود. در غیر این صورت، می توانید با بافرهایی مواجه شوید که در آن همه مقادیر کوانتیزه شده به یک مقدار شناور نشان داده می شوند، که باعث ایجاد مشکلاتی برای عملیاتی می شود که باید محاسبات بیشتری را روی آنها انجام دهند.
استدلال ها:
- scope: یک شی Scope
- min_range: حداقل مقدار اسکالری که احتمالاً برای ورودی تولید می شود.
- max_range: حداکثر مقدار اسکالری که احتمالاً برای ورودی تولید می شود.
برمیگرداند:
- خروجی
Output
: داده های کوانتیزه شده تولید شده از ورودی شناور. -
Output
output_min: حداقل مقدار واقعی اسکالر مورد استفاده برای خروجی. -
Output
output_max: حداکثر مقدار واقعی اسکالر مورد استفاده برای خروجی.
توابع استاتیک عمومی |
---|
Mode (StringPiece x) | |
RoundMode (StringPiece x) | |
صفات عمومی
عملیات
Operation operation
خروجی
::tensorflow::Output output
output_max
::tensorflow::Output output_max
خروجی_دقیقه
::tensorflow::Output output_min
توابع عمومی
QuantizeV2
QuantizeV2(
const ::tensorflow::Scope & scope,
::tensorflow::Input input,
::tensorflow::Input min_range,
::tensorflow::Input max_range,
DataType T
)
QuantizeV2
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 License است. نمونه کدها نیز دارای مجوز Apache 2.0 License است. برای اطلاع از جزئیات، به خطمشیهای سایت Google Developers مراجعه کنید. جاوا علامت تجاری ثبتشده Oracle و/یا شرکتهای وابسته به آن است.
تاریخ آخرین بهروزرسانی 2025-07-27 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-07-27 بهوقت ساعت هماهنگ جهانی."],[],[],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/r2.0/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/r2.0/api_docs/cc/class/tensorflow/output#classtensorflow_1_1_output) output: The quantized data produced from the float input.\n- [Output](/versions/r2.0/api_docs/cc/class/tensorflow/output#classtensorflow_1_1_output) output_min: The actual minimum scalar value used for the output.\n- [Output](/versions/r2.0/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/r2.0/api_docs/cc/class/tensorflow/scope#classtensorflow_1_1_scope)` & scope, ::`[tensorflow::Input](/versions/r2.0/api_docs/cc/class/tensorflow/input#classtensorflow_1_1_input)` input, ::`[tensorflow::Input](/versions/r2.0/api_docs/cc/class/tensorflow/input#classtensorflow_1_1_input)` min_range, ::`[tensorflow::Input](/versions/r2.0/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/r2.0/api_docs/cc/class/tensorflow/scope#classtensorflow_1_1_scope)` & scope, ::`[tensorflow::Input](/versions/r2.0/api_docs/cc/class/tensorflow/input#classtensorflow_1_1_input)` input, ::`[tensorflow::Input](/versions/r2.0/api_docs/cc/class/tensorflow/input#classtensorflow_1_1_input)` min_range, ::`[tensorflow::Input](/versions/r2.0/api_docs/cc/class/tensorflow/input#classtensorflow_1_1_input)` max_range, DataType T, const `[QuantizeV2::Attrs](/versions/r2.0/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/r2.0/api_docs/cc/class/tensorflow/operation#classtensorflow_1_1_operation) |\n| [output](#classtensorflow_1_1ops_1_1_quantize_v2_1ada6c67087a12267be45aac6a9120de55) | `::`[tensorflow::Output](/versions/r2.0/api_docs/cc/class/tensorflow/output#classtensorflow_1_1_output) |\n| [output_max](#classtensorflow_1_1ops_1_1_quantize_v2_1a79cc3f023f74de524cd442b0492e4dae) | `::`[tensorflow::Output](/versions/r2.0/api_docs/cc/class/tensorflow/output#classtensorflow_1_1_output) |\n| [output_min](#classtensorflow_1_1ops_1_1_quantize_v2_1a7690769886d844fc55484420e1c43b65) | `::`[tensorflow::Output](/versions/r2.0/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/r2.0/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/r2.0/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/r2.0/api_docs/cc/struct/tensorflow/ops/quantize-v2/attrs) | Optional attribute setters for [QuantizeV2](/versions/r2.0/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```"]]