Please enable JavaScript to use CodeHS

AI Engineer

The big picture

AI engineering is still engineering, but the programs you build have billions of adjustable knobs. This tutorial explains what those knobs are, why the training loop is the center of everything, and what you will build by the end of this series.

AI ~15 min Programmer
Vocabulary

Key terms for this tutorial

Here are the words that matter most in this tutorial. Come back here if a term feels fuzzy.

epoch
One full pass through the entire training dataset.
inference
Using a trained model to make predictions on new inputs.
loss
A number that measures how wrong the model is. Training tries to minimize loss.
model
A function with learnable parameters that maps inputs to outputs.
optimizer
An algorithm, like SGD or Adam, that updates model weights using gradients.
parameters
The collection of learnable numbers inside a model.
tensor
A multi-dimensional array of numbers, the basic data type in deep learning.
training loop
The repeated cycle of forward pass, loss, backward pass, and optimizer step that trains a model.

Models

A model is a function with learnable knobs

Every program you have written takes inputs, runs logic, and returns outputs. A machine-learning model does the same thing, except the logic is mostly linear algebra and the exact behavior is determined by numbers called parameters or weights.

A tiny model might have a few hundred weights. GPT-4 has hundreds of billions. The job of an AI engineer is to choose the shape of the function, supply data, and write the loop that finds good weights.

Core idea: the model does not 'know' anything at first. It is a big, differentiable expression. Training searches for the parameter values that make the expression produce useful outputs.

Stuck or curious? Explain this to me on ChatGPT

Prompt: I am a beginner AI engineer working through the CodeHS tutorial 'AI Engineer: The Big Picture'. Explain the section 'A model is a function with learnable knobs' in plain English. Include simple analogies, vocabulary definitions, and a short example.


Training loop

The universal training loop

Almost every deep-learning system follows the same four steps: make a prediction, measure the error with a loss function, compute how each weight contributed to the error with gradients, and nudge the weights in the direction that lowers the loss.

  • Data → examples of inputs and desired outputs
  • Model → a differentiable function with weights
  • Loss → a score for how wrong the model is
  • Gradients → how to change the weights
  • Optimizer → the rule for updating the weights

This loop scales from a three-weight AND gate to ChatGPT. The math is the same; only the size of the model and the amount of data change.

Stuck or curious? Explain this to me on ChatGPT

Prompt: I am a beginner AI engineer working through the CodeHS tutorial 'AI Engineer: The Big Picture'. Explain the section 'The universal training loop' in plain English. Include simple analogies, vocabulary definitions, and a short example.


Stack

What an AI engineer actually touches

You do not need to invent new math to build models. You need a framework (usually PyTorch or JAX), a way to load and clean data, compute (GPUs or TPUs), and experiment-tracking tools.

Most of the work is debugging data, choosing architectures, tuning hyperparameters, and watching loss curves. Writing the training loop is the easy part; getting the model to generalize is the art.

Stuck or curious? Explain this to me on ChatGPT

Prompt: I am a beginner AI engineer working through the CodeHS tutorial 'AI Engineer: The Big Picture'. Explain the section 'What an AI engineer actually touches' in plain English. Include simple analogies, vocabulary definitions, and a short example.


Code

The loop in PyTorch

Here is the same loop expressed in code. Do not worry about every line yet; you will understand every piece by the end of the series.

train_loop.py
import torch
import torch.nn as nn

# A tiny linear model: 1 input -> 1 output
model = nn.Linear(1, 1)

# Squared-error loss and an optimizer
loss_fn = nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)

# Fake data: we want the model to learn y = 2*x + 1
xs = torch.tensor([[0.0], [1.0], [2.0], [3.0]])
ys = torch.tensor([[1.0], [3.0], [5.0], [7.0]])

for epoch in range(100):
    optimizer.zero_grad()
    pred = model(xs)
    loss = loss_fn(pred, ys)
    loss.backward()
    optimizer.step()

print(f"Learned: y = {model.weight.item():.2f}*x + {model.bias.item():.2f}")
Output
Learned: y = 2.00*x + 1.00

Line-by-line: train_loop.py

