The “Hello World” of AI is a program that learns a rule from examples instead of having the rule written into its code. A classic Hello World proves that your toolchain can compile and print. An AI Hello World proves the core loop of machine learning: make a prediction, measure the error, adjust the parameters, repeat.
A neural network that “outputs hello world” does not teach you anything, because there is nothing to learn. In this tutorial you will train a one-neuron model in about 20 lines of Python, watch it discover the rule y = 2x - 1 from six examples, and then break it on purpose so you can recognize the most common training failure.
Two terms come up throughout. Training works like a build step. Instead of compiling source code, it produces an artifact from example data, and here that artifact is just two numbers, the model’s weights. Inference means calling a function with that artifact on new input. The comparison has limits. A build is deterministic and you can read its source. Training is statistical, and you cannot tell what a large model will do by reading its weights the way you read code.
Prerequisites
- Use Python 3.12 to reproduce this run. NumPy 2.3.5 requires Python 3.11 or newer.
- NumPy, pinned here to the tested version.
python -m venv .venv
# macOS/Linux: source .venv/bin/activate
# Windows: .venv\Scripts\activate
pip install numpy==2.3.5The program
Save this as hello_ai.py. The model is the line w * x + b: one weight, one bias, which is exactly what a single neuron without an activation function computes.
import sys
import numpy as np
# Training data: the model never sees the rule y = 2x - 1, only examples.
x = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0])
y = np.array([-3.0, -1.0, 1.0, 3.0, 5.0, 7.0])
learning_rate = float(sys.argv[1]) if len(sys.argv) > 1 else 0.05
w, b = 0.0, 0.0 # the model's two parameters, starting from zero
for epoch in range(500):
prediction = w * x + b
error = prediction - y
loss = np.mean(error ** 2) # mean squared error
w -= learning_rate * np.mean(2 * error * x) # gradient of loss w.r.t. w
b -= learning_rate * np.mean(2 * error) # gradient of loss w.r.t. b
if epoch % 100 == 0:
print(f"epoch {epoch:3d} loss {loss:<10.4g} w {w:<10.4g} b {b:.4g}")
print(f"learned: y = {w:.4g}x {b:+.4g}")
print(f"prediction for x=10: {w * 10 + b:.4g}")Run it
python hello_ai.pyExpected output:
epoch 0 loss 15.67 w 0.8833 b 0.2
epoch 100 loss 3.154e-05 w 1.998 b -0.9929
epoch 200 loss 7.862e-10 w 2 b -1
epoch 300 loss 1.96e-14 w 2 b -1
epoch 400 loss 4.885e-19 w 2 b -1
learned: y = 2x -1
prediction for x=10: 19The model was never told the rule, and x = 10 is not in the training data. It still predicts 19, because it recovered w = 2 and b = -1 from the examples.
What just happened
- Predict. Compute
w * x + bfor every example. With both parameters at zero, every prediction is 0. - Measure. The loss is the mean squared error: the average of the squared differences between predictions and the expected values. It starts at 15.67.
- Adjust. The gradient says in which direction each parameter increases the loss. Gradient descent moves each parameter a small step the other way. The size of the step is the learning rate.
- Repeat. After a few hundred passes (epochs) the loss is close to zero.
Gradient descent is like walking down a hill in thick fog. You cannot see the valley floor, but you can feel which way the ground slopes under your feet, so you take a step downhill and check again. The learning rate is the length of your stride. Where the picture breaks: this example has a single smooth valley, so going downhill always ends at the best answer. Real neural networks have many dimensions, flat stretches and many local valleys, and reaching a good one is not guaranteed.
Large neural networks run the same loop. They have millions or billions of parameters instead of two, non-linear activation functions between layers, and libraries such as PyTorch or TensorFlow compute the gradients automatically through backpropagation.
Break it: a learning rate that is too high
Run the same program with a larger step:
python hello_ai.py 0.2epoch 0 loss 15.67 w 3.533 b 0.8
epoch 100 loss 1.762e+21 w 2.118e+10 b 6.831e+09
epoch 200 loss 2.155e+41 w 2.342e+20 b 7.555e+19
epoch 300 loss 2.636e+61 w 2.591e+30 b 8.356e+29
epoch 400 loss 3.225e+81 w 2.865e+40 b 9.242e+39
learned: y = -2.515e+50x -8.111e+49
prediction for x=10: -2.596e+51In the fog picture, the stride is now so long that each step jumps across the valley and lands higher on the opposite slope. Each step overshoots the minimum by more than the previous error, so the loss grows instead of shrinking. This is called divergence. If your loss rises or turns into inf or nan in a real project, lowering the learning rate or scaling the input data is the first thing to try.
Turn it into a test
Machine learning code deserves tests too. This one runs the script and checks the learned behavior rather than exact parameter values:
import pathlib, subprocess, sys
script = pathlib.Path(__file__).with_name("hello_ai.py")
out = subprocess.run([sys.executable, str(script)],
capture_output=True, text=True, check=True).stdout
value = float(out.strip().splitlines()[-1].split(":")[1])
assert abs(value - 19.0) < 0.01, value
print("PASS")Where to go next
- Add a non-linear rule (for example
y = x²) and see why one neuron is not enough. That is where hidden layers and activation functions come in. - Read how neural networks work for the bigger picture.
- Rebuild the same model with a framework, as described in implementing neural networks with TensorFlow and PyTorch.
As an Amazon Associate I earn from qualifying purchases. For a longer introduction to training and evaluating models, see Aurélien Géron’s Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow (3rd edition). The book is optional; the example above runs with NumPy alone.



