2d_matryoshka_sts.py 4.57 KB
Newer Older
Rayyyyy's avatar
Rayyyyy committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
"""
This examples trains BERT (or any other transformer model like RoBERTa, DistilBERT etc.) for the STSbenchmark from scratch.
It uses MatryoshkaLoss with the powerful CoSENTLoss to train models that perform well at output dimensions [768, 512, 256, 128, 64].
It generates sentence embeddings that can be compared using cosine-similarity to measure the similarity.

Usage:
python 2d_matryoshka_sts.py

OR
python 2d_matryoshka_sts.py pretrained_transformer_model_name
"""

import logging
import sys
Rayyyyy's avatar
Rayyyyy committed
15
16
import traceback
from datetime import datetime
Rayyyyy's avatar
Rayyyyy committed
17

Rayyyyy's avatar
Rayyyyy committed
18
19
20
21
22
23
from datasets import load_dataset
from sentence_transformers import (
    SentenceTransformer,
    SentenceTransformerTrainer,
    SentenceTransformerTrainingArguments,
    losses,
Rayyyyy's avatar
Rayyyyy committed
24
)
Rayyyyy's avatar
Rayyyyy committed
25
from sentence_transformers.evaluation import EmbeddingSimilarityEvaluator, SimilarityFunction
Rayyyyy's avatar
Rayyyyy committed
26

Rayyyyy's avatar
Rayyyyy committed
27
28
# Set the log level to INFO to get more information
logging.basicConfig(format="%(asctime)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S", level=logging.INFO)
Rayyyyy's avatar
Rayyyyy committed
29
30

model_name = sys.argv[1] if len(sys.argv) > 1 else "distilbert-base-uncased"
Rayyyyy's avatar
Rayyyyy committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
batch_size = 16
num_train_epochs = 4

# Save path of the model
output_dir = f"output/2d_matryoshka_sts_{model_name.replace('/', '-')}-{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}"

# 1. Here we define our SentenceTransformer model. If not already a Sentence Transformer model, it will automatically
# create one with "mean" pooling.
model = SentenceTransformer(model_name)
# If we want, we can limit the maximum sequence length for the model
# model.max_seq_length = 75
logging.info(model)

# 2. Load the STSB dataset: https://huggingface.co/datasets/sentence-transformers/stsb
train_dataset = load_dataset("sentence-transformers/stsb", split="train")
eval_dataset = load_dataset("sentence-transformers/stsb", split="validation")
test_dataset = load_dataset("sentence-transformers/stsb", split="test")
logging.info(train_dataset)

# 3. Define our training loss
# CoSENTLoss (https://sbert.net/docs/package_reference/sentence_transformer/losses.html#cosentloss) needs two text columns and one
# similarity score column (between 0 and 1)
inner_train_loss = losses.CoSENTLoss(model=model)
train_loss = losses.Matryoshka2dLoss(model, inner_train_loss, [768, 512, 256, 128, 64])

# 4. Define an evaluator for use during training. This is useful to keep track of alongside the evaluation loss.
dev_evaluator = EmbeddingSimilarityEvaluator(
    sentences1=eval_dataset["sentence1"],
    sentences2=eval_dataset["sentence2"],
    scores=eval_dataset["score"],
    main_similarity=SimilarityFunction.COSINE,
    name="sts-dev",
Rayyyyy's avatar
Rayyyyy committed
63
64
)

Rayyyyy's avatar
Rayyyyy committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 5. Define the training arguments
args = SentenceTransformerTrainingArguments(
    # Required parameter:
    output_dir=output_dir,
    # Optional training parameters:
    num_train_epochs=num_train_epochs,
    per_device_train_batch_size=batch_size,
    per_device_eval_batch_size=batch_size,
    warmup_ratio=0.1,
    fp16=True,  # Set to False if you get an error that your GPU can't run on FP16
    bf16=False,  # Set to True if you have a GPU that supports BF16
    # Optional tracking/debugging parameters:
    eval_strategy="steps",
    eval_steps=100,
    save_strategy="steps",
    save_steps=100,
    save_total_limit=2,
    logging_steps=100,
    run_name="2d-matryoshka-sts",  # Will be used in W&B if `wandb` is installed
Rayyyyy's avatar
Rayyyyy committed
84
85
)

Rayyyyy's avatar
Rayyyyy committed
86
87
88
89
90
91
92
93
# 6. Create the trainer & start training
trainer = SentenceTransformerTrainer(
    model=model,
    args=args,
    train_dataset=train_dataset,
    eval_dataset=eval_dataset,
    loss=train_loss,
    evaluator=dev_evaluator,
Rayyyyy's avatar
Rayyyyy committed
94
)
Rayyyyy's avatar
Rayyyyy committed
95
trainer.train()
Rayyyyy's avatar
Rayyyyy committed
96
97


Rayyyyy's avatar
Rayyyyy committed
98
99
100
101
102
103
104
105
106
# 7. Evaluate the model performance on the STS Benchmark test dataset
test_evaluator = EmbeddingSimilarityEvaluator(
    sentences1=test_dataset["sentence1"],
    sentences2=test_dataset["sentence2"],
    scores=test_dataset["score"],
    main_similarity=SimilarityFunction.COSINE,
    name="sts-test",
)
test_evaluator(model)
Rayyyyy's avatar
Rayyyyy committed
107

Rayyyyy's avatar
Rayyyyy committed
108
109
110
# 8. Save the trained & evaluated model locally
final_output_dir = f"{output_dir}/final"
model.save(final_output_dir)
Rayyyyy's avatar
Rayyyyy committed
111

Rayyyyy's avatar
Rayyyyy committed
112
# 9. (Optional) save the model to the Hugging Face Hub!
Rayyyyy's avatar
Rayyyyy committed
113
114
115
116
117
118
# It is recommended to run `huggingface-cli login` to log into your Hugging Face account first
model_name = model_name if "/" not in model_name else model_name.split("/")[-1]
try:
    model.push_to_hub(f"{model_name}-sts-2d-matryoshka")
except Exception:
    logging.error(
Rayyyyy's avatar
Rayyyyy committed
119
120
        f"Error uploading model to the Hugging Face Hub:\n{traceback.format_exc()}To upload it manually, you can run "
        f"`huggingface-cli login`, followed by loading the model using `model = SentenceTransformer({final_output_dir!r})` "
Rayyyyy's avatar
Rayyyyy committed
121
122
        f"and saving it using `model.push_to_hub('{model_name}-sts-2d-matryoshka')`."
    )