Part 3: Creating AIChapter 8 of 12

Training AI - Teaching Machines to Think

19 min5,200 words334 reading now

Remember training a puppy? Training AI is remarkably similar: show an example, give feedback, adjust behavior, repeat. The key difference? AI can practice millions of times per hour!

🐕The Dog Training Analogy (Extended)

Puppy Training

  • → Show action
  • → Give treat/correction
  • → Puppy learns
  • → Repeat 100 times
  • → Puppy masters trick
  • Takes weeks to months

AI Training

  • → Show example
  • → Calculate error
  • → Adjust weights
  • → Repeat millions of times
  • → AI masters task
  • Takes hours to weeks

The key difference? AI can practice millions of times per hour!

Pre-training vs Fine-tuning: Building vs Decorating

🏗️

Pre-training

Building a House from Scratch

Start with: Empty lot (random weights)
Process: Pour foundation, frame, walls, roof, everything
Time: 6 months
Cost: $500,000
Result: Complete house
In AI terms:
  • • Start: Random numbers
  • • Process: Learn language from scratch using Internet
  • • Time: 3-6 months
  • • Cost: $2-5 million
  • • Result: GPT, LLaMA, etc.
🎨

Fine-tuning

Decorating an Existing House

Start with: Finished house
Process: Paint, furniture, personal touches
Time: 2 weeks
Cost: $5,000
Result: Your perfect home
In AI terms:
  • • Start: Pre-trained model (GPT, LLaMA)
  • • Process: Teach specific knowledge/style
  • • Time: 2-6 hours
  • • Cost: $0-100
  • • Result: Your specialized AI

Real Training Example: Teaching AI to Write Emails

Training Data Example

Input:
"Request meeting about budget"
Output:
Subject: Budget Discussion Meeting Request

Dear [Name],

I hope this email finds you well. I would like to schedule
a meeting to discuss our Q3 budget allocations.

Would you be available next week? I'm flexible with timing
and happy to work around your schedule.

Best regards,
[Your name]
[... 999 more examples ...]

The Training Loop

Epoch 1

(First pass through data)
AI attempt: "budget meet want discuss now"
Error: Very high
Adjustment: Major weight changes

Epoch 10

AI attempt: "Hello, I want to discuss the budget. Can we meet?"
Error: Medium
Adjustment: Moderate refinements

Epoch 50

AI attempt: "Dear colleague, I'd like to schedule a meeting to discuss our budget. Are you available next week?"
Error: Low
Adjustment: Fine-tuning

Epoch 100

AI attempt: "Perfect professional emails"
Error: Minimal
Adjustment: Training complete!

The Math (Without the Math!)

Here's what's actually happening during training:

The Learning Process:

1. Forward Pass (Making a Guess)

Input → Layer 1 → Layer 2 → ... → Output
"Write email" → [processing] → "Dear Sir/Madam..."

2. Calculate Error (How Wrong Were We?)

Perfect output: "Dear Mr. Smith..."
Our output: "Dear Sir/Madam..." → Error: Medium (too generic)

3. Backward Pass (Figure Out What Went Wrong)

Work backwards through network
"Output was too generic because Layer 5 didn't recognize need for personalization"

4. Adjust Weights (Learn from Mistake)

Strengthen connections that would have been right
Weaken connections that led to mistakes

5. Repeat

Do this thousands of times until perfect!

Learning Rate: The Speed of Learning

Learning rate is like how big steps you take while learning:

Too Fast (High Learning Rate)

Like learning to ride bike at 30 mph

  • • Might overshoot the goal
  • • Unstable, erratic progress
  • • May never converge

Too Slow (Low Learning Rate)

Like learning to ride bike at 0.1 mph

  • • Takes forever
  • • Might get stuck
  • • Wastes computational resources

Just Right

Like learning at walking speed

  • • Steady progress
  • • Reaches goal efficiently
  • • Can fine-tune at the end

Real Code: Fine-tuning in Action

Here's actual code that trains a model (simplified for clarity):

# Load pre-trained model (like buying a house)
model = load_model("llama-7b")

