tensorflow:: אופס:: QuantizeV2
#include <array_ops.h>
כוונט את טנסור ה"קלט" מסוג float לטנסור "פלט" מסוג "T".
תַקצִיר
[min_range, max_range] הם צפים סקלאריים המציינים את הטווח עבור נתוני 'הקלט'. התכונה 'מצב' שולטת בדיוק באילו חישובים נעשה שימוש כדי להמיר את ערכי הציפה למקבילות הכמותיות שלהם. התכונה '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
output_min
::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
)
RoundMode
Attrs RoundMode(
StringPiece x
)
אלא אם צוין אחרת, התוכן של דף זה הוא ברישיון Creative Commons Attribution 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```"]]