Feature Extraction
sentence-transformers
Safetensors
Transformers
Russian
English
roberta
mteb
Eval Results (legacy)
text-embeddings-inference
Instructions to use ai-forever/ru-en-RoSBERTa with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sentence-transformers
How to use ai-forever/ru-en-RoSBERTa with sentence-transformers:
from sentence_transformers import SentenceTransformer model = SentenceTransformer("ai-forever/ru-en-RoSBERTa") sentences = [ "The weather is lovely today.", "It's so sunny outside!", "He drove to the stadium." ] embeddings = model.encode(sentences) similarities = model.similarity(embeddings, embeddings) print(similarities.shape) # [3, 3] - Transformers
How to use ai-forever/ru-en-RoSBERTa with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("feature-extraction", model="ai-forever/ru-en-RoSBERTa")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModel tokenizer = AutoTokenizer.from_pretrained("ai-forever/ru-en-RoSBERTa") model = AutoModel.from_pretrained("ai-forever/ru-en-RoSBERTa", device_map="auto") - Inference
- Notebooks
- Google Colab
- Kaggle
Update README.md
Browse files
README.md
CHANGED
|
@@ -1,3 +1,52 @@
|
|
| 1 |
-
---
|
| 2 |
-
license: mit
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
language:
|
| 4 |
+
- ru
|
| 5 |
+
- en
|
| 6 |
+
tags:
|
| 7 |
+
- PyTorch
|
| 8 |
+
- Transformers
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
# ru-en-RoBERTa-large model for Sentence Embeddings in Russian and English language.
|
| 12 |
+
The model is described [in this article](<link of our arxiv>)
|
| 13 |
+
Russian MTEB [metrics](<lin of our ruMTEB>)
|
| 14 |
+
|
| 15 |
+
For better quality, use mean token embeddings.
|
| 16 |
+
## Usage (HuggingFace Models Repository)
|
| 17 |
+
You can use the model directly from the model repository to compute sentence embeddings:
|
| 18 |
+
```python
|
| 19 |
+
from transformers import AutoTokenizer, AutoModel
|
| 20 |
+
import torch
|
| 21 |
+
|
| 22 |
+
#You might to use two mode for embeddings creation: CLS token embs or MEAN Pooling
|
| 23 |
+
|
| 24 |
+
#Mean Pooling example - Take attention mask into account for correct averaging
|
| 25 |
+
def mean_pooling(model_output, attention_mask):
|
| 26 |
+
token_embeddings = model_output[0] #First element of model_output contains all token embeddings
|
| 27 |
+
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
|
| 28 |
+
sum_embeddings = torch.sum(token_embeddings * input_mask_expanded, 1)
|
| 29 |
+
sum_mask = torch.clamp(input_mask_expanded.sum(1), min=1e-9)
|
| 30 |
+
return sum_embeddings / sum_mask
|
| 31 |
+
|
| 32 |
+
#Sentences we want sentence embeddings for
|
| 33 |
+
sentences = ['Привет! Как твои дела?',
|
| 34 |
+
'А правда, что 42 твое любимое число?']
|
| 35 |
+
#Load AutoModel from huggingface model repository
|
| 36 |
+
tokenizer = AutoTokenizer.from_pretrained("ai-forever/ru-en-RoSBERTa")
|
| 37 |
+
model = AutoModel.from_pretrained("ai-forever/ru-en-RoSBERTa")
|
| 38 |
+
#Tokenize sentences
|
| 39 |
+
encoded_input = tokenizer(sentences, padding=True, truncation=True, max_length=512, return_tensors='pt')
|
| 40 |
+
|
| 41 |
+
#Compute token embeddings
|
| 42 |
+
with torch.no_grad():
|
| 43 |
+
model_output = model(**encoded_input)
|
| 44 |
+
|
| 45 |
+
#In this case, mean pooling
|
| 46 |
+
sentence_mean_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
|
| 47 |
+
|
| 48 |
+
#In this case, cls "pooling"
|
| 49 |
+
last_hidden_states = model_output[0]
|
| 50 |
+
sentence_cls_embeddings = last_hidden_states[:,0]
|
| 51 |
+
|
| 52 |
+
```
|