Intermediate 25 min

Add a second input

One feature was enough to learn the idea. Real problems usually have many. Same neuron—more terms:

raw = study_hours × weight1 + sleep_hours × weight2 + bias
Study HoursSleep HoursPassed
150
260
371
481

Each input gets its own weight. The model can learn “study pushes pass up, poor sleep pushes down” if the data supports it—in our toy table, both rise together, so weights may look similar. That is still useful: you see the mechanism scale.

Interactive Diagram

This interactive diagram requires JavaScript to be enabled.

Diagram

Steps:

  1. Two inputs feed one sum: w1×study + w2×sleep + bias.
  2. Sigmoid and loss work exactly as before—only the raw formula widens.

Training loop — two inputs


              import math

def sigmoid(x):
  return 1 / (1 + math.exp(-x))

data = [
  (1, 5, 0),
  (2, 6, 0),
  (3, 7, 1),
  (4, 8, 1),
]

weight1, weight2, bias = 0.1, 0.1, 0.0
learning_rate = 0.1

for epoch in range(1000):
  total_loss = 0
  for study_hours, sleep_hours, actual in data:
      raw = study_hours * weight1 + sleep_hours * weight2 + bias
      prediction = sigmoid(raw)
      error = prediction - actual
      total_loss += error ** 2

      weight1 -= learning_rate * error * study_hours
      weight2 -= learning_rate * error * sleep_hours
      bias -= learning_rate * error

  if epoch % 200 == 0:
      print(f"Epoch {epoch}, Loss: {total_loss:.4f}")

print("w1", weight1, "w2", weight2, "bias", bias)

            

Notice the update for weight2 uses sleep_hours exactly like weight1 uses study_hours. That is backprop’s local rule: error × the input that fed that weight.

Live runner — two-feature training

🐍 Python Train with study + sleep
📟 Console Output
Run code to see output...

Predict with two inputs

def predict(study_hours, sleep_hours):
    raw = study_hours * weight1 + sleep_hours * weight2 + bias
    prob = sigmoid(raw)
    label = "Pass" if prob >= 0.5 else "Fail"
    return label, prob

print(predict(2, 6))
print(predict(3, 5))

How this scales

PiecesOur tutorialLarge network
Inputs2thousands
Weights2 + biasmillions
Layers1 neuronmany stacked layers
Update rulemanual loopautomatic backprop

The rhythm stays: forward → loss → backward-ish updates.

Key takeaways

  1. More inputs = more weights, same sigmoid and loss.
  2. Each weight update multiplies error by its input.
  3. NumPy version is the same math—just matrix shorthand.

Next: name backpropagation, list what we did not cover, and take the final quiz.