Hi,
I am trying to fine tune the T5-base model on this dataset. It contains 13966 texts and their corresponding summaries. However, the results I am getting are quite horrible so maybe I have missed something trivial. Below is my code (I tried to follow the Huggingface tutorial on summarisation tasks):
# Define the tokenizer and model
checkpoint = "t5-base"
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
model = AutoModelForSeq2SeqLM.from_pretrained(checkpoint)
prefix = "summarize: "
# Define the preprocess function
def preprocess_function(examples):
inputs = [prefix + doc for doc in examples["source_text"]]
model_inputs = tokenizer(inputs, max_length=1024, truncation=True)
# Note: You might need to adjust this line if your target summaries are not in the 'target_summary' field
labels = tokenizer(examples["target_summary"], max_length=128, truncation=True)
model_inputs["labels"] = labels["input_ids"]
return model_inputs
# Note that my raw data is loaded into dicts
train_dataset = Dataset.from_dict(train_dataset)
val_dataset = Dataset.from_dict(val_dataset)
tokenized_train_dataset = train_dataset.map(preprocess_function, batched=True)
tokenized_val_dataset = val_dataset.map(preprocess_function, batched=True)
import evaluate
rouge = evaluate.load("rouge")
data_collator = DataCollatorForSeq2Seq(tokenizer=tokenizer, model=model)
# Define the compute metrics function
def compute_metrics(eval_pred):
predictions, labels = eval_pred
decoded_preds = tokenizer.batch_decode(predictions, skip_special_tokens=True)
labels = np.where(labels != -100, labels, tokenizer.pad_token_id)
decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True)
# Note: You need to implement the 'rouge' function yourself or use a library like 'rouge-score'
result = rouge.compute(predictions=decoded_preds, references=decoded_labels, use_stemmer=True)
prediction_lens = [np.count_nonzero(pred != tokenizer.pad_token_id) for pred in predictions]
result["gen_len"] = np.mean(prediction_lens)
return {k: round(v, 4) for k, v in result.items()}
# Define the training arguments
training_args = Seq2SeqTrainingArguments(
output_dir="T5_summarization_model",
evaluation_strategy="epoch",
learning_rate=2e-5,
per_device_train_batch_size=4,
per_device_eval_batch_size=4,
weight_decay=0.01,
save_total_limit=3,
num_train_epochs=2,
predict_with_generate=True,
fp16=True,
)
# Define the trainer
trainer = Seq2SeqTrainer(
model=model,
args=training_args,
train_dataset=tokenized_train_dataset,
eval_dataset=tokenized_val_dataset,
tokenizer=tokenizer,
data_collator=data_collator,
compute_metrics=compute_metrics,
)
# Train the model
trainer.train()
Epoch | Training Loss | Validation Loss | Rouge1 | Rouge2 | Rougel | Rougelsum | Gen Len |
---|---|---|---|---|---|---|---|
1 | 2.507700 | 2.355953 | 0.189500 | 0.084200 | 0.154900 | 0.154700 | 19.000000 |
2 | 2.641300 | 2.495491 | 0.186300 | 0.078300 | 0.150800 | 0.150700 | 19.000000 |
As you can see, the validation loss increased. I am unsure if 2 epochs is too little or if I have forgotten something important. And when I try to do inference on the test dataset, I see a lot of repetition. Any feedback would be much appreciated!