Create train_script.py
Browse files- train_script.py +97 -0
train_script.py
ADDED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import logging
|
2 |
+
from datasets import load_dataset, Dataset
|
3 |
+
from sentence_transformers import (
|
4 |
+
SentenceTransformer,
|
5 |
+
SentenceTransformerTrainer,
|
6 |
+
SentenceTransformerTrainingArguments,
|
7 |
+
SentenceTransformerModelCardData,
|
8 |
+
)
|
9 |
+
from sentence_transformers.losses import MultipleNegativesRankingLoss
|
10 |
+
from sentence_transformers.training_args import BatchSamplers
|
11 |
+
from sentence_transformers.evaluation import InformationRetrievalEvaluator
|
12 |
+
|
13 |
+
|
14 |
+
logging.basicConfig(
|
15 |
+
format="%(asctime)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S", level=logging.INFO
|
16 |
+
)
|
17 |
+
|
18 |
+
# 1. Load a model to finetune with 2. (Optional) model card data
|
19 |
+
model = SentenceTransformer(
|
20 |
+
"jinaai/jina-embeddings-v2-base-en",
|
21 |
+
trust_remote_code=True,
|
22 |
+
model_card_data=SentenceTransformerModelCardData(
|
23 |
+
language="en",
|
24 |
+
license="apache-2.0",
|
25 |
+
model_name="jina-embeddings-v2-base-en trained on Natural Questions pairs",
|
26 |
+
),
|
27 |
+
)
|
28 |
+
model_name = "jina-v2-base-natural-questions"
|
29 |
+
|
30 |
+
# 3. Load a dataset to finetune on
|
31 |
+
dataset = load_dataset("sentence-transformers/natural-questions", split="train")
|
32 |
+
dataset = dataset.add_column("id", range(len(dataset)))
|
33 |
+
train_dataset: Dataset = dataset.select(range(90_000))
|
34 |
+
eval_dataset: Dataset = dataset.select(range(90_000, len(dataset)))
|
35 |
+
|
36 |
+
# 4. Define a loss function
|
37 |
+
loss = MultipleNegativesRankingLoss(model)
|
38 |
+
|
39 |
+
|
40 |
+
# 5. (Optional) Specify training arguments
|
41 |
+
args = SentenceTransformerTrainingArguments(
|
42 |
+
# Required parameter:
|
43 |
+
output_dir=f"models/{model_name}",
|
44 |
+
# Optional training parameters:
|
45 |
+
num_train_epochs=1,
|
46 |
+
per_device_train_batch_size=16,
|
47 |
+
per_device_eval_batch_size=16,
|
48 |
+
learning_rate=2e-5,
|
49 |
+
warmup_ratio=0.1,
|
50 |
+
fp16=False, # Set to False if you get an error that your GPU can't run on FP16
|
51 |
+
bf16=True, # Set to True if you have a GPU that supports BF16
|
52 |
+
batch_sampler=BatchSamplers.NO_DUPLICATES, # MultipleNegativesRankingLoss benefits from no duplicate samples in a batch
|
53 |
+
# Optional tracking/debugging parameters:
|
54 |
+
eval_strategy="steps",
|
55 |
+
eval_steps=200,
|
56 |
+
save_strategy="steps",
|
57 |
+
save_steps=200,
|
58 |
+
save_total_limit=2,
|
59 |
+
logging_steps=200,
|
60 |
+
logging_first_step=True,
|
61 |
+
run_name=model_name, # Will be used in W&B if `wandb` is installed
|
62 |
+
)
|
63 |
+
|
64 |
+
# 6. (Optional) Create an evaluator & evaluate the base model
|
65 |
+
# The full corpus, but only the evaluation queries
|
66 |
+
queries = dict(zip(eval_dataset["id"], eval_dataset["query"]))
|
67 |
+
corpus = {cid: dataset[cid]["answer"] for cid in range(10_000)} | {cid: dataset[cid]["answer"] for cid in eval_dataset["id"]}
|
68 |
+
relevant_docs = {qid: {qid} for qid in eval_dataset["id"]}
|
69 |
+
dev_evaluator = InformationRetrievalEvaluator(
|
70 |
+
corpus=corpus,
|
71 |
+
queries=queries,
|
72 |
+
relevant_docs=relevant_docs,
|
73 |
+
show_progress_bar=True,
|
74 |
+
name="natural-questions-dev",
|
75 |
+
batch_size=8,
|
76 |
+
)
|
77 |
+
dev_evaluator(model)
|
78 |
+
|
79 |
+
# 7. Create a trainer & train
|
80 |
+
trainer = SentenceTransformerTrainer(
|
81 |
+
model=model,
|
82 |
+
args=args,
|
83 |
+
train_dataset=train_dataset.remove_columns("id"),
|
84 |
+
eval_dataset=eval_dataset.remove_columns("id"),
|
85 |
+
loss=loss,
|
86 |
+
evaluator=dev_evaluator,
|
87 |
+
)
|
88 |
+
trainer.train()
|
89 |
+
|
90 |
+
# (Optional) Evaluate the trained model on the evaluator after training
|
91 |
+
dev_evaluator(model)
|
92 |
+
|
93 |
+
# 8. Save the trained model
|
94 |
+
model.save_pretrained(f"models/{model_name}/final")
|
95 |
+
|
96 |
+
# 9. (Optional) Push it to the Hugging Face Hub
|
97 |
+
model.push_to_hub(f"{model_name}")
|