> ## Documentation Index
> Fetch the complete documentation index at: https://wb-21fd5541-evaltables.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Hugging Face

> Visualize and track Hugging Face model performance with W&B, logging hyperparameters, metrics, and GPU utilization.

export const ColabLink = ({url}) => <a href={url} target="_blank" rel="noopener noreferrer" className="colab-link">
    <svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
      <path d="M14.25.18l.9.2.73.26.59.3.45.32.34.34.25.34.16.33.1.3.04.26.02.2-.01.13V8.5l-.05.63-.13.55-.21.46-.26.38-.3.31-.33.25-.35.19-.35.14-.33.1-.3.07-.26.04-.21.02H8.77l-.69.05-.59.14-.5.22-.41.27-.33.32-.27.35-.2.36-.15.37-.1.35-.07.32-.04.27-.02.21v3.06H3.17l-.21-.03-.28-.07-.32-.12-.35-.18-.36-.26-.36-.36-.35-.46-.32-.59-.28-.73-.21-.88-.14-1.05-.05-1.23.06-1.22.16-1.04.24-.87.32-.71.36-.57.4-.44.42-.33.42-.24.4-.16.36-.1.32-.05.24-.01h.16l.06.01h8.16v-.83H6.18l-.01-2.75-.02-.37.05-.34.11-.31.17-.28.25-.26.31-.23.38-.2.44-.18.51-.15.58-.12.64-.1.71-.06.77-.04.84-.02 1.27.05zm-6.3 1.98l-.23.33-.08.41.08.41.23.34.33.22.41.09.41-.09.33-.22.23-.34.08-.41-.08-.41-.23-.33-.33-.22-.41-.09-.41.09zm13.09 3.95l.28.06.32.12.35.18.36.27.36.35.35.47.32.59.28.73.21.88.14 1.04.05 1.23-.06 1.23-.16 1.04-.24.86-.32.71-.36.57-.4.45-.42.33-.42.24-.4.16-.36.09-.32.05-.24.02-.16-.01h-8.22v.82h5.84l.01 2.76.02.36-.05.34-.11.31-.17.29-.25.25-.31.24-.38.2-.44.17-.51.15-.58.13-.64.09-.71.07-.77.04-.84.01-1.27-.04-1.07-.14-.9-.2-.73-.25-.59-.3-.45-.33-.34-.34-.25-.34-.16-.33-.1-.3-.04-.25-.02-.2.01-.13v-5.34l.05-.64.13-.54.21-.46.26-.38.3-.32.33-.24.35-.2.35-.14.33-.1.3-.06.26-.04.21-.02.13-.01h5.84l.69-.05.59-.14.5-.21.41-.28.33-.32.27-.35.2-.36.15-.36.1-.35.07-.32.04-.28.02-.21V6.07h2.09l.14.01.21.03zm-6.47 14.25l-.23.33-.08.41.08.41.23.33.33.23.41.08.41-.08.33-.23.23-.33.08-.41-.08-.41-.23-.33-.33-.23-.41-.08-.41.08z" />
    </svg>
    Try in Colab
  </a>;

<ColabLink url="https://colab.research.google.com/github/wandb/examples/blob/master/colabs/huggingface/Huggingface_wandb.ipynb" />

