Views
No views yet
[CLS] embeddings can be used as a sentence representation aligned between Russian and English.[CLS] embeddings distilled from LaBSE, rubert-base-cased-sentence, Laser and USE.1# pip install transformers sentencepiece
2import torch
3from transformers import AutoTokenizer, AutoModel
4tokenizer = AutoTokenizer.from_pretrained("cointegrated/rubert-tiny")
5model = AutoModel.from_pretrained("cointegrated/rubert-tiny")
6# model.cuda() # uncomment it if you have a GPU
7
8def embed_bert_cls(text, model, tokenizer):
9 t = tokenizer(text, padding=True, truncation=True, return_tensors='pt')
10 with torch.no_grad():
11 model_output = model(**{k: v.to(model.device) for k, v in t.items()})
12 embeddings = model_output.last_hidden_state[:, 0, :]
13 embeddings = torch.nn.functional.normalize(embeddings)
14 return embeddings[0].cpu().numpy()
15
16print(embed_bert_cls('привет мир', model, tokenizer).shape)
17# (312,)