# Finetuning T5 for Summarisation - Poor results

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ffinetuning-t5-for-summarisation-poor-results%2F79449%3E%5C
**Category:** Intermediate\
**Created:** [March 30, 2024, 7:01pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ffinetuning-t5-for-summarisation-poor-results%2F79449 "2024-03-30T19:01:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ManVer95](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/manver95/32/25331_2.png) [@ManVer95](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FManVer95)%5C
**Post date:** [March 30, 2024, 7:01pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ffinetuning-t5-for-summarisation-poor-results%2F79449%2F1 "2024-03-30T19:01:30Z")

</div>

Hi,

I am trying to fine tune the T5-base model on [this](https://github.com/amanpreet692/Open4Business) 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):

```auto
# 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!

---

<div class="post-metadata">

**Author:** ![vibhu](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/vibhu/32/23403_2.png) [@vibhu](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fvibhu)%5C
**Post date:** [April 28, 2024, 3:19pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ffinetuning-t5-for-summarisation-poor-results%2F79449%2F2 "2024-04-28T15:19:35Z")

</div>

I have been experimenting with similar usecase with T5 for summarization post FT cycles. I started getting better results after 5 epochs, but my preproc routine was different. After pre-proc my training loss was around 1.35 and downward trajectory. This tells me that more epochs needed to experiment if loss keep reducing, along with val-loss.
