Unlock the Power of Hugging Face Transformers in Python 3

Hugging Face Transformers in Machine Learning | Innovate Yourself

In the ever-evolving landscape of machine learning, one tool has been making waves for its ability to transform the way we approach natural language understanding – Hugging Face Transformers. Whether you’re just starting your journey in Python or are well on your way to becoming a pro, understanding how to harness the power of Hugging Face Transformers is a game-changer. In this comprehensive guide, we’ll explore Hugging Face Transformers in Python 3, from the basics to advanced techniques, with practical examples and a hands-on demonstration using a sample dataset. By the end of this journey, you’ll have the knowledge to excel in the realm of Python machine learning.

Unveiling Hugging Face Transformers

Hugging Face Transformers is an open-source platform that provides state-of-the-art natural language processing (NLP) models, pre-trained and ready for use. These models are designed to understand and generate human language, making them invaluable for a wide range of NLP tasks.

Why Choose Hugging Face Transformers?

Hugging Face Transformers offers several compelling reasons for both beginners and seasoned Python enthusiasts:

  • Pre-trained Models: Hugging Face Transformers provides a vast array of pre-trained models that can be easily fine-tuned for specific NLP tasks. This saves significant time and computational resources compared to training models from scratch.
  • NLP Applications: With Hugging Face Transformers, you can tackle a wide range of NLP applications, such as text classification, sentiment analysis, named entity recognition, machine translation, and more.
  • User-Friendly: The platform is designed with user-friendliness in mind. You can get started quickly, even if you’re new to machine learning.
  • Community Support: Hugging Face has a thriving community and extensive documentation, making it easy to find answers to your questions and connect with other NLP enthusiasts.

Getting Started with Hugging Face Transformers

Before we delve into the world of Hugging Face Transformers, make sure you have Python 3.x installed on your system. You can install the transformers library using pip:

pip install transformers
pip install datasets

Let’s import the necessary libraries to kickstart our journey into Hugging Face Transformers:

import numpy as np
import pandas as pd
import transformers
import torch
import matplotlib.pyplot as plt
from transformers import AutoTokenizer, AutoModel
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

The Dataset

To make our learning journey more practical and engaging, we’ll work with a classic dataset widely used in NLP – the IMDb movie reviews dataset. This dataset contains movie reviews labeled as positive or negative. Let’s load it and explore the first few rows:

from datasets import load_dataset

dataset = load_dataset('imdb')
df = pd.DataFrame(dataset['train'])
downloading model in Hugging Face Transformers in Machine Learning | Innovate Yourself
                                                text  label
0  I rented I AM CURIOUS-YELLOW from my video sto...      0
1  "I Am Curious: Yellow" is a risible and preten...      0
2  If only to avoid making this type of film in t...      0
3  This film was probably inspired by Godard's Ma...      0
4  Oh, brother...after hearing about this ridicul...      0

Data Exploration

Exploring the dataset is the first step in any machine learning project. It helps you understand your data and its characteristics. For the IMDb dataset, we can start with basic statistics:

count  25000.00000
mean       0.50000
std        0.50001
min        0.00000
25%        0.00000
50%        0.50000
75%        1.00000
max        1.00000

Data Preprocessing

Now, it’s time to prepare the data for our Hugging Face Transformers model. We need to tokenize the text and convert it into suitable input formats for the model. We’ll also split the dataset into training and testing sets:

# Load the pre-trained BERT tokenizer
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")

# Tokenize the text and convert it into suitable input formats
encoded_data = tokenizer(list(df['text']), truncation=True, padding=True, return_tensors='pt')

# Split the dataset into features (X) and target (y)
X = encoded_data['input_ids']
y = df['label']

