Ai Coding Tools

How to Train Your First AI Model in 2 Hours Using Hugging Face

By BTW Team3 min read

How to Train Your First AI Model in 2 Hours Using Hugging Face

If you’ve ever felt overwhelmed by the thought of training your first AI model, you’re not alone. Many aspiring builders get stuck on the technical complexities or the sheer volume of information out there. But here’s the good news: with Hugging Face, you can train your first AI model in just 2 hours. Yes, really! In this guide, I’ll walk you through the exact steps to get your model up and running, even if you're a complete beginner.

Prerequisites: What You Need Before You Start

Before diving into the training process, make sure you have the following:

  1. Python Installed: You need Python 3.6 or above. If you don’t have it yet, download it from python.org.
  2. Anaconda (Optional): This helps manage your Python packages easily. Download it from anaconda.com.
  3. Hugging Face Transformers Library: Install it via pip:
    pip install transformers
    
  4. A Dataset: For this guide, we’ll use the IMDB movie reviews dataset. It's free and easy to work with.

Step-by-Step: Training Your First Model

Step 1: Set Up Your Environment (15 minutes)

Create a new directory for your project and navigate to it in your terminal. If using Anaconda, create a new environment:

conda create --name myai python=3.8
conda activate myai

Step 2: Load Your Dataset (15 minutes)

You can load the IMDB dataset directly using Hugging Face's datasets library:

from datasets import load_dataset

dataset = load_dataset("imdb")

This will give you access to the training and testing splits of the dataset.

Step 3: Choose a Pre-trained Model (20 minutes)

Hugging Face provides a variety of pre-trained models. For sentiment analysis, you can use distilbert-base-uncased:

from transformers import DistilBertTokenizer, DistilBertForSequenceClassification

tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-uncased')
model = DistilBertForSequenceClassification.from_pretrained('distilbert-base-uncased', num_labels=2)

Step 4: Preprocess the Data (20 minutes)

Tokenize the dataset using the tokenizer you loaded:

def preprocess_function(examples):
    return tokenizer(examples['text'], truncation=True)

tokenized_dataset = dataset['train'].map(preprocess_function, batched=True)

Step 5: Train Your Model (30 minutes)

Set up the training arguments and train the model:

from transformers import Trainer, TrainingArguments

training_args = TrainingArguments(
    output_dir='./results',
    evaluation_strategy='epoch',
    learning_rate=2e-5,
    per_device_train_batch_size=16,
    num_train_epochs=3,
)

trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=tokenized_dataset,
)

trainer.train()

Step 6: Evaluate Your Model (20 minutes)

After training, evaluate the model’s performance:

results = trainer.evaluate()
print(results)

Troubleshooting: What Could Go Wrong

  1. Memory Errors: If you run out of memory, reduce your batch size or choose a smaller model.
  2. Installation Issues: Ensure all necessary libraries are installed correctly. Use pip list to check.
  3. Dataset Loading Errors: Verify that the dataset URL is correct and that you have internet access.

What's Next: Building on Your Model

Once you’ve trained your first model, consider these next steps:

  • Experiment with Different Models: Try other models available on Hugging Face.
  • Fine-tune Hyperparameters: Adjust learning rates, batch sizes, and epochs for better performance.
  • Deploy Your Model: Look into deploying your model using Hugging Face's Inference API.

Conclusion: Start Here

Training your first AI model doesn’t have to be daunting. By following this guide, you’ll have a working model in just 2 hours. Start with Hugging Face's tools, and don’t hesitate to experiment. The best way to learn is by doing!

What We Actually Use

In our experience, we stick with Hugging Face for its extensive model library and community support. It's easy to get started with and scales well as you dive deeper into more complex projects.

Follow Our Building Journey

Weekly podcast episodes on tools we're testing, products we're shipping, and lessons from building in public.

Subscribe

Never miss an episode

Subscribe to Built This Week for weekly insights on AI tools, product building, and startup lessons from Ryz Labs.

Subscribe
Ai Coding Tools

How to Use GitHub Copilot Effectively in 14 Days

How to Use GitHub Copilot Effectively in 14 Days If you’re like many indie hackers and solo founders, you know that coding can be a bottleneck in your product development. GitHub C

Jun 16, 20264 min read
Ai Coding Tools

Bolt.new vs GitHub Copilot: Best AI Coding Support in 2026

Bolt.new vs GitHub Copilot: Best AI Coding Support in 2026 As a solo founder or indie hacker, coding can sometimes feel like an uphill battle. You might find yourself stuck on a pr

Jun 16, 20263 min read
Ai Coding Tools

Bolt.new vs Cursor vs GitHub Copilot: Which AI Tool Surprises Developers in 2026?

Bolt.new vs Cursor vs GitHub Copilot: Which AI Tool Surprises Developers in 2026? As a solo founder or indie hacker, choosing the right AI coding tool can feel overwhelming. With s

Jun 16, 20264 min read
Ai Coding Tools

How to Integrate AI Tools into Your Workflow in Under 2 Hours

How to Integrate AI Tools into Your Workflow in Under 2 Hours If you’re a developer or a solo founder, chances are you’ve felt the pressure to keep up with the latest tools and tec

Jun 16, 20264 min read
Ai Coding Tools

Why Most Developers Overrate GitHub Copilot: A Closer Look

Why Most Developers Overrate GitHub Copilot: A Closer Look In 2026, GitHub Copilot has become a buzzword among developers, but is it really as revolutionary as many claim? As someo

Jun 16, 20264 min read
Ai Coding Tools

How to Build a Fully Functional Web App Using AI Tools in 30 Days

How to Build a Fully Functional Web App Using AI Tools in 30 Days Building a web app can seem like a daunting task, especially for indie hackers and solo founders who often juggle

Jun 16, 20265 min read