ai-forever commited on
Commit
284355c
1 Parent(s): ce90b89

Upload README.md

Browse files
Files changed (1) hide show
  1. README.md +53 -0
README.md ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language:
3
+ - ru
4
+ tags:
5
+ - PyTorch
6
+ - Transformers
7
+ ---
8
+
9
+ # BERT large model (uncased) for Sentence Embeddings in Russian language.
10
+ The model is described [in this article](https://habr.com/ru/company/sberdevices/blog/527576/)
11
+ For better quality, use mean token embeddings.
12
+
13
+ ## Usage (HuggingFace Models Repository)
14
+
15
+ You can use the model directly from the model repository to compute sentence embeddings:
16
+ ```python
17
+ from transformers import AutoTokenizer, AutoModel
18
+ import torch
19
+
20
+
21
+ #Mean Pooling - Take attention mask into account for correct averaging
22
+ def mean_pooling(model_output, attention_mask):
23
+ token_embeddings = model_output[0] #First element of model_output contains all token embeddings
24
+ input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
25
+ sum_embeddings = torch.sum(token_embeddings * input_mask_expanded, 1)
26
+ sum_mask = torch.clamp(input_mask_expanded.sum(1), min=1e-9)
27
+ return sum_embeddings / sum_mask
28
+
29
+
30
+
31
+ #Sentences we want sentence embeddings for
32
+ sentences = ['Привет! Как твои дела?',
33
+ 'А правда, что 42 твое любимое число?']
34
+
35
+ #Load AutoModel from huggingface model repository
36
+ tokenizer = AutoTokenizer.from_pretrained("ai-forever/sbert_large_nlu_ru")
37
+ model = AutoModel.from_pretrained("ai-forever/sbert_large_nlu_ru")
38
+
39
+ #Tokenize sentences
40
+ encoded_input = tokenizer(sentences, padding=True, truncation=True, max_length=24, return_tensors='pt')
41
+
42
+ #Compute token embeddings
43
+ with torch.no_grad():
44
+ model_output = model(**encoded_input)
45
+
46
+ #Perform pooling. In this case, mean pooling
47
+ sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
48
+ ```
49
+
50
+ # Authors
51
+ + [SberDevices](https://sberdevices.ru/) Team.
52
+ + Aleksandr Abramov: [HF profile](https://huggingface.co/Andrilko), [Github](https://github.com/Ab1992ao), [Kaggle Competitions Master](https://www.kaggle.com/andrilko);
53
+ + Denis Antykhov: [Github](https://github.com/gaphex);