Create train_script.py
Browse files- train_script.py +80 -0
train_script.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datasets import load_dataset
|
2 |
+
from sentence_transformers import (
|
3 |
+
SparseEncoder,
|
4 |
+
SparseEncoderTrainer,
|
5 |
+
SparseEncoderTrainingArguments,
|
6 |
+
SparseEncoderModelCardData,
|
7 |
+
)
|
8 |
+
from sentence_transformers.sparse_encoder.losses import SpladeLoss, SparseMultipleNegativesRankingLoss
|
9 |
+
from sentence_transformers.training_args import BatchSamplers
|
10 |
+
from sentence_transformers.sparse_encoder.evaluation import SparseNanoBEIREvaluator
|
11 |
+
|
12 |
+
# 1. Load a model to finetune with 2. (Optional) model card data
|
13 |
+
model = SparseEncoder(
|
14 |
+
"distilbert/distilbert-base-uncased",
|
15 |
+
model_card_data=SparseEncoderModelCardData(
|
16 |
+
language="en",
|
17 |
+
license="apache-2.0",
|
18 |
+
model_name="Distilbert base trained on Natural-Questions tuples",
|
19 |
+
)
|
20 |
+
)
|
21 |
+
|
22 |
+
# 3. Load a dataset to finetune on
|
23 |
+
full_dataset = load_dataset("sentence-transformers/natural-questions", split="train").select(range(100_000))
|
24 |
+
dataset_dict = full_dataset.train_test_split(test_size=1_000, seed=12)
|
25 |
+
train_dataset = dataset_dict["train"]
|
26 |
+
eval_dataset = dataset_dict["test"]
|
27 |
+
|
28 |
+
# 4. Define a loss function
|
29 |
+
loss = SpladeLoss(
|
30 |
+
model=model,
|
31 |
+
loss=SparseMultipleNegativesRankingLoss(model=model),
|
32 |
+
lambda_query=5e-5,
|
33 |
+
lambda_corpus=3e-5,
|
34 |
+
)
|
35 |
+
|
36 |
+
# 5. (Optional) Specify training arguments
|
37 |
+
args = SparseEncoderTrainingArguments(
|
38 |
+
# Required parameter:
|
39 |
+
output_dir="models/splade-distilbert-base-uncased-nq",
|
40 |
+
# Optional training parameters:
|
41 |
+
num_train_epochs=1,
|
42 |
+
per_device_train_batch_size=16,
|
43 |
+
per_device_eval_batch_size=16,
|
44 |
+
learning_rate=2e-5,
|
45 |
+
warmup_ratio=0.1,
|
46 |
+
fp16=True, # Set to False if you get an error that your GPU can't run on FP16
|
47 |
+
bf16=False, # Set to True if you have a GPU that supports BF16
|
48 |
+
batch_sampler=BatchSamplers.NO_DUPLICATES, # MultipleNegativesRankingLoss benefits from no duplicate samples in a batch
|
49 |
+
# Optional tracking/debugging parameters:
|
50 |
+
eval_strategy="steps",
|
51 |
+
eval_steps=1000,
|
52 |
+
save_strategy="steps",
|
53 |
+
save_steps=1000,
|
54 |
+
save_total_limit=2,
|
55 |
+
logging_steps=100,
|
56 |
+
run_name="splade-distilbert-base-uncased-nq", # Will be used in W&B if `wandb` is installed
|
57 |
+
)
|
58 |
+
|
59 |
+
# 6. (Optional) Create an evaluator & evaluate the base model
|
60 |
+
dev_evaluator = SparseNanoBEIREvaluator(dataset_names=["msmarco", "nfcorpus", "nq"], batch_size=16)
|
61 |
+
|
62 |
+
# 7. Create a trainer & train
|
63 |
+
trainer = SparseEncoderTrainer(
|
64 |
+
model=model,
|
65 |
+
args=args,
|
66 |
+
train_dataset=train_dataset,
|
67 |
+
eval_dataset=eval_dataset,
|
68 |
+
loss=loss,
|
69 |
+
evaluator=dev_evaluator,
|
70 |
+
)
|
71 |
+
trainer.train()
|
72 |
+
|
73 |
+
# 8. Evaluate the model performance again after training
|
74 |
+
dev_evaluator(model)
|
75 |
+
|
76 |
+
# 9. Save the trained model
|
77 |
+
model.save_pretrained("models/splade-distilbert-base-uncased-nq/final")
|
78 |
+
|
79 |
+
# 10. (Optional) Push it to the Hugging Face Hub
|
80 |
+
model.push_to_hub("splade-distilbert-base-uncased-nq")
|