HuggingFace镜像/GLuCoSE-base-ja-openmind
Model Introduction
Files & versions
Analysis

GLuCoSE (General Luke-based Contrastive Sentence Embedding)-base-Japanese

日本語のREADME/Japanese README

GLuCoSE (General LUke-based COntrastive Sentence Embedding, "glucose") is a Japanese text embedding model based on LUKE. In order to create a general-purpose, user-friendly Japanese text embedding model, GLuCoSE has been trained on a mix of web data and various datasets associated with natural language inference and search. This model is not only suitable for sentence vector similarity tasks but also for semantic search tasks.

  • Maximum token count: 512
  • Output dimension: 768
  • Pooling: mean pooling
  • Supported language: Japanese

Usage

You can use this model easily with sentence-transformers.

First, install sentence-transformers with pip as follows:

pip install -U sentence-transformers

You can load the model and convert sentences into dense vectors as shown below:

from openmind import AutoTokenizer, AutoModel, is_torch_npu_available
from openmind_hub import snapshot_download
import torch.nn.functional as F
from torch import Tensor
import openmind
import torch
import argparse

# Mean Pooling - Take attention mask into account for correct averaging
def mean_pooling(model_output, attention_mask):
    token_embeddings = model_output[0]  # First element of model_output contains all token embeddings
    input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
    return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--model_name_or_path",
        type=str,
        help="Path to model",
        default="jeffding/GLuCoSE-base-ja-openmind",
    )
    args = parser.parse_args()
    return args

def main():
    args = parse_args()
    model_path = args.model_name_or_path

    if is_torch_npu_available():
        device = "npu:0"
    else:
        device = "cpu"
        
    # Load model from HuggingFace Hub
    tokenizer = AutoTokenizer.from_pretrained(model_path)
    model = AutoModel.from_pretrained(model_path).to(device)
    sentences = [
    "PKSHA Technologyは機械学習/深層学習技術に関わるアルゴリズムソリューションを展開している。",
    "この深層学習モデルはPKSHA Technologyによって学習され、公開された。",
    "広目天は、仏教における四天王の一尊であり、サンスクリット語の「種々の眼をした者」を名前の由来とする。",
    ]
    # Tokenize sentences
    encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt').to(device)

    # Compute token embeddings
    with torch.no_grad():
        model_output = model(**encoded_input)
    # Perform pooling. In this case, mean pooling.
    sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
    print("Sentence embeddings:")
    print(sentence_embeddings)
    
if __name__ == "__main__":
    main()

Since the loss function used during training is cosine similarity, we recommend using cosine similarity for downstream tasks.

This text embedding model can also be used in LangChain. Please refer to this page for more information.

Resources Used

The following resources were used to train this model.

Pre-trained model

  • studio-ousia/luke-japanese-base-lite

Datasets

  • mC4
  • MQA
  • JNLI
  • JSNLI
  • PAWS-X
  • JSeM
  • MoritzLaurer/multilingual-NLI-26lang-2mil7
    • MultiNLI
    • WANLI
    • FeverNLI
    • LingNLI
  • JSICK
  • Mr.Tidy
  • JSTS (used for validation) 1

Benchmarks

Semantic Similarity Calculation (JSTS dev set)

Evaluation by Spearman's correlation coefficient and Pearson's correlation coefficient.

ModelSpearmanPearson
text-embedding-ada-0020.83720.7902
pkshatech/simcse-ja-bert-base-clcmlp30.8500.801
pkshatech/GLuCoSE-base-ja0.8640.818

Zero-shot Search (AIO3 dev set)

Evaluation by top-k retrieval accuracy4 (the fraction of questions that have a correct answer in the top-k retrieved documents at least once.)

ModelTop-1Top-5Top-10Top-50
text-embedding-ada-00233.5057.8065.1076.60
pkshatech/simcse-ja-bert-base-clcmlp330.6054.5062.5076.70
pkshatech/GLuCoSE-base-ja36.1059.4066.4078.30

Authors

Akihiko Fukuchi, Yuichiro Hoshino, Yotarow Watanabe

License

This model is published under the Apache License, Version 2.0.

Footnotes

  1. When we trained this model, the test data of JGLUE was not released, so we used the dev set of JGLUE as a private evaluation data. Therefore, we selected the checkpoint on the train set of JGLUE insted of its dev set. ↩

  2. https://qiita.com/akeyhero/items/ce371bfed64399027c23 ↩ ↩2

  3. This is the model we have released before. ↩ ↩2

  4. For more details, please refer to https://arxiv.org/pdf/2004.04906.pdf. ↩