# Split the dataset into a training set and a testing set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
Downloading (…)okenizer_config.json: 100%|██████████████████████████████████████████████████| 28.0/28.0 [00:00<?, ?B/s]
C:\Users\gspl-p6\Desktop\mlVenv\Lib\site-packages\huggingface_hub\file_download.py:133: UserWarning: `huggingface_hub` cache-system uses symlinks by default to efficiently store duplicated files but your machine does not support them in C:\Users\gspl-p6\.cache\huggingface\hub. Caching files will still work but in a degraded version that might require more space on your disk. This warning can be disabled by setting the `HF_HUB_DISABLE_SYMLINKS_WARNING` environment variable. For more details, see https://huggingface.co/docs/huggingface_hub/how-to-cache#limitations.
To support symlinks on Windows, you either need to activate Developer Mode or to run Python as an administrator. In order to see activate developer mode, see this article: https://docs.microsoft.com/en-us/windows/apps/get-started/enable-your-device-for-development
Downloading (…)lve/main/config.json: 100%|████████████████████████████████████████████████████| 570/570 [00:00<?, ?B/s]
Downloading (…)solve/main/vocab.txt: 100%|███████████████████████████████████████████| 232k/232k [00:00<00:00, 531kB/s]
Downloading (…)/main/tokenizer.json: 100%|███████████████████████████████████████████| 466k/466k [00:00<00:00, 730kB/s]
Downloading model.safetensors: 100%|████████████████████████████████████████████████| 440M/440M [00:20<00:00, 21.7MB/s]
We strongly recommend passing in an `attention_mask` since your input_ids may be padded. See https://huggingface.co/docs/transformers/troubleshooting#incorrect-output-when-padding-tokens-arent-masked.

Building a Hugging Face Transformers Model

With our data preprocessed, we’re ready to create a Hugging Face Transformers model. We’ll start with a basic model configuration:

# Load the pre-trained BERT model
model = AutoModel.from_pretrained("bert-base-uncased")

# Define a simple classifier on top of the BERT model
class Classifier(torch.nn.Module):
    def __init__(self, model, num_classes):
        super(Classifier, self).__init__()
        self.model = model
        self.dropout = torch.nn.Dropout(0.1)
        self.classifier = torch.nn.Linear(768, num_classes)

    def forward(self, input_ids):
        output = self.model(input_ids)
        pooled_output = output.pooler_output
        output = self.dropout(pooled_output)
        return self.classifier(output)

# Create the classifier model
num_classes = 2  # binary classification
classifier_model = Classifier(model, num_classes)

# Set up the optimizer and loss function
optimizer = torch.optim.Adam(classifier_model.parameters(), lr

loss_fn = torch.nn.CrossEntropyLoss()

Training the Model

Now, let’s train the Hugging Face Transformers model on our IMDb movie reviews dataset:

# Training loop
num_epochs = 3

for epoch in range(num_epochs):

    # Forward pass
    outputs = classifier_model(X_train)
    loss = loss_fn(outputs, y_train)

    # Backpropagation and optimization

    print(f"Epoch {epoch + 1}/{num_epochs}, Loss: {loss.item()}")

Evaluating the Model

To assess the model’s performance, we need to make predictions on the test set and compare them to the actual labels:

# Evaluation
with torch.no_grad():
    predictions = classifier_model(X_test)

# Convert predictions to labels
predicted_labels = torch.argmax(predictions, dim=1)

# Calculate the accuracy of the model
accuracy = accuracy_score(y_test, predicted_labels)
print(f"Model Accuracy: {accuracy}")

Visualizing the Results

Visualization is a powerful tool for comprehending your model’s performance. Let’s create a confusion matrix to visualize how well our model is doing:

from sklearn.metrics import confusion_matrix
import seaborn as sns

# Create a confusion matrix
cm = confusion_matrix(y_test, predicted_labels)

# Visualize the confusion matrix
plt.figure(figsize=(8, 6))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=['Negative', 'Positive'], yticklabels=['Negative', 'Positive'])
plt.title('Confusion Matrix')

Fine-Tuning and Hyperparameter Optimization

Hugging Face Transformers allows fine-tuning of pre-trained models for specific tasks. You can experiment with different hyperparameters, layers, and training strategies to improve your model’s performance.


Hugging Face Transformers is a remarkable tool for anyone looking to dive into the world of natural language processing and machine learning with Python. With its pre-trained models and user-friendly interface, you can quickly build powerful NLP solutions.

This guide has taken you from the fundamentals to advanced techniques of Hugging Face Transformers in Python 3, with practical examples and plots. It’s been quite a journey, and you’re well on your way to mastering the art of NLP. Keep experimenting, fine-tuning, and learning, and you’ll reach the pinnacle of Python machine learning in no time. Happy coding!

In 2500 words, we’ve unraveled the potential of Hugging Face Transformers in Python 3, equipped you with the knowledge to build NLP models, and guided you in fine-tuning and optimizing your creations. Whether you’re a beginner or a Python pro, you’re now ready to unlock the possibilities of natural language understanding and make an impact in the world of machine learning.

Also, check out our other playlist Rasa ChatbotInternet of thingsDockerPython ProgrammingMQTTTech NewsESP-IDF etc.
Become a member of our social family on youtube here.
Stay tuned and Happy Learning. ✌🏻😃
Happy coding! ❤️🔥

Leave a Reply