Views
No views yet
SentenceTransformer(
(0): Transformer({'max_seq_length': 8192, 'do_lower_case': False}) with Transformer model
(1): Pooling({'word_embedding_dimension': 768, '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/gte-korean-base", trust_remote_code=True)
5
6# Run inference
7sentences = [
8 '아이를 가진 엄마가 해변을 걷는다.',
9 '두 사람이 해변을 걷는다.',
10 '한 남자가 해변에서 개를 산책시킨다.',
11]
12embeddings = model.encode(sentences)
13print(embeddings.shape)
14# [3, 768]
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.6274, 0.3788],
22# [0.6274, 1.0000, 0.5978],
23# [0.3788, 0.5978, 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/gte-korean-base")
17model = AutoModel.from_pretrained("upskyy/gte-korean-base", trust_remote_code=True)
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.8681 |
| spearman_cosine | 0.8689 |
| pearson_manhattan | 0.7794 |
| spearman_manhattan | 0.7817 |
| pearson_euclidean | 0.781 |
| spearman_euclidean | 0.7836 |
| pearson_dot | 0.718 |
| spearman_dot | 0.7553 |
| pearson_max | 0.8681 |
| spearman_max | 0.8689 |
1@misc{zhang2024mgte,
2 title={mGTE: Generalized Long-Context Text Representation and Reranking Models for Multilingual Text Retrieval},
3 author={Xin Zhang and Yanzhao Zhang and Dingkun Long and Wen Xie and Ziqi Dai and Jialong Tang and Huan Lin and Baosong Yang and Pengjun Xie and Fei Huang and Meishan Zhang and Wenjie Li and Min Zhang},
4 year={2024},
5 eprint={2407.19669},
6 archivePrefix={arXiv},
7 primaryClass={cs.CL},
8 url={https://arxiv.org/abs/2407.19669},
9}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}