The Trainer API

The Trainer API

The Trainer API Overview

  • The Trainer API in the Transformers library simplifies fine-tuning transformer models on custom datasets.
  • It supports various setups (CPU, GPU, TPUs) and can evaluate models if metrics are provided.
  • Dynamic padding is handled automatically with a DataCollatorWithPadding; final data processing steps are managed by the Trainer.

Setting Up the Trainer

  • Define your model and training hyperparameters using the TrainingArguments class.
  • Specify a results folder and customize hyperparameters like learning rate and epochs for training.
  • Launching training displays a progress bar; however, initial results may only show training loss without evaluation metrics.

Evaluating Model Performance

  • Use the predict method to gather predictions from the evaluation set, returning predictions, label_ids, and metrics.
  • Match predictions with labels using argmax to determine predicted classes; accuracy achieved was 85.7%.
  • Implement a compute_metrics function to track evaluation metrics during training by passing it to TrainingArguments.
Video description

The Trainer API of the Transformers library, and how to use it to fine-tune a model. This video is part of the Hugging Face course: http://huggingface.co/course Open in colab to run the code samples: https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/videos/trainer_api.ipynb Related videos: - How to instantiate a Transformers model: https://youtu.be/AhChOFRegn4 - How to preprocess sentence pairs: https://youtu.be/0u3ioSwev3s - The 🤗 Datasets library: https://youtu.be/_BZearw7f0w - What is dynamic padding: https://youtu.be/7q5NyFT8REg Have a question? Checkout the forums: https://discuss.huggingface.co/c/course/20 Subscribe to our newsletter: https://huggingface.curated.co/