Create train.py
Browse files
train.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import T5ForConditionalGeneration, T5Tokenizer, Trainer, TrainingArguments
|
3 |
+
from datasets import load_dataset
|
4 |
+
|
5 |
+
# Load dataset (Replace this with your dataset)
|
6 |
+
dataset = load_dataset("mbzuai/NLP-Cover-Letters") # Example dataset
|
7 |
+
|
8 |
+
# Load model and tokenizer
|
9 |
+
model_name = "t5-large"
|
10 |
+
tokenizer = T5Tokenizer.from_pretrained(model_name)
|
11 |
+
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
12 |
+
|
13 |
+
# Tokenization function
|
14 |
+
def tokenize_function(examples):
|
15 |
+
inputs = [ex["input"] for ex in examples]
|
16 |
+
targets = [ex["output"] for ex in examples]
|
17 |
+
model_inputs = tokenizer(inputs, max_length=512, truncation=True, padding="max_length")
|
18 |
+
labels = tokenizer(targets, max_length=512, truncation=True, padding="max_length")
|
19 |
+
model_inputs["labels"] = labels["input_ids"]
|
20 |
+
return model_inputs
|
21 |
+
|
22 |
+
# Apply tokenization
|
23 |
+
tokenized_datasets = dataset.map(tokenize_function, batched=True)
|
24 |
+
|
25 |
+
# Training arguments
|
26 |
+
training_args = TrainingArguments(
|
27 |
+
output_dir="./t5-finetuned",
|
28 |
+
per_device_train_batch_size=2, # Smaller batch to avoid memory errors
|
29 |
+
per_device_eval_batch_size=2, # Smaller eval batch
|
30 |
+
save_total_limit=1, # Keep only 1 checkpoint
|
31 |
+
num_train_epochs=1, # Quick test with 1 epoch
|
32 |
+
logging_steps=50, # More frequent logging
|
33 |
+
evaluation_strategy="epoch", # Evaluate only at the end of the epoch
|
34 |
+
save_strategy="epoch", # Save only at the end of the epoch
|
35 |
+
push_to_hub=False # Avoid pushing test model to Hugging Face Hub
|
36 |
+
)
|
37 |
+
|
38 |
+
# Trainer setup
|
39 |
+
trainer = Trainer(
|
40 |
+
model=model,
|
41 |
+
args=training_args,
|
42 |
+
train_dataset=tokenized_datasets["train"],
|
43 |
+
eval_dataset=tokenized_datasets["test"],
|
44 |
+
)
|
45 |
+
|
46 |
+
# Train the model
|
47 |
+
trainer.train()
|
48 |
+
|
49 |
+
# Save and push model to Hugging Face Hub
|
50 |
+
trainer.push_to_hub("your-hf-username/t5-cover-letter")
|