Ibragim commited on
Commit
a23020b
1 Parent(s): 6ef8561
Files changed (1) hide show
  1. README.md +40 -0
README.md ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ BERT large model (uncased) for Sentence Embeddings in Russian language.
2
+ The model is described in this article](https://habr.com/ru/company/sberdevices/blog/527576/)
3
+ For better quality, use mean token embeddings.
4
+
5
+ ## Usage (HuggingFace Models Repository)
6
+
7
+ You can use the model directly from the model repository to compute sentence embeddings:
8
+ ```python
9
+ from transformers import AutoTokenizer, AutoModel
10
+ import torch
11
+
12
+
13
+ #Mean Pooling - Take attention mask into account for correct averaging
14
+ def mean_pooling(model_output, attention_mask):
15
+ token_embeddings = model_output[0] #First element of model_output contains all token embeddings
16
+ input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
17
+ sum_embeddings = torch.sum(token_embeddings * input_mask_expanded, 1)
18
+ sum_mask = torch.clamp(input_mask_expanded.sum(1), min=1e-9)
19
+ return sum_embeddings / sum_mask
20
+
21
+
22
+
23
+ #Sentences we want sentence embeddings for
24
+ sentences = ['Привет! Как твои дела?',
25
+ 'А правда, что 42 твое любимое число?']
26
+
27
+ #Load AutoModel from huggingface model repository
28
+ tokenizer = AutoTokenizer.from_pretrained("sberbank-ai/sbert_large_nlu_ru")
29
+ model = AutoModel.from_pretrained("sberbank-ai/sbert_large_nlu_ru")
30
+
31
+ #Tokenize sentences
32
+ encoded_input = tokenizer(sentences, padding=True, truncation=True, max_length=24, return_tensors='pt')
33
+
34
+ #Compute token embeddings
35
+ with torch.no_grad():
36
+ model_output = model(**encoded_input)
37
+
38
+ #Perform pooling. In this case, mean pooling
39
+ sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
40
+ ```