A lot of people treat AI as a black box: data goes in, a prediction pops out. This is the finale of my series, and it’s all code. We’ll walk through the model that predicts Titanic survivors and watch it get better in real time.
No scikit-learn shortcuts here. It’s PyTorch Tensors, manual Gradient Descent and our own Matrix Multiplication, so you can see exactly how a machine “learns.” If any of those terms are new, look them up first (or catch the earlier videos in my series: https://www.youtube.com/@WillGWithAI).
The Master Loop: 4 Steps ⚙️
Every neural network trains on the same cycle. Here’s what my train_model function does, step by step:
- Forward pass. Push the passenger data through the weights and wrap the result in a Sigmoid. Sigmoid squashes any number into the 0 to 1 range, so we can read the output as a probability.
- Loss. Compare the model’s prediction to the real survival records, using something like mean squared error or cross-entropy. Lower loss means a better model.
- Backward pass. Call
loss.backward()to get the downhill slope (gradient) for every weight. The loss is a tensor that remembers how it was computed. That’s PyTorch’s automatic differentiation, and it’s the thing a numpy array, Pandas dataframe or plain list can’t give you. - Step. Nudge the weights (our “radio knobs”) to be a little less wrong: subtract gradient × learning rate from each one. Then reset the gradients with
layer.grad.zero_()(more on that below).
Thought Provoker: If an AI is just matrix multiplications and subtractions, at what point does “math” become “intelligence”? Or is “intelligence” just our word for very complex statistics? It still feels a little crazy to me that some tabular data and basic linear algebra can predict Titanic survival this well.
Why Sigmoid (and Normalization) Matter 🔄
You’ll see the results wrapped in torch.sigmoid(). Without it, the model spits out arbitrary, huge numbers.
- Normalization: We divide each input by its max value. Otherwise an Age of 80 looks 80x more important than a Class of 1, just because the number is bigger.
- Sigmoid: Turns the output into a clean 0 to 1 answer.
Challenge: Look at the LogFare column we created. After watching the video, can you explain why we took the log of the fare instead of using the raw dollar amount?
The zero_grad Line 🧭
layer.grad.zero_() trips up a lot of beginners. In PyTorch, gradients accumulate across every backward() call. If you don’t clear them after each step, the model follows directions from where it used to be, not where it is now. In the video I compare it to a GPS: you want it pointing from your current position, not last turn’s.
- Delete that line and see what happens. The model will likely blow up, taking huge erratic steps until the numbers turn into
NaN(Not a Number). - A human parallel? When you learn from a mistake, do you carry the old “bad” data with you, or start fresh? I’m drawing a loose line between brains and AI here, and I’m honestly not sure of my own answer.
What’s Next 🌐
The engine works. Loss went down and we hit 83% accuracy. But running the code (or watching the video) is only half of it. The useful part is knowing why it works and where it breaks. If you watch the video, please dig into any terminology that’s new to you.
- Overfitting: If we ran this loop 10,000 times, would the model get smarter, or just memorize who survived?
- Bias in the data: This data is from 1912. Can the model tell us anything about a shipwreck in 2026?
Seeing how this works at a low level changed how I think about AI, and I’m excited to build on it.