|
|
|
# Text Embedding with Transformers
|
|
|
|
|
|
|
|
*author: Jael Gu and David Wang*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Desription
|
|
|
|
|
|
|
|
A REALM[1] text embedding operator implemented with pretrained models from [Huggingface Transformers](https://huggingface.co/docs/transformers).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
from towhee import ops
|
|
|
|
|
|
|
|
text_encoder = ops.text_embedding.realm('google/realm-cc-news-pretrained-encoder')
|
|
|
|
text_embedding = text_encoder("Hello, world.")
|
|
|
|
```
|
|
|
|
|
|
|
|
## Factory Constructor
|
|
|
|
|
|
|
|
Create the operator via the following factory method
|
|
|
|
|
|
|
|
***ops.text_embedding.realm(model_name)***
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Interface
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
|
|
**Parameters:**
|
|
|
|
|
|
|
|
***text***: *str*
|
|
|
|
|
|
|
|
The text in string.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
**Returns**: *numpy.ndarray*
|
|
|
|
|
|
|
|
The text embedding extracted by model.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Code Example
|
|
|
|
|
|
|
|
Use the pretrained model ('google/realm-cc-news-pretrained-encoder')
|
|
|
|
to generate a text embedding for the sentence "Hello, world.".
|
|
|
|
|
|
|
|
*Write the pipeline in simplified style*:
|
|
|
|
|
|
|
|
```python
|
|
|
|
import towhee.DataCollection as dc
|
|
|
|
|
|
|
|
dc.glob("Hello, world.")
|
|
|
|
.text_embedding.realm('google/realm-cc-news-pretrained-encoder')
|
|
|
|
.show()
|
|
|
|
```
|
|
|
|
|
|
|
|
*Write a same pipeline with explicit inputs/outputs name specifications:*
|
|
|
|
|
|
|
|
```python
|
|
|
|
from towhee import DataCollection as dc
|
|
|
|
|
|
|
|
dc.glob['text']('Hello, world.')
|
|
|
|
.text_embedding.realm['text', 'vec']('bert-base-cased')
|
|
|
|
.select('vec')
|
|
|
|
.show()
|
|
|
|
```
|
|
|
|
|
|
|
|
[1] https://arxiv.org/abs/2002.08909
|