🚀 unsup-simcse-ja-large
本模型 unsup-simcse-ja-large
可用于句子特征提取和相似度计算,借助预训练的Transformer架构,能高效地将句子转换为向量表示。
🚀 快速开始
使用此模型,你可以选择使用 sentence-transformers
库或 HuggingFace Transformers
库,以下为你详细介绍两种使用方式。
📦 安装指南
若你选择使用 sentence-transformers
库,可通过以下命令进行安装:
pip install -U fugashi[unidic-lite] sentence-transformers
💻 使用示例
基础用法(Sentence-Transformers)
from sentence_transformers import SentenceTransformer
sentences = ["こんにちは、世界!", "文埋め込み最高!文埋め込み最高と叫びなさい", "極度乾燥しなさい"]
model = SentenceTransformer("cl-nagoya/unsup-simcse-ja-large")
embeddings = model.encode(sentences)
print(embeddings)
高级用法(HuggingFace Transformers)
若不使用 sentence-transformers
库,你可以按如下方式使用该模型:首先将输入数据传入Transformer模型,然后对上下文词嵌入应用合适的池化操作。
from transformers import AutoTokenizer, AutoModel
import torch
def cls_pooling(model_output, attention_mask):
return model_output[0][:,0]
sentences = ['This is an example sentence', 'Each sentence is converted']
tokenizer = AutoTokenizer.from_pretrained("cl-nagoya/unsup-simcse-ja-large")
model = AutoModel.from_pretrained("cl-nagoya/unsup-simcse-ja-large")
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
with torch.no_grad():
model_output = model(**encoded_input)
sentence_embeddings = cls_pooling(model_output, encoded_input['attention_mask'])
print("Sentence embeddings:")
print(sentence_embeddings)
🔧 技术细节
完整模型架构
SentenceTransformer(
(0): Transformer({'max_seq_length': 512, 'do_lower_case': False}) with Transformer model: BertModel
(1): Pooling({'word_embedding_dimension': 768, 'pooling_mode_cls_token': True, 'pooling_mode_mean_tokens': False, 'pooling_mode_max_tokens': False, 'pooling_mode_mean_sqrt_len_tokens': False})
)
模型概要
更多详细的实验设置,请参考 GitHub仓库。
📄 许可证
本模型采用 cc-by-sa-4.0
许可证。
📚 引用与作者
若你使用了此模型,请按照以下格式进行引用:
@misc{
hayato-tsukagoshi-2023-simple-simcse-ja,
author = {Hayato Tsukagoshi},
title = {Japanese Simple-SimCSE},
year = {2023},
publisher = {GitHub},
journal = {GitHub repository},
howpublished = {\url{https://github.com/hppRC/simple-simcse-ja}}
}