# Load your training data (like choosing decorations)
training_data = load_dataset("my_email_dataset.json")

# Set training parameters (like planning renovation)
training_config = {
    "learning_rate": 0.0001,  # How fast to learn
    "epochs": 3,               # How many times through data
    "batch_size": 4            # Examples per update
}

# Training loop (like actually decorating)
for epoch in range(3):
    for batch in training_data:
        # Make prediction
        prediction = model(batch.input)

        # Calculate error
        error = compare(prediction, batch.output)

        # Update model
        model.adjust_weights(error)

    print(f"Epoch {epoch} complete!")

# Save your fine-tuned model
save_model("my_email_assistant")

The Cost Breakdown: Training Your Own Model

Option 1: Google Colab

Model: 7B parameters
Training time: 4-6 hours
Cost: $0
Limitations:
  • • Session timeouts
  • • Limited GPU time
  • • Must stay connected

Option 2: Cloud GPU

Service: Vast.ai, RunPod
GPU: RTX 3090
Cost: ~$0.40/hour
7B training: ~$2-3 total
13B training: ~$5-10 total

Option 3: Your Own GPU

Hardware: RTX 3090 ($1,500)
Electricity: ~$5 for training
Advantage: Unlimited use after initial investment
Break-even: After ~150 models

Common Training Problems and Solutions

Problem 1: Overfitting (Memorizing Instead of Learning)

Symptom: Perfect on training data, terrible on new data
Like: Student who memorizes test answers but can't solve new problems
Solution:
  • • More diverse training data
  • • Dropout (randomly disable neurons during training)
  • • Early stopping (quit while ahead)

Problem 2: Underfitting (Not Learning Enough)

Symptom: Poor performance even on training data
Like: Student who doesn't study enough
Solution:
  • • More training time
  • • More complex model
  • • Better features

Problem 3: Catastrophic Forgetting

Symptom: Learning new task makes AI forget old tasks
Like: Learning Spanish makes you forget French
Solution:
  • • Lower learning rate
  • • Mix old and new data
  • • Use specialized techniques (LoRA, etc.)
🎯

Hands-On: Train a Simple Model (No Coding!)

The Pattern Recognition Exercise

1. Create "Training Data" (10 examples):

Input: Weather → Output: Clothing
Sunny, 85°F → T-shirt, shorts
Rainy, 60°F → Raincoat, pants
Snowy, 30°F → Winter coat, boots
[... 7 more examples ...]

2. "Train" Yourself:

  • • Study patterns
  • • Notice: Temperature → clothing weight
  • • Notice: Precipitation → waterproof needs

3. Test Your "Model":

New input: "Cloudy, 55°F"
Your output: "Light jacket, pants"
Why? You learned the pattern!

4. This is Exactly How AI Training Works!

  • • Just with millions of examples
  • • And mathematical weight adjustments
  • • But same core concept

The Future: One-Shot and Zero-Shot Learning

Traditional training needs thousands of examples. But new techniques are emerging:

Traditional (Fine-tuning):
Show 10,000 cat photos → AI recognizes cats
Few-shot Learning:
Show 5 cat photos → AI recognizes cats
One-shot Learning:
Show 1 cat photo → AI recognizes cats
Zero-shot Learning:
Describe a cat in words → AI recognizes cats without seeing any!

This is the cutting edge of AI research, getting closer to how humans learn.

🎓 Key Takeaways

  • Pre-training is building from scratch - expensive and time-consuming ($2-5M, 3-6 months)
  • Fine-tuning is decorating - cheap and fast ($0-100, 2-6 hours)
  • Learning rate is critical - too fast overshoots, too slow wastes time
  • Training loop is simple - predict, calculate error, adjust weights, repeat
  • Common problems have solutions - overfitting, underfitting, catastrophic forgetting

Ready to Fine-tune Your Own AI?

In Chapter 9, discover how to specialize AI models for your needs with LoRA, see real before/after examples, and build your own writing assistant!

Continue to Chapter 9
Free Tools & Calculators