模型:

hackathon-pln-es/bertin-roberta-base-finetuning-esnli

英文

bertin-roberta-base-finetuning-esnli

这是一个在一系列西班牙语NLI任务上进行训练的模型,参数为 sentence-transformers 。它将句子和段落映射到一个768维的稠密向量空间,并可用于聚类或语义搜索等任务。

基于 this paper 中的连体网络方法。

您可以在此模型的演示中查看。

您可以在 here 中找到我们的其他模型,即paraphrase-spanish-distilroberta,以及其演示 here

使用(Sentence-Transformers)

如果您已经安装了 sentence-transformers ,则可以轻松使用该模型:

pip install -U sentence-transformers

然后您可以像这样使用模型:

from sentence_transformers import SentenceTransformer
sentences = ["Este es un ejemplo", "Cada oración es transformada"]

model = SentenceTransformer('hackathon-pln-es/bertin-roberta-base-finetuning-esnli')
embeddings = model.encode(sentences)
print(embeddings)

使用(HuggingFace Transformers)

如果没有 sentence-transformers ,则可以按照以下步骤使用该模型:首先,您将输入传递给变换器模型,然后必须在上下文词嵌入之上应用正确的汇集操作。

from transformers import AutoTokenizer, AutoModel
import torch


#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 = ['This is an example sentence', 'Each sentence is converted']

# Load model from HuggingFace Hub
tokenizer = AutoTokenizer.from_pretrained('hackathon-pln-es/bertin-roberta-base-finetuning-esnli')
model = AutoModel.from_pretrained('hackathon-pln-es/bertin-roberta-base-finetuning-esnli')

# 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, mean pooling.
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])

print("Sentence embeddings:")
print(sentence_embeddings)

评估结果

我们的模型在使用 SemEval-2015 Task 进行 Spanish 的语义文本相似性任务上进行了评估。

12317321 BERTIN STS (this model) Relative improvement
cosine_pearson 0.609803 0.683188 +12.03
cosine_spearman 0.528776 0.615916 +16.48
euclidean_pearson 0.590613 0.672601 +13.88
euclidean_spearman 0.526529 0.611539 +16.15
manhattan_pearson 0.589108 0.672040 +14.08
manhattan_spearman 0.525910 0.610517 +16.09
dot_pearson 0.544078 0.600517 +10.37
dot_spearman 0.460427 0.521260 +13.21

训练

该模型采用以下参数进行训练:

数据集

我们使用了一系列自然语言推理数据集作为训练数据:

使用的整个数据集可在 here 中找到。

这里我们留下了我们用来增加训练数据量的技巧:

  for row in reader:
    if row['language'] == 'es':
      
      sent1 = row['sentence1'].strip()
      sent2 = row['sentence2'].strip()
    
      add_to_samples(sent1, sent2, row['gold_label'])
      add_to_samples(sent2, sent1, row['gold_label'])  #Also add the opposite

DataLoader:

sentence_transformers.datasets.NoDuplicatesDataLoader.NoDuplicatesDataLoader参数长度为1818:

{'batch_size': 64}

Loss:

sentence_transformers.losses.MultipleNegativesRankingLoss.MultipleNegativesRankingLoss参数:

{'scale': 20.0, 'similarity_fct': 'cos_sim'}

fit()方法的参数:

{
    "epochs": 10,
    "evaluation_steps": 0,
    "evaluator": "sentence_transformers.evaluation.EmbeddingSimilarityEvaluator.EmbeddingSimilarityEvaluator",
    "max_grad_norm": 1,
    "optimizer_class": "<class 'transformers.optimization.AdamW'>",
    "optimizer_params": {
        "lr": 2e-05
    },
    "scheduler": "WarmupLinear",
    "steps_per_epoch": null,
    "warmup_steps": 909,
    "weight_decay": 0.01
}

完整模型架构

SentenceTransformer(
  (0): Transformer({'max_seq_length': 514, 'do_lower_case': False}) with Transformer model: RobertaModel 
  (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})
)

作者

Anibal Pérez

Emilio Tomás Ariza

Lautaro Gesuelli

Mauricio Mazuecos