Views
No views yet
pip install -U fugashi[unidic-lite] sentence-transformers1from sentence_transformers import SentenceTransformer
2sentences = ["こんにちは、世界!", "文埋め込み最高!文埋め込み最高と叫びなさい", "極度乾燥しなさい"]
3
4model = SentenceTransformer("cl-nagoya/unsup-simcse-ja-large")
5embeddings = model.encode(sentences)
6print(embeddings)1from transformers import AutoTokenizer, AutoModel
2import torch
3
4
5def cls_pooling(model_output, attention_mask):
6 return model_output[0][:,0]
7
8
9# Sentences we want sentence embeddings for
10sentences = ['This is an example sentence', 'Each sentence is converted']
11
12# Load model from HuggingFace Hub
13tokenizer = AutoTokenizer.from_pretrained("cl-nagoya/unsup-simcse-ja-large")
14model = AutoModel.from_pretrained("cl-nagoya/unsup-simcse-ja-large")
15
16# Tokenize sentences
17encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
18
19# Compute token embeddings
20with torch.no_grad():
21 model_output = model(**encoded_input)
22
23# Perform pooling. In this case, cls pooling.
24sentence_embeddings = cls_pooling(model_output, encoded_input['attention_mask'])
25
26print("Sentence embeddings:")
27print(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})
)@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}}
}