Chapter 6 · Part 3
See it, and what's next
Your network hit 99.75% on a problem no straight line can touch. Let's make one prediction, see the curve it learned, and then connect what you built to the frameworks everyone else uses.
Predict a point
A prediction is just a forward pass on a single point, thresholded at 0.5. Add to
net.py:
def predict(point):
_, _, prob = forward(np.array([point], dtype=float))
return int(prob[0, 0] > 0.5)
print(predict([0.5, 0.5])) # same sign -> expect 1
print(predict([-0.5, 0.5])) # opposite -> expect 01
0Both correct — same-sign is class 1, opposite-sign is class 0, exactly the XOR rule the
network discovered on its own.
See the boundary it learned (optional)
Numbers are convincing; a picture is unforgettable. If you install matplotlib
(pip install matplotlib), you can sweep the network's prediction across the whole plane
and watch the curved boundary appear:
import matplotlib.pyplot as plt
# score a fine grid of points across the plane
gx, gy = np.meshgrid(np.linspace(-1, 1, 200), np.linspace(-1, 1, 200))
grid = np.c_[gx.ravel(), gy.ravel()]
_, _, probs = forward(grid)
probs = probs.reshape(gx.shape)
plt.contourf(gx, gy, probs, levels=20, cmap="RdBu") # decision surface
plt.scatter(X[:, 0], X[:, 1], c=y.ravel(), cmap="RdBu", edgecolors="k", s=15)
plt.title("Learned decision boundary")
plt.savefig("boundary.png", dpi=120)You'll get a red-and-blue checkerboard: four quadrants, split by soft curved borders that bend exactly where the classes switch. A single-layer model could only ever draw one straight line through that — the hidden layer is what let your network carve the corners.
The big reveal
Step back and look at what you wrote: a forward pass, a bce loss, and a backward pass —
maybe forty lines of NumPy — that together learn. That backward function is the punchline
of the whole hands-on track:
When you called
loss.backward()in the PyTorch course, it built and ran that exact computation automatically — for a far bigger network, across far more layers. You just wrote autograd by hand.
That's the difference between using a deep-learning framework and understanding one. From here:
- Add a layer, or more units. Stack another
W/band ReLU; the same forward/backward pattern extends. This is how depth is built. - Go multi-class. Swap sigmoid + binary cross-entropy for softmax + cross-entropy and you can classify digits — the very task the PyTorch course handed to a framework.
- See the theory in motion. How a Network Actually Learns animates every step you just coded — the gradient, the chain rule, the backward pass.
- Let a framework take over. Now that you know what it's doing, Your First PyTorch Project will feel less like magic and more like well-earned convenience.
You didn't just train a network — you built the engine that trains all of them.