Getting Started with Hugging Face Transformers in Python

Hugging Face Transformers is a Python library that provides state-of-the-art natural language processing (NLP) models for various tasks, such as text classification, tokenization, and language generation. In this guide, we'll walk you through the process of installing and using the Hugging Face Transformers library in Python.

Table of Contents

  1. Installation
  2. Tokenization
  3. Pre-trained Models
  4. Fine-tuning Models
  5. Conclusion


To install the Hugging Face Transformers library, use the following command:

pip install transformers

This will install the latest version of the library, along with its dependencies.


Tokenization is the process of converting a text into a sequence of tokens, which can be fed as input to a model. Hugging Face Transformers provides various tokenizers for different models. Let's start by importing a tokenizer for the BERT model:

from transformers import BertTokenizer

tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')

Now, we can tokenize a given text using the encode method:

text = "Hello, I am learning Hugging Face Transformers in Python!"
tokens = tokenizer.encode(text)


This will output the tokenized sequence:

[101, 7592, 1010, 1045, 2572, 4083, 17662, 2227, 19081, 2015, 1999, 18750, 999, 102]

Pre-trained Models

Hugging Face Transformers provides pre-trained models for various NLP tasks. Let's import the BERT model and use it for text classification:

from transformers import BertForSequenceClassification

model = BertForSequenceClassification.from_pretrained('bert-base-uncased')

To use the model for classification, we'll first tokenize the input text and then feed it to the model:

input_text = "This is a positive sentiment example."
input_tokens = tokenizer.encode(input_text, return_tensors='pt')

output = model(input_tokens)

The output variable contains the logits for the classification task. We can convert the logits to probabilities using the softmax function:

import torch.nn.functional as F

probabilities = F.softmax(output.logits, dim=-1)

Now, we can get the predicted class using the argmax function:

predicted_class = torch.argmax(probabilities)


Fine-tuning Models

In some cases, you may want to fine-tune a pre-trained model on your specific task. To do this, you'll need a labeled dataset and an optimizer. Let's import the necessary components:

from transformers import AdamW

optimizer = AdamW(model.parameters(), lr=2e-5)

Now, we can iterate through the dataset, compute the loss, and update the model's parameters using the optimizer:

import torch

for epoch in range(epochs):
    for batch in dataloader:
        input_ids, attention_mask, labels = batch

        outputs = model(input_ids, attention_mask=attention_mask, labels=labels)
        loss = outputs.loss


After fine-tuning, you can use the model for prediction, as shown in the Pre-trained Models section.


In this guide, we've covered how to get started with Hugging Face Transformers in Python, including installation, tokenization, and model usage for natural language processing tasks. With this knowledge, you can now start exploring various pre-trained models and fine-tune them on your specific tasks to achieve state-of-the-art performance.

An AI coworker, not just a copilot

View VelocityAI