Views
No views yet
pip install -U sentence-transformers1from sentence_transformers import SentenceTransformer
2sentences = ["This is an example sentence", "Each sentence is converted"]
3
4model = SentenceTransformer("bi-matrix/gmatrix-embedding")
5embeddings = model.encode(sentences)
6print(embeddings)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 = ['This is an example sentence', 'Each sentence is converted']
14
15# Load model from HuggingFace Hub
16tokenizer = AutoTokenizer.from_pretrained("bi-matrix/gmatrix-embedding")
17model = AutoModel.from_pretrained("bi-matrix/gmatrix-embedding")
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)| model | cosine_pearson | cosine_spearman | euclidean_pearson | euclidean_spearman | manhattan_pearson | manhattan_spearman | dot_pearson | dot_spearman |
|---|---|---|---|---|---|---|---|---|
| gmatrix-embedding | 85.77 | 86.30 | 84.82 | 85.29 | 84.84 | 85.33 | 83.19 | 83.19 |
| kf-deberta-multitask | 85.75 | 86.25 | 84.79 | 85.25 | 84.80 | 85.27 | 82.93 | 82.86 |
| ko-sroberta-multitask | 84.77 | 85.6 | 83.71 | 84.40 | 83.70 | 84.38 | 82.42 | 82.33 |
| ko-sbert-multitask | 84.13 | 84.71 | 82.42 | 82.66 | 82.41 | 82.69 | 80.05 | 79.69 |
| ko-sroberta-base-nli | 82.83 | 83.85 | 82.87 | 83.29 | 82.88 | 83.28 | 80.34 | 79.69 |
| ko-sbert-nli | 82.24 | 83.16 | 82.19 | 82.31 | 82.18 | 82.3 | 79.3 | 78.78 |
| ko-sroberta-sts | 81.84 | 81.82 | 81.15 | 81.25 | 81.14 | 81.25 | 79.09 | 78.54 |
| ko-sbert-sts | 81.55 | 81.23 | 79.94 | 79.79 | 79.9 | 79.75 | 76.02 | 75.31 |
| model | cosine_pearson | cosine_spearman | euclidean_pearson | euclidean_spearman | manhattan_pearson | manhattan_spearman | dot_pearson | dot_spearman |
|---|---|---|---|---|---|---|---|---|
| gmatrix-embedding | 75.86 | 65.75 | 72.65 | 65.20 | 72.48 | 65.32 | 64.71 | 53.90 |
| ko-sroberta-multitask | 71.78 | 63.16 | 70.80 | 63.47 | 70.89 | 63.72 | 53.57 | 44.23 |
| bge-m3 | 64.15 | 60.65 | 61.88 | 60.68 | 61.88 | 60.19 | 64.16 | 60.71 |

torch.utils.data.dataloader.DataLoader of length 329 with parameters:{'batch_size': 32, 'sampler': 'torch.utils.data.sampler.RandomSampler', 'batch_sampler': 'torch.utils.data.sampler.BatchSampler'}sentence_transformers.losses.CosineSimilarityLoss.CosineSimilarityLossSentenceTransformer(
(0): Transformer({'max_seq_length': 128, 'do_lower_case': True}) with Transformer model: DeBERTaV2Model
(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})
)