text_plain_output_1.png
from datasets import load_dataset

from sentence_transformers import InputExample

from sentence_transformers.cross_encoder import CrossEncoder

from sentence_transformers.cross_encoder.evaluation import CECorrelationEvaluator

from torch.utils.data import DataLoader

dataset_train = load_dataset('stsb_multi_mt', name='it', split='train')
dataset_test = load_dataset('stsb_multi_mt', name='it', split='test')

gold_samples = []
batch_size = 16
for df in dataset_train:
    score = float(df['similarity_score']) / 5.0
    gold_samples.append(InputExample(texts=[df['sentence1'], df['sentence2']], label=score))
    gold_samples.append(InputExample(texts=[df['sentence2'], df['sentence1']], label=score))
train_dataloader = DataLoader(gold_samples, shuffle=True, batch_size=batch_size)

from sentence_transformers.cross_encoder import CrossEncoder
model_checkpoint = 'dbmdz/bert-base-italian-uncased'
cross_encoder = CrossEncoder(model_checkpoint, num_labels=1)

from sentence_transformers.cross_encoder.evaluation import CECorrelationEvaluator
import math
evaluator = CECorrelationEvaluator([[x['sentence1'], x['sentence2']] for x in dataset_test], [x / 5.0 for x in dataset_test['similarity_score']])

num_epochs = 4
evaluation_steps = 500
warmup_steps = int(len(train_dataloader) * num_epochs * 0.1)

cross_encoder.fit(train_dataloader=train_dataloader, evaluator=evaluator, epochs=num_epochs, evaluation_steps=evaluation_steps, warmup_steps=warmup_steps, save_best_model=True, output_path='cross-encoder/')

evaluator(cross_encoder)