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 Hours | Sleep Hours | Passed |
|---|---|---|
| 1 | 5 | 0 |
| 2 | 6 | 0 |
| 3 | 7 | 1 |
| 4 | 8 | 1 |
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.
Steps:
- Two inputs feed one sum: w1×study + w2×sleep + bias.
- 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)
# Same math, vector-friendly style for later courses
import numpy as np
def sigmoid(x):
return 1 / (1 + np.exp(-x))
X = np.array([[1, 5], [2, 6], [3, 7], [4, 8]], dtype=float)
y = np.array([0, 0, 1, 1], dtype=float)
weights = np.array([0.1, 0.1])
bias = 0.0
lr = 0.1
for epoch in range(1000):
raw = X @ weights + bias
pred = sigmoid(raw)
error = pred - y
loss = np.sum(error ** 2)
weights -= lr * (X.T @ error)
bias -= lr * np.sum(error)
print("weights", weights, "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
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
| Pieces | Our tutorial | Large network |
|---|---|---|
| Inputs | 2 | thousands |
| Weights | 2 + bias | millions |
| Layers | 1 neuron | many stacked layers |
| Update rule | manual loop | automatic backprop |
The rhythm stays: forward → loss → backward-ish updates.
Key takeaways
- More inputs = more weights, same sigmoid and loss.
- Each weight update multiplies error by its input.
- NumPy version is the same math—just matrix shorthand.
Next: name backpropagation, list what we did not cover, and take the final quiz.