This tutorial shows you how to use the W\&B integration with [Hugging Face Transformers](https://github.com/huggingface/transformers) to automatically track training and evaluation metrics, hyperparameters, and system stats while fine-tuning a model. By following this tutorial, you learn how to visualize your model's performance through the [W\&B](https://wandb.ai/site) dashboard so you can compare experiments and iterate on your models with confidence.

You can compare hyperparameters, output metrics, and system stats like GPU utilization across your models.

## Why use W\&B

<Frame>
  <img src="https://mintcdn.com/wb-21fd5541-evaltables/2M5oikArrZlRoR02/images/tutorials/huggingface-why.png?fit=max&auto=format&n=2M5oikArrZlRoR02&q=85&s=32c85b33f1710d7ef74be96585a6ad19" alt="Benefits of using W&B" width="4672" height="816" data-path="images/tutorials/huggingface-why.png" />
</Frame>

* **Unified dashboard**: Central repository for all your model metrics and predictions.
* **Lightweight**: No code changes required to integrate with Hugging Face.
* **Accessible**: Free for individuals and academic teams.
* **Secure**: All projects are private by default.
* **Trusted**: Used by machine learning teams at OpenAI, Toyota, Lyft, and more.

W\&B works like GitHub for machine learning models. Save machine learning experiments to your private, hosted dashboard. Experiment with the confidence that W\&B saves all versions of your models, no matter where you run your scripts.

W\&B lightweight integrations work with any Python script. Sign up for a free W\&B account to start tracking and visualizing your models.

In the Hugging Face Transformers repository, W\&B has instrumented the Trainer to automatically log training and evaluation metrics to W\&B at each logging step.

Here's an in-depth look at how the integration works: [Hugging Face + W\&B Report](https://app.wandb.ai/jxmorris12/huggingface-demo/reports/Train-a-model-with-Hugging-Face-and-Weights-%26-Biases--VmlldzoxMDE2MTU).

## Install, import, and log in

This section sets up the environment you need to run the tutorial. Install the Hugging Face and W\&B libraries, and download the GLUE dataset and training script for this tutorial:

* [Hugging Face Transformers](https://github.com/huggingface/transformers): Natural language models and datasets.
* [W\&B](/): Experiment tracking and visualization.
* [GLUE dataset](https://gluebenchmark.com/): A language understanding benchmark dataset.
* [GLUE script](https://raw.githubusercontent.com/huggingface/transformers/refs/heads/main/examples/pytorch/text-classification/run_glue.py): Model training script for sequence classification.

```notebook theme={null}
!pip install datasets wandb evaluate accelerate -qU
!wget https://raw.githubusercontent.com/huggingface/transformers/refs/heads/main/examples/pytorch/text-classification/run_glue.py
```

```notebook theme={null}
# the run_glue.py script requires transformers dev
!pip install -q git+https://github.com/huggingface/transformers
```

Before continuing, you must [sign up for a free account](https://app.wandb.ai/login?signup=true). An account is required to send your run data to a W\&B dashboard.

## Add your API key

Authenticating with your API key links this notebook to your W\&B account so that runs are logged to your projects. After you sign up, run the next cell and click the link to get your API key and authenticate this notebook.

```python theme={null}
import wandb
wandb.login()
```

Optionally, you can set environment variables to customize what W\&B logs during training. For example, you can log both gradients and parameters by setting `WANDB_WATCH=all`. See the [Hugging Face integration guide](/models/integrations/huggingface/) for the full list of options.

```python theme={null}
# Optional: log both gradients and parameters
%env WANDB_WATCH=all
```

## Train the model

With the environment configured and authentication complete, you're ready to start a training run. Call the downloaded training script [`run_glue.py`](https://huggingface.co/transformers/examples.html#glue) and see training automatically get tracked to the W\&B dashboard. This script fine-tunes BERT on the Microsoft Research Paraphrase Corpus (pairs of sentences with human annotations indicating whether they're semantically equivalent).

```python theme={null}
%env WANDB_PROJECT=huggingface-demo
%env TASK_NAME=MRPC

!python run_glue.py \
  --model_name_or_path bert-base-uncased \
  --task_name $TASK_NAME \
  --do_train \
  --do_eval \
  --max_seq_length 256 \
  --per_device_train_batch_size 32 \
  --learning_rate 2e-4 \
  --num_train_epochs 3 \
  --output_dir /tmp/$TASK_NAME/ \
  --overwrite_output_dir \
  --logging_steps 50
```

## Visualize results in the dashboard

After training starts, you can monitor metrics in real time. Click the link printed out by the previous cell, or go to [wandb.ai](https://app.wandb.ai) to see your results stream in live. The link to see your run in the browser appears after all the dependencies are loaded. Look for the following output: "**wandb**: View run at \[URL to your unique run]"

### Visualize model performance

Look across experiments, zoom in on findings, and visualize high-dimensional data.

<Frame>
  <img src="https://mintcdn.com/wb-21fd5541-evaltables/2M5oikArrZlRoR02/images/tutorials/huggingface-visualize.gif?s=0bff12214a447645abc924e63b6fb093" alt="Model metrics dashboard" width="1986" height="1420" data-path="images/tutorials/huggingface-visualize.gif" />
</Frame>

### Compare architectures

Here's an example comparing [BERT versus DistilBERT](https://app.wandb.ai/jack-morris/david-vs-goliath/reports/Does-model-size-matter%3F-Comparing-BERT-and-DistilBERT-using-Sweeps--VmlldzoxMDUxNzU). The automatic line plot visualizations show how different architectures affect the evaluation accuracy throughout training.

<Frame>
  <img src="https://mintcdn.com/wb-21fd5541-evaltables/2M5oikArrZlRoR02/images/tutorials/huggingface-comparearchitectures.gif?s=59d47fd6148ce041d152b72549ee7312" alt="BERT versus DistilBERT comparison" width="1638" height="878" data-path="images/tutorials/huggingface-comparearchitectures.gif" />
</Frame>

## Track key information by default

This section describes what W\&B captures automatically so you know what data is available in your dashboard without additional configuration. W\&B saves a new run for each experiment. Here's the information saved by default:

* **Hyperparameters**: W\&B saves settings for your model in Config.
* **Model metrics**: W\&B saves time series data of metrics streaming in to Log.
* **Terminal logs**: W\&B saves command line outputs and makes them available in a tab.
* **System metrics**: GPU and CPU utilization, memory, and temperature.

## Learn more

* [Video walkthroughs on YouTube](http://wandb.me/youtube)
