Views
No views yet
pip install -U sentence-transformers1from sentence_transformers import SentenceTransformer
2sentences = ["ሓደ ሰብኣይ ፈረስ ይጋልብ ኣሎ።", "ሓንቲ ጓል ክራር ትጻወት ኣላ።"]
3
4model = SentenceTransformer('fgaim/tiroberta-bi-encoder')
5embeddings = model.encode(sentences)
6print(embeddings)1import torch
2from transformers import AutoModel, AutoTokenizer
3
4
5# Mean Pooling - Take attention mask into account for correct averaging
6def mean_pooling(model_output, attention_mask):
7 token_embeddings = model_output[0] # First element of model_output contains all token embeddings
8 input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
9 return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
10
11
12# Sentences we want sentence embeddings for
13sentences = ["ሓደ ሰብኣይ ፈረስ ይጋልብ ኣሎ።", "ሓንቲ ጓል ክራር ትጻወት ኣላ።"]
14
15# Load model from HuggingFace Hub
16tokenizer = AutoTokenizer.from_pretrained("fgaim/tiroberta-bi-encoder")
17model = AutoModel.from_pretrained("fgaim/tiroberta-bi-encoder")
18
19# Tokenize sentences
20encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors="pt")
21
22# Compute token embeddings
23with torch.no_grad():
24 model_output = model(**encoded_input)
25
26# Perform pooling. In this case, mean pooling.
27sentence_embeddings = mean_pooling(model_output, encoded_input["attention_mask"])
28
29print("Sentence embeddings:", sentence_embeddings)| Model Size | Layers | Attn. Heads | Hidden Size | FFN | Parameters | Max. Seq |
|---|---|---|---|---|---|---|
| BASE | 12 | 12 | 768 | 3072 | 125M | 512 |
5127681SentenceTransformer(
2 Transformer(
3 {
4 'max_seq_length': 512,
5 'do_lower_case': False
6 }
7 ) # with Transformer model: RobertaModel
8
9 Pooling(
10 {
11 'word_embedding_dimension': 768,
12 'pooling_mode_cls_token': False,
13 'pooling_mode_mean_tokens': True,
14 'pooling_mode_max_tokens': False,
15 'pooling_mode_mean_sqrt_len_tokens': False,
16 'pooling_mode_weightedmean_tokens': False,
17 'pooling_mode_lasttoken': False,
18 'include_prompt': True,
19 }
20 )
21)1@misc{gaim-2024-semantic-search,
2 title = {{Semantic Search Models for Tigrinya}},
3 author = {Fitsum Gaim},
4 month = {January},
5 year = {2024},
6 publisher = {Hugging Face Hub},
7 doi = {10.57967/hf/6068},
8 url = {https://huggingface.co/fgaim/tiroberta-bi-encoder}
9}