[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-01-13。"],[],[],null,["# Text embedding guide\n\nThe MediaPipe Text Embedder task lets you create a numeric representation of text data to\ncapture its semantic meaning. This functionality is frequently used to compare\nthe semantic similarity of two pieces of text using mathematical comparison\ntechniques such as Cosine Similarity. This task operates on text data with a\nmachine learning (ML) model, and outputs a numeric representation of the text\ndata as a list of high-dimensional feature vectors, also known as embedding\nvectors, in either floating-point or quantized form.\n\n[Try it!arrow_forward](https://mediapipe-studio.webapps.google.com/demo/text_embedder)\n\nGet Started\n-----------\n\nStart using this task by following one of these implementation guides for your\ntarget platform. These platform-specific guides walk you through a basic\nimplementation of this task, including a recommended model, and code example\nwith recommended configuration options:\n\n- **Android** - [Code\n example](https://github.com/google-ai-edge/mediapipe-samples/tree/main/examples/text_embedder/android) - [Guide](./android)\n- **Python** - [Code\n example](https://colab.sandbox.google.com/github/googlesamples/mediapipe/blob/main/examples/text_embedder/python/text_embedder.ipynb) - [Guide](./python)\n- **Web** - [Code example](https://codepen.io/mediapipe-preview/pen/XWBVZmE) - [Guide](./web_js)\n\nTask details\n------------\n\nThis section describes the capabilities, inputs, outputs, and configuration\noptions of this task.\n\n### Features\n\n- **Input text processing** - Supports out-of-graph tokenization for models without in-graph tokenization.\n- **Embedding similarity computation** - Built-in utility function to compute the [cosine similarity](https://en.wikipedia.org/wiki/Cosine_similarity) between two feature vectors.\n- **Quantization** - Supports scalar quantization for the feature vectors.\n\n| Task inputs | Task outputs |\n|---------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| Text Embedder accepts the following input data type: - String | Text Embedder outputs a list of embeddings consisting of: - Embedding: the feature vector itself, either in floating-point form or scalar-quantized. \u003c!-- --\u003e - Head index: the index for the head that produced this embedding. \u003c!-- --\u003e - Head name (optional): the name of the head that produced this embedding. |\n\n### Configurations options\n\nThis task has the following configuration options:\n\n| Option Name | Description | Value Range | Default Value |\n|----------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------|---------------|\n| `l2_normalize` | Whether to normalize the returned feature vector with L2 norm. Use this option only if the model does not already contain a native L2_NORMALIZATION TFLite Op. In most cases, this is already the case and L2 normalization is thus achieved through TFLite inference with no need for this option. | `Boolean` | `False` |\n| `quantize` | Whether the returned embedding should be quantized to bytes via scalar quantization. Embeddings are implicitly assumed to be unit-norm and therefore any dimension is guaranteed to have a value in \\[-1.0, 1.0\\]. Use the l2_normalize option if this is not the case. | `Boolean` | `False` |\n\nModels\n------\n\nWe offer a default, recommended model when you start developing with this task.\n| **Attention:** This MediaPipe Solutions Preview is an early release. [Learn more](/edge/mediapipe/solutions/about#notice).\n\n### Universal Sentence Encoder model (recommended)\n\nThis model uses a [dual encoder architecture](https://aclanthology.org/2022.emnlp-main.640.pdf)\nand was trained on various question-answer datasets.\n\nConsider the following pairs of sentences:\n\n- (\"it's a charming and often affecting journey\", \"what a great and fantastic trip\")\n- (\"I like my phone\", \"I hate my phone\")\n- (\"This restaurant has a great gimmick\", \"We need to double-check the details of our plan\")\n\nThe text embeddings in the first two pairs will have a higher cosine similarity\nthan the embeddings in the third pair because the first two pairs of sentences\nshare a common topic of \"trip sentiment\" and \"phone opinion\" respectively while\nthe third pair of sentences do not share a common topic.\n\nNote that although the two sentences in the second pair have opposing sentiments,\nthey have a high similarity score because they share a common topic.\n\n| Model name | Input shape | Quantization type | Versions |\n|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------|-------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------|\n| [Universal Sentence Encoder](https://storage.googleapis.com/mediapipe-models/text_embedder/universal_sentence_encoder/float32/latest/universal_sentence_encoder.tflite) | string, string, string | None (float32) | [Latest](https://storage.googleapis.com/mediapipe-models/text_embedder/universal_sentence_encoder/float32/latest/universal_sentence_encoder.tflite) |\n\nTask benchmarks\n---------------\n\nHere's the task benchmarks for the whole pipeline based on the above\npre-trained models. The latency result is the average latency on Pixel 6 using\nCPU / GPU.\n\n| Model Name | CPU Latency | GPU Latency |\n|----------------------------|-------------|-------------|\n| Universal Sentence Encoder | 18.21ms | - |"]]