模型:
这是一个经过微调的模型,特别针对科学论文文档进行训练的。
当你安装好了 sentence-transformers 后,使用这个模型就很简单:
pip install -U sentence-transformers
然后你可以像这样使用模型:
from sentence_transformers import SentenceTransformer
sentences = [
"Human-Computer Interaction (HCI) can enhance emotion regulation training",
"One of the most effective emotion regulation in youth is through cognitive reapproval",
"Business analytics is an extensive form of using data"
]
model = SentenceTransformer('salsabiilashifa11/sbert-paper')
embeddings = model.encode(sentences)
print(embeddings)
如果没有 sentence-transformers ,你可以像下面这样使用模型:首先,你将输入数据通过变换模型,然后在上下文词嵌入之上应用正确的池化操作。
from transformers import AutoTokenizer, AutoModel
import torch
import torch.nn.functional as F
#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)
# Sentences we want sentence embeddings for
sentences = [
"Human-Computer Interaction (HCI) can enhance emotion regulation training",
"One of the most effective emotion regulation in youth is through cognitive reapproval",
"Business analytics is an extensive form of using data"
]
# Load model from HuggingFace Hub
tokenizer = AutoTokenizer.from_pretrained('sentence-transformers/all-mpnet-base-v2')
model = AutoModel.from_pretrained('salsabiilashifa11/sbert-paper')
# 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
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
# Normalize embeddings
sentence_embeddings = F.normalize(sentence_embeddings, p=2, dim=1)
print("Sentence embeddings:")
print(sentence_embeddings)
该模型的训练使用了以下参数:
数据加载器 :
sentence_transformers.datasets.NoDuplicatesDataLoader.NoDuplicatesDataLoader,长度为200,参数如下:
{'batch_size': 8}
损失函数:
sentence_transformers.losses.MultipleNegativesRankingLoss.MultipleNegativesRankingLoss,参数如下:
{'scale': 20.0, 'similarity_fct': 'cos_sim'}
fit() 方法的参数:
{
"epochs": 3,
"evaluation_steps": 0,
"evaluator": "NoneType",
"max_grad_norm": 1,
"optimizer_class": "<class 'torch.optim.adamw.AdamW'>",
"optimizer_params": {
"lr": 2e-05
},
"scheduler": "WarmupLinear",
"steps_per_epoch": null,
"warmup_steps": 60,
"weight_decay": 0.01
}
SentenceTransformer(
(0): Transformer({'max_seq_length': 512, 'do_lower_case': False}) with Transformer model: DistilBertModel
(1): Pooling({'word_embedding_dimension': 768, 'pooling_mode_cls_token': False, 'pooling_mode_mean_tokens': True, 'pooling_mode_max_tokens': False, 'pooling_mode_mean_sqrt_len_tokens': False})
)