logo
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Readme
Files and versions

77 lines
1.7 KiB

# Text Embedding with Transformers
*author: Jael Gu*
<br />
## Desription
A text embedding operator takes a sentence, paragraph, or document in string as an input
and output an embedding vector in ndarray which captures the input's core semantic elements.
This operator uses the REALM model, which is a retrieval-augmented language model that firstly retrieves documents from a textual knowledge corpus and then utilizes retrieved documents to process question answering tasks. [1]
The original model was proposed in REALM: Retrieval-Augmented Language Model Pre-Training by Kelvin Guu, Kenton Lee, Zora Tung, Panupong Pasupat and Ming-Wei Chang.[2]
### References
[1].https://huggingface.co/docs/transformers/model_doc/realm
[2].https://arxiv.org/abs/2002.08909
<br />
## Code Example
Use the pretrained model "google/realm-cc-news-pretrained-embedder"
to generate a text embedding for the sentence "Hello, world.".
*Write the pipeline*:
```python
import towhee
towhee.dc(["Hello, world."]) \
.text_embedding.realm(model_name="google/realm-cc-news-pretrained-embedder")
```
<br />
## Factory Constructor
Create the operator via the following factory method
***text_embedding.transformers(model_name="google/realm-cc-news-pretrained-embedder")***
**Parameters:**
***model_name***: *str*
The model name in string.
The default value is "google/realm-cc-news-pretrained-embedder".
Supported model names:
- "google/realm-cc-news-pretrained-embedder"
<br />
## Interface
The operator takes a text in string as input.
It loads tokenizer and pre-trained model using model name.
and then return text embedding in ndarray.
**Parameters:**
***text***: *str*
The text in string.
**Returns**:
*numpy.ndarray*
​ The text embedding extracted by model.