Exploring HuggingFace Transformers: A Beginner's Guide to Portfolio Projects
Written on
Chapter 1: The Importance of a Machine Learning Portfolio
For newcomers in the field of machine learning, having a portfolio is crucial. It serves as tangible evidence of your ability to tackle real-world business problems through data science initiatives. However, executing your project from the ground up can be challenging due to various technical constraints, particularly in deep learning. Utilizing pre-trained models is essential to minimize both computational expenses and training durations.
Fortunately, the HuggingFace Transformers API enables users to access and refine cutting-edge pre-trained machine learning models. HuggingFace is a community-driven platform that seeks to propel AI forward by providing a wide array of models, datasets, and collaborative spaces. This resource is ideal for both novices and seasoned professionals eager to enhance their portfolios with pre-trained models.
In this article, we'll dive into the different HuggingFace Transformers and demonstrate how to effectively use the Transformers API in your machine learning endeavors. Let’s delve into the HuggingFace Transformers.
HuggingFace Transformers: An Overview
HuggingFace Transformers offers a collection of APIs that provide various pre-trained models tailored for multiple applications, including:
- Text Use Cases: Text classification, information extraction, and question answering.
- Image Use Cases: Image detection, classification, and segmentation.
- Audio Use Cases: Speech recognition and audio classification.
- Multimodal Use Cases: Table-based question answering and optical character recognition.
To get started with the Transformers API, we need to first install the transformers package.
As a helpful tip, it’s advisable to set up the Transformers in a virtual environment to avoid any dependency conflicts that might arise with other projects. If you’re unsure how to create a virtual environment, you can refer to my related article.
Alternatively, utilizing cloud services like Google Colab is also a viable option. Regardless, keeping projects in separate environments is a best practice.
Once you've settled on your environment, install the Transformers package using the following command:
pip install transformers
The Transformers API is dependent on either TensorFlow 2.0 or the PyTorch framework. Ensure that you've installed one of these packages beforehand.
With all prerequisites in place, let's initiate the Transformers API. The simplest way to load a pre-trained model from HuggingFace is through the pipeline function.
from transformers import pipeline
The pipeline function is user-friendly and requires you to specify the task you wish to execute.
Text Generation Example
For example, if you're interested in creating a text generation model, you can execute the following command:
generator = pipeline("text-generation")
By specifying the desired task, the system will automatically download the default model if no specific model is provided. For text generation, the default model is GPT-2.
If you’re curious about where the model is saved, it can be found in the directory ~/.cache/huggingface/transformers/. For those needing offline access to all models, further information can be found in the Transformers Installation guide.
Now that we have our pre-trained model, we can utilize it to generate text. Let’s try generating text using the prompt below:
generator("Why do people steal?")
The generated results will be stored in the generated_text variable, allowing you to assess whether the outcome meets your expectations. Should you wish to explore alternative models for text generation, the Model Hub offers a variety of options.
Once you’ve identified a suitable model, you can pass it to the pipeline function like this:
generator = pipeline(model="EleutherAI/gpt-neo-1.3B")
In this instance, I opted for the GPT-Neo model, which is more resource-intensive, so consider increasing your RAM capacity to accommodate larger models.
If you want to utilize a specific tokenizer from another model, you can do so by importing it and passing it to your generator:
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("distilgpt2")
generator = pipeline(task="text-generation", tokenizer=tokenizer)
Exploring More with Transformers API
There’s a wealth of possibilities with the Transformers API, such as:
Image Classification
Image classification involves categorizing images based on their content. The Transformers API simplifies acquiring a model for this task:
classifier = pipeline("image-classification")
In just one line, you've obtained an image classifier model, and you can easily classify images from any source:
Audio Classification
The Transformers API also includes models for classifying audio data. Depending on the model selected, the audio classification results may vary. Here’s how to use the default model:
audio_classifier = pipeline(task="audio-classification")
You can then classify sample audio files with this model:
audio_classifier("sample-3s.mp3")
With HuggingFace Transformers, you can explore countless projects that previously seemed out of reach!
For those interested in dataset examples, particularly from HuggingFace, you can refer to my linked article.
Conclusion
The HuggingFace Transformers API is an invaluable resource for both beginners and experienced practitioners alike. It provides access to pre-trained models that were once challenging to implement due to technical barriers.
With just a single command, you can engage in various tasks, including image classification, text classification, and audio classification.
I hope you find this information helpful!
Connect with me on LinkedIn or Twitter for more insights. Don't miss out on my content; subscribe to my newsletter for deeper knowledge to advance your data science career. If you're not a Medium member yet, please consider signing up through my referral link.
Schedule a DDIChat session with Cornellius Yudha Wijaya at the link below.
Apply to be a DDIChat Expert here.
Subscribe to DDIntel here.