deepblue
All Posts

Transformers and its Trainer

AuthorsBen Shalom Albert and Vinod Thomas
Published
Organizationdeepblue Research
CategoryTechnology, Deep Learning
circuits on a black board

Overview

Hugging Face's Trainer provides a feature-rich API for training and evaluating PyTorch models in the Transformers ecosystem. It supports distributed training across multiple GPUs/TPUs and mixed precision on supported hardware.

Trainer is commonly paired with TrainingArguments, which configures the run. For sequence-to-sequence tasks such as translation and summarization, the article also points to Seq2SeqTrainer and Seq2SeqTrainingArguments.

Main Trainer parameters

model

The model to train, evaluate, or use for predictions. It can be a pretrained Hugging Face model or a torch.nn.Module. If the model is not supplied directly, model_init can create it.

1trainer = transformers.Trainer(
2 model_init=function_to_instantiate_a_model
3)

args

An instance of TrainingArguments, containing the training configuration. If omitted, default arguments are used.

data_collator

A function that forms batches from individual examples. The default behavior depends on whether a tokenizer is supplied; padding-aware collation can be used when one is available.

train_dataset

The training dataset. For a Hugging Face Dataset, columns not accepted by the model's forward() method can be removed automatically. The article also discusses reproducible randomization for iterable datasets in distributed training, using a shared torch.Generator or a set_epoch() method.

eval_dataset

The evaluation dataset. It can be a dataset or a dictionary of datasets. Dictionary keys are incorporated into metric names when evaluating each dataset.

tokenizer

The tokenizer used to preprocess text. It can support automatic padding and is saved with the model for later reuse or resuming work.

model_init

A callable that instantiates a model. It may accept no arguments or a hyperparameter-search trial object (for example, from Optuna or Ray Tune). Each call to train() can create a fresh model instance.

compute_metrics

A function that receives an EvalPrediction and returns a dictionary of metric names and values. The article notes that batch-based metric computation can require a compute_result argument to signal the final evaluation batch.

callbacks

Callbacks inspect training state and respond to events in the training loop. The article mentions Trainer.remove_callback() for removing a default callback.

optimizers

A tuple containing the optimizer and learning-rate scheduler. If not specified, defaults are selected based on the training arguments.

preprocess_logits_for_metrics

A function that processes logits before they are cached during evaluation. It receives logits and labels and returns the processed logits used by compute_metrics. Labels may be None if the dataset does not include them.

Takeaway

Trainer packages much of the standard PyTorch training and evaluation loop into a reusable API. TrainingArguments configures the run, while the other parameters supply the model, datasets, batching, metrics, callbacks, and optimization components.

Source coverage note: The accessible article concludes by describing Trainer as a feature-complete training and evaluation loop optimized for Hugging Face Transformers.

Related Posts