Ai Coding Tools

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

By BTW Team3 min read

Comparing tools? See the AI tools tested on the show.

In short

A step-by-step guide to help beginners create and train an AI model within a tight two-hour timeframe using Hugging Face's resources.

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.

Watch real AI builds every week

Built This Week is a weekly podcast where Jordan Metzner and Sam Nadler build something real with AI every episode, test new AI tools and talk with founders who build with AI.

See the tools

Watch real AI builds every week

Built This Week is a weekly podcast where Jordan Metzner and Sam Nadler build something real with AI every episode, test new AI tools and talk with founders who build with AI.

See the tools
Ai Coding Tools

3 AI Coding Tools That Are Overrated (and Why You Should Avoid Them)

3 AI Coding Tools That Are Overrated (and Why You Should Avoid Them) In the everevolving landscape of AI coding tools, it's easy to get swept up in the hype. As indie hackers and s

Nov 30, 20263 min read
Ai Coding Tools

5 Ways AI Coding Tools Can Accelerate Your MVP Development in 2026

5 Ways AI Coding Tools Can Accelerate Your MVP Development in 2026 In 2026, the landscape for indie hackers and solo founders has evolved significantly, especially with the advent

Nov 26, 20264 min read
Ai Coding Tools

5 AI Coding Tools That Make Legacy Code Feel Brand New in 2026

5 AI Coding Tools That Make Legacy Code Feel Brand New in 2026 If you’re a developer stuck maintaining legacy code, you know the frustration. It’s like navigating a maze blindfolde

Nov 23, 20264 min read
Ai Coding Tools

10 AI Coding Tools That Will Save You Hours of Debugging in 2026

10 AI Coding Tools That Will Save You Hours of Debugging in 2026 Debugging can feel like a neverending cycle of frustration. Whether you're a solo founder or an indie hacker, spend

Nov 18, 20265 min read
Ai Coding Tools

10 Common Mistakes When Using AI Coding Tools for MVP Development

10 Common Mistakes When Using AI Coding Tools for MVP Development As a founder in 2026, you’re likely aware that AI coding tools can significantly speed up the MVP development proc

Nov 16, 20264 min read
Ai Coding Tools

5 AI Coding Myths Every Beginner Developer Should Ditch in 2026

5 AI Coding Myths Every Beginner Developer Should Ditch in 2026 As a beginner developer, you’ve probably come across a ton of buzz surrounding AI coding tools. But with all the hyp

Nov 12, 20263 min read