Read this slowly. Each line does one small, concrete thing.

  1. import torch — Import the torch library so we can use its built-in tools.
  2. import torch.nn as nn — Import the torch library so we can use its built-in tools.
  3. model = nn.Linear(1, 1) — Add a linear layer that multiplies inputs by a weight matrix and adds a bias.
  4. loss_fn = nn.MSELoss() — Choose MSELoss as the loss function; it tells the optimizer how wrong the predictions are.
  5. optimizer = torch.optim.SGD(model.parameters(), lr=0.01) — Create an SGD optimizer that will update the model's weights using gradients.
  6. xs = torch.tensor([[0.0], [1.0], [2.0], [3.0]]) — Create a PyTorch tensor from raw Python numbers.
  7. ys = torch.tensor([[1.0], [3.0], [5.0], [7.0]]) — Create a PyTorch tensor from raw Python numbers.
  8. for epoch in range(100): — Start the training loop. Each epoch is one full pass through the dataset.
  9. optimizer.zero_grad() — Clear out old gradients so they do not accumulate across steps.
  10. pred = model(xs) — Generate predictions by running inputs through the model.
  11. loss = loss_fn(pred, ys) — Compute the loss, which measures how far the prediction is from the target.
  12. loss.backward() — Compute gradients: how much each weight contributed to the loss.
  13. optimizer.step() — Update the model weights using the gradients the optimizer just computed.
  14. print(f"Learned: y = {model.weight.item():.2f}*x + {model.bias.item():.2f}") — Print a result so we can see what the model learned.

Pop-in: You are seeing the same loop again

Almost every deep-learning script repeats the same four steps: predict, compute loss, backpropagate, update weights. Once you can spot that pattern, the rest is just details.

The model starts random. After 100 loops, it discovers the rule hidden in the data. That is the entire trick.

Stuck or curious? Explain this to me on ChatGPT

Prompt: I am a beginner AI engineer working through the CodeHS tutorial 'AI Engineer: The Big Picture'. Explain the section 'The loop in PyTorch' in plain English. Include simple analogies, vocabulary definitions, and a short example. Also walk through the code example in this section step by step.


Check yourself

Check yourself

?

Why are modern models called 'differentiable programs'?

Think about what 'differentiable' means in calculus and why it matters for the training loop.

Because every output is a smooth function of the weights, we can use calculus to compute derivatives. Those derivatives tell us exactly how to adjust each weight to reduce the loss. If the program were not differentiable, we could not do this efficiently.

Stuck or curious? Explain this to me on ChatGPT

Prompt: I am a beginner AI engineer studying 'AI Engineer: The Big Picture'. The practice question 'Why are modern models called 'differentiable programs'?' asks: Think about what 'differentiable' means in calculus and why it matters for the training loop. The answer is: Because every output is a smooth function of the weights, we can use calculus to compute derivatives. Those derivatives tell us exactly how to adjust each weight to reduce the loss. If the program were not differentiable, we could not do this efficiently.. Explain the concept behind this in simple terms.

Stuck or curious? Explain this to me on ChatGPT

Prompt: I am a beginner AI engineer working through the CodeHS tutorial 'AI Engineer: The Big Picture'. Explain the section 'Check yourself' in plain English. Include simple analogies, vocabulary definitions, and a short example.


Quick quiz

Three questions

1. What is a model's 'parameter'?

Stuck or curious? Explain this question to me on ChatGPT

Prompt: I am a beginner AI engineer studying 'AI Engineer: The Big Picture'. The quiz asks: What is a model's 'parameter'? The correct answer is: A number inside the model that is adjusted during training. Explain why that answer is right and why the other choices are wrong, using a simple example.

2. What does the loss function do?

Stuck or curious? Explain this question to me on ChatGPT

Prompt: I am a beginner AI engineer studying 'AI Engineer: The Big Picture'. The quiz asks: What does the loss function do? The correct answer is: It measures how far the model's predictions are from the desired outputs. Explain why that answer is right and why the other choices are wrong, using a simple example.

3. Why can the same training loop scale from tiny models to huge ones?

Stuck or curious? Explain this question to me on ChatGPT

Prompt: I am a beginner AI engineer studying 'AI Engineer: The Big Picture'. The quiz asks: Why can the same training loop scale from tiny models to huge ones? The correct answer is: Because the core idea — differentiable model + loss + gradients + optimizer — scales up. Explain why that answer is right and why the other choices are wrong, using a simple example.

Keep learning

Let's go deeper: scaling laws, data quality, and why size changes model behavior.

Part of the CodeHS tutorial library.