Text Summarization

Summarization creates a shorter version of a document or an article and can be formulated as a sequence-to-sequence task.

Verified

On this page

Definition

Summarization creates a shorter version of a document or an article. Along with translation, summarization is an example of a task that can be formulated as a sequence-to-sequence task. Extractive summarization extracts the most relevant information from a document.

Abstractive summarization generates new text that captures the most relevant information. The official documentation shows how to finetune T5 on the California state bill subset of the BillSum dataset for abstractive summarization. The official documentation also shows how to use the finetuned model for inference.

At a glance

Summarization can be formulated as a sequence-to-sequence task. Summarization can be extractive or abstractive.

  • In the BillSum example, the text field holds the text of the bill and is the input to the model.
  • In the BillSum example, the summary field is a condensed version of text and is the model target.
  • Preprocessing prefixes the input with a prompt so T5 knows this is a summarization task.
  • The training arguments are passed to Seq2SeqTrainer along with the model, dataset, tokenizer, data collator, and compute_metrics function.
  • The generate method is used to create the summarization.
  • The generated token ids are decoded back into text.

The source does not state the order of these points, so they are listed without arrows.

How it works

Preprocessing starts by loading a T5 tokenizer to process text and summary. The documentation says some models capable of multiple NLP tasks require prompting for specific tasks. The preprocessing function uses the keyword text_target argument when tokenizing labels.

The preprocessing function truncates sequences to be no longer than the maximum length set by the max_length parameter. The preprocessing function is applied over the entire dataset with the Datasets map method. A batch of examples is created using DataCollatorForSeq2Seq.

The Hugging Face guide says setting batched=True can speed up map by processing multiple dataset elements at once. The documentation says dynamically padding the sentences to the longest length in a batch during collation is more efficient than padding the whole dataset to the maximum length. The documentation says including a metric during training is often helpful for evaluating the model's performance.

For this task, the documentation loads the ROUGE metric. The metric function passes predictions and labels to compute to calculate the ROUGE metric. For training, T5 is loaded with AutoModelForSeq2SeqLM.

Training hyperparameters are defined in Seq2SeqTrainingArguments, where the only required parameter is output_dir, which specifies where to save the model. For the guide’s T5/BillSum training-configuration example, the guide states that the Trainer evaluates ROUGE and saves the training checkpoint at the end of each epoch. Calling the train method finetunes the model.

The model is pushed to the Hub by setting push_to_hub to True, which requires being signed in to Hugging Face.

Example

The documentation's example starts by loading the smaller California state bill subset of the BillSum dataset. The dataset is then split into a train and test set with the train_test_split method. In the inference example, for T5, the input needs to be prefixed depending on the task being worked on. For inference, the text is tokenized and the input_ids are returned as PyTorch tensors.

Sources

This page is written from the official sources below. Open them to check the original text.

Verified ·

This page is written by AI from the official sources above and checked automatically. This site has not tested it. Found a mistake? Tell us.