模型:
sentence-transformers/allenai-specter
该模型是将 AllenAI SPECTER 模型转换为 sentence-transformers 模型的变换模型。它可以用于将科学出版物的标题和摘要映射到向量空间,使得相似的论文靠近。
安装 sentence-transformers 后,使用该模型非常简单:
pip install -U sentence-transformers
然后你可以这样使用该模型:
from sentence_transformers import SentenceTransformer sentences = ["This is an example sentence", "Each sentence is converted"] model = SentenceTransformer('sentence-transformers/allenai-specter') embeddings = model.encode(sentences) print(embeddings)
如果没有 sentence-transformers ,你可以这样使用该模型:首先,将输入传递给变换模型,然后必须在上下文化的词向量之上应用正确的汇集操作。
from transformers import AutoTokenizer, AutoModel import torch def cls_pooling(model_output, attention_mask): return model_output[0][:,0] # Sentences we want sentence embeddings for sentences = ['This is an example sentence', 'Each sentence is converted'] # Load model from HuggingFace Hub tokenizer = AutoTokenizer.from_pretrained('sentence-transformers/allenai-specter') model = AutoModel.from_pretrained('sentence-transformers/allenai-specter') # Tokenize sentences encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt') # Compute token embeddings with torch.no_grad(): model_output = model(**encoded_input) # Perform pooling. In this case, max pooling. sentence_embeddings = cls_pooling(model_output, encoded_input['attention_mask']) print("Sentence embeddings:") print(sentence_embeddings)
有关该模型的自动化评估,请参见句子嵌入基准(Sentence Embeddings Benchmark): https://seb.sbert.net
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}) )