Views
No views yet
SentenceTransformer(
(0): Transformer({'max_seq_length': 8192, 'do_lower_case': False}) with Transformer model: XLMRobertaModel
(1): Pooling({'word_embedding_dimension': 1024, 'pooling_mode_cls_token': False, 'pooling_mode_mean_tokens': True, 'pooling_mode_max_tokens': False, 'pooling_mode_mean_sqrt_len_tokens': False, 'pooling_mode_weightedmean_tokens': False, 'pooling_mode_lasttoken': False, 'include_prompt': True})
)pip install -U sentence-transformers1from sentence_transformers import SentenceTransformer
2
3# Download from the 🤗 Hub
4model = SentenceTransformer("upskyy/bge-m3-korean")
5
6# Run inference
7sentences = [
8 '아이를 가진 엄마가 해변을 걷는다.',
9 '두 사람이 해변을 걷는다.',
10 '한 남자가 해변에서 개를 산책시킨다.',
11]
12embeddings = model.encode(sentences)
13print(embeddings.shape)
14# [3, 1024]
15
16# Get the similarity scores for the embeddings
17similarities = model.similarity(embeddings, embeddings)
18print(similarities.shape)
19# [3, 3]
20print(similarities)
21# tensor([[1.0000, 0.6173, 0.3672],
22# [0.6173, 1.0000, 0.4775],
23# [0.3672, 0.4775, 1.0000]])1from transformers import AutoTokenizer, AutoModel
2import torch
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("upskyy/bge-m3-korean")
17model = AutoModel.from_pretrained("upskyy/bge-m3-korean")
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:")
30print(sentence_embeddings)sts-devEmbeddingSimilarityEvaluator| Metric | Value |
|---|---|
| pearson_cosine | 0.874 |
| spearman_cosine | 0.8724 |
| pearson_manhattan | 0.8593 |
| spearman_manhattan | 0.8688 |
| pearson_euclidean | 0.8598 |
| spearman_euclidean | 0.8694 |
| pearson_dot | 0.8684 |
| spearman_dot | 0.8666 |
| pearson_max | 0.874 |
| spearman_max | 0.8724 |
1@misc{bge-m3,
2 title={BGE M3-Embedding: Multi-Lingual, Multi-Functionality, Multi-Granularity Text Embeddings Through Self-Knowledge Distillation},
3 author={Jianlv Chen and Shitao Xiao and Peitian Zhang and Kun Luo and Defu Lian and Zheng Liu},
4 year={2024},
5 eprint={2402.03216},
6 archivePrefix={arXiv},
7 primaryClass={cs.CL}
8}1@inproceedings{reimers-2019-sentence-bert,
2 title = "Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks",
3 author = "Reimers, Nils and Gurevych, Iryna",
4 booktitle = "Proceedings of the 2019 Conference on Empirical Methods in Natural Language Processing",
5 month = "11",
6 year = "2019",
7 publisher = "Association for Computational Linguistics",
8 url = "https://arxiv.org/abs/1908.10084",
9}