1from sentence_transformers import SentenceTransformer
2import torch.nn.functional as F
3
4# Download from the 🤗 Hub
5model = SentenceTransformer("pkshatech/GLuCoSE-base-ja-v2")
6
7# Each input text should start with "query: " or "passage: ".
8# For tasks other than retrieval, you can simply use the "query: " prefix.
9sentences = [
10 'query: PKSHAはどんな会社ですか?',
11 'passage: 研究開発したアルゴリズムを、多くの企業のソフトウエア・オペレーションに導入しています。',
12 'query: 日本で一番高い山は?',
13 'passage: 富士山(ふじさん)は、標高3776.12 m、日本最高峰(剣ヶ峰)の独立峰で、その優美な風貌は日本国外でも日本の象徴として広く知られている。',
14]
15embeddings = model.encode(sentences,convert_to_tensor=True)
16print(embeddings.shape)
17# [4, 768]
18
19# Get the similarity scores for the embeddings
20similarities = F.cosine_similarity(embeddings.unsqueeze(0), embeddings.unsqueeze(1), dim=2)
21print(similarities)
22# [[1.0000, 0.6050, 0.4341, 0.5537],
23# [0.6050, 1.0000, 0.5018, 0.6815],
24# [0.4341, 0.5018, 1.0000, 0.7534],
25# [0.5537, 0.6815, 0.7534, 1.0000]]
261import torch.nn.functional as F
2from torch import Tensor
3from transformers import AutoTokenizer, AutoModel
4
5def mean_pooling(last_hidden_states: Tensor,attention_mask: Tensor) -> Tensor:
6 emb = last_hidden_states * attention_mask.unsqueeze(-1)
7 emb = emb.sum(dim=1) / attention_mask.sum(dim=1).unsqueeze(-1)
8 return emb
9
10# Download from the 🤗 Hub
11tokenizer = AutoTokenizer.from_pretrained("pkshatech/GLuCoSE-base-ja-v2")
12model = AutoModel.from_pretrained("pkshatech/GLuCoSE-base-ja-v2")
13
14# Each input text should start with "query: " or "passage: ".
15# For tasks other than retrieval, you can simply use the "query: " prefix.
16sentences = [
17 'query: PKSHAはどんな会社ですか?',
18 'passage: 研究開発したアルゴリズムを、多くの企業のソフトウエア・オペレーションに導入しています。',
19 'query: 日本で一番高い山は?',
20 'passage: 富士山(ふじさん)は、標高3776.12 m、日本最高峰(剣ヶ峰)の独立峰で、その優美な風貌は日本国外でも日本の象徴として広く知られている。',
21]
22
23# Tokenize the input texts
24batch_dict = tokenizer(sentences, max_length=512, padding=True, truncation=True, return_tensors='pt')
25
26outputs = model(**batch_dict)
27embeddings = mean_pooling(outputs.last_hidden_state, batch_dict['attention_mask'])
28print(embeddings.shape)
29# [4, 768]
30
31# Get the similarity scores for the embeddings
32similarities = F.cosine_similarity(embeddings.unsqueeze(0), embeddings.unsqueeze(1), dim=2)
33print(similarities)
34# [[1.0000, 0.6050, 0.4341, 0.5537],
35# [0.6050, 1.0000, 0.5018, 0.6815],
36# [0.4341, 0.5018, 1.0000, 0.7534],
37# [0.5537, 0.6815, 0.7534, 1.0000]]
38| Model | Size | MIRACL Recall@5 | JQaRA nDCG@10 | JaCWIR MAP@10 | MLDR nDCG@10 |
|---|---|---|---|---|---|
| intfloat/multilingual-e5-large | 0.6B | 89.2 | 55.4 | 87.6 | 29.8 |
| cl-nagoya/ruri-large | 0.3B | 78.7 | 62.4 | 85.0 | 37.5 |
| intfloat/multilingual-e5-base | 0.3B | 84.2 | 47.2 | 85.3 | 25.4 |
| cl-nagoya/ruri-base | 0.1B | 74.3 | 58.1 | 84.6 | 35.3 |
| pkshatech/GLuCoSE-base-ja | 0.1B | 53.3 | 30.8 | 68.6 | 25.2 |
| GLuCoSE v2 | 0.1B | 85.5 | 60.6 | 85.3 | 33.8 |
| Model | Size | Avg. | Retrieval | STS | Classification | Reranking | Clustering | PairClassification |
|---|---|---|---|---|---|---|---|---|
| OpenAI/text-embedding-3-small | - | 69.18 | 66.39 | 79.46 | 73.06 | 92.92 | 51.06 | 62.27 |
| OpenAI/text-embedding-3-large | - | 74.05 | 74.48 | 82.52 | 77.58 | 93.58 | 53.32 | 62.35 |
| intfloat/multilingual-e5-large | 0.6B | 70.90 | 70.98 | 79.70 | 72.89 | 92.96 | 51.24 | 62.15 |
| cl-nagoya/ruri-large | 0.3B | 73.31 | 73.02 | 83.13 | 77.43 | 92.99 | 51.82 | 62.29 |
| intfloat/multilingual-e5-base | 0.3B | 68.61 | 68.21 | 79.84 | 69.30 | 92.85 | 48.26 | 62.26 |
| cl-nagoya/ruri-base | 0.1B | 71.91 | 69.82 | 82.87 | 75.58 | 92.91 | 54.16 | 62.38 |
| pkshatech/GLuCoSE-base-ja | 0.1B | 67.29 | 59.02 | 78.71 | 76.82 | 91.90 | 49.78 | 66.39 |
| GLuCoSE v2 | 0.1B | 72.23 | 73.36 | 82.96 | 74.21 | 93.01 | 48.65 | 62.37 |