…
…
AI Fundamentals · Lesson 15 / 31 · Neural network intuition
PreviewIn this lesson you will
You can read the explanation without an account. The exercises and quiz at the end of the lesson are for members.
While looking at the support program list, Elena notices something odd: the linear model from the last lesson said "pass" for a student who studies 16 hours a week but has 59% attendance. The rule is clear, though: a student with attendance below 60 fails. Elena asks: "How do we teach this model both conditions at once?"
In this lesson you will answer Elena's question. By the end you will have the completed version of the table below: for each of three students, two intermediate results and the model's final decision. For now you do not know what the intermediate columns mean; we will build each one step by step.
| Hours | Attendance | Attendance OK | Hours OK | Network output |
|---|---|---|---|---|
| 10 | 70 | ? | ? | ? |
| 16 | 59 | ? | ? | ? |
| 4 | 100 | ? | ? | ? |
In this lesson you will:
In the last lesson you built a linear model: multiply each input by a weight, add the results, and say 1 if the total reaches the threshold, 0 if it does not. In this lesson we give that whole calculation a single name: an artificial neuron. There is no new calculation, only a new name. Why rename it? Because we are about to use this calculation several times and give each copy its own job; calling each one "a neuron" will make that easier.
A neuron has four parts. The inputs are the numbers given to the neuron; for us, the student's weekly study hours and attendance percentage. The weights say how much each input counts toward the sum. The threshold is the limit the sum has to reach. The output is a single number: 1 (pass) if the sum reaches the threshold, 0 (fail) if it does not. The neuron below gives hours a weight of 5, attendance a weight of 1, and its threshold is 120. You remember the decision boundary from the last lesson: 5 × hours + attendance ≥ 120. It is the same model, now as a neuron. Step through the code and watch the total box: it comes out exactly equal to the threshold. Why is the output still 1?
Run the code line by line: see which line runs next, the variable boxes and the output.
hours = 10 attendance = 70 weight_hours = 5 weight_attendance = 1 threshold = 120 total = weight_hours * hours + weight_attendance * attendance if total >= threshold: output = 1 else: output = 0 print("Total:", total) print("Output:", output)
The total is 5 × 10 + 70 = 120. The comparison uses >=, which asks "is it equal or greater?"; since 120 reaches the threshold, the output is 1. This is the detail that most often slips past when you calculate by hand: a total exactly equal to the threshold counts as reaching it.
The name "neuron" comes from the nerve cell in the brain, but the similarity ends there. It is an inspiration, not a copy. An artificial neuron is nothing more than one multiplication, one addition, and one comparison; a real nerve cell works in a far more complicated way. A program that uses a neural network does not "think like a brain" for this reason; it calculates with numbers.
The same calculation works outside school too. Example 1 (outside the case): Should I go to the concert? A friend of yours makes the concert decision with a neuron. It has two inputs: the ticket price (in tens of dollars; 4 means $40) and the number of friends who will come. The price has a weight of −1, a friend has a weight of 3, and the threshold is 2. You remember negative weights from the last lesson: as the price rises, the total falls.
| Price (tens of $) | Friends coming | Total = −1 × price + 3 × friends | Output (threshold 2) |
|---|---|---|---|
| 4 | 2 | −4 + 6 = 2 | 1 (goes) |
| 6 | 2 | −6 + 6 = 0 | 0 (stays home) |
| 1 | 1 | −1 + 3 = 2 | 1 (goes) |
Notice something in this table. The $60 ticket with two friends gives "stays home"; but if three friends come, the total becomes −6 + 9 = 3 and the decision flips to "goes." The surplus of one input covered the shortfall of the other. For a concert that may make sense. For Elena's rule, this is exactly the problem.
We keep Elena's rule simple in this lesson: to pass, a student must both keep attendance at 60 or above and study at least 7 hours a week. If either condition is missing, the student fails. Now run the student from the case, the one who studies 16 hours a week with 59% attendance, through our neuron.
A neuron with weight 5 for hours, weight 1 for attendance, and threshold 120 receives (16 hours, attendance 59). What is the output?
That is the problem: a single neuron has one total, and in the total the surplus of one input covers the shortfall of another. Like the third friend in the concert example, every extra hour of study brings 5 points and fills in for the missing attendance. Running three students through the same neuron gives this table.
Example 2 (case): One neuron, three students
| Hours | Attendance | Total = 5 × hours + attendance | Neuron's output | Correct by the rule |
|---|---|---|---|---|
| 10 | 70 | 120 | 1 | 1 |
| 16 | 59 | 139 | 1 | 0 |
| 4 | 100 | 120 | 1 | 0 |
The neuron said "pass" for all three. The second student's attendance is too low, and the third student's study hours are too low. Remember the picture from the last lesson: a linear model splits students in two with a single straight line. A "this and that" rule asks for a region with corners: students with attendance below 60 are outside on one side, students who study fewer than 7 hours are outside on the other, and what stays inside is an area with a corner. A single straight line cannot make a corner.
Maybe the setting is wrong, you might think. You can test that below: change the weight for hours and the threshold, and look for a setting that gets all three students right.
Change the weight for hours and the threshold: can a single neuron get all three students right?
Pick a value; predict the output first, then look.
1weight_hours = 52threshold = 1203# (hours, attendance, correct output by the rule)4students = [(10, 70, 1), (16, 59, 0), (4, 100, 0)]5correct = 06for hours, attendance, expected in students:7 total = weight_hours * hours + 1 * attendance8 if total >= threshold:9 output = 110 else:11 output = 012 if output == expected:13 correct = correct + 114 print("hours", hours, "attendance", attendance, "total", total,15 "output", output, "expected", expected)16print("Correct:", correct, "/ 3")
hours 10 attendance 70 total 120 output 1 expected 1 hours 16 attendance 59 total 139 output 1 expected 0 hours 4 attendance 100 total 120 output 1 expected 0 Correct: 1 / 3
Faded lines didn't run with this value. None of the twelve combinations gets 3 out of 3. When the weight grows, a student who studies a lot but has 59% attendance passes; when it shrinks, a student with 100% attendance and 4 study hours gets a higher total than a student with 10 hours and 70% attendance. The problem is not the setting: a single neuron computes one total, so it cannot check two separate conditions at the same time.
None of the twelve settings got 3 out of 3. As the weight for hours grows, the student who studies a lot but has low attendance passes; as it shrinks, the student with 100% attendance who studies 4 hours comes out ahead. The problem is not the setting but the structure. The idea "if we find better weights, a single neuron can build any rule" is wrong for this reason: whichever weights you choose, a single neuron computes one total and cannot check two separate conditions at the same time.
The solution is not to search for better weights but to connect neurons to each other. The idea: build a separate neuron for each of the two conditions, then ask a third neuron what those two neurons said.
Neurons that work side by side are called a layer. The layer between the inputs and the output is called the hidden layer, because we do not see the intermediate results it produces; Elena sees only the final decision. The whole structure built from neurons that take a layer's outputs as their inputs is called a neural network. Our network has a hidden layer with two neurons and an output layer with one neuron. Each neuron still does the same job: multiply, add, compare with the threshold. The only thing that changes is whose output it takes as input.
A student's two numbers go through the small network. Put the steps in order.
Click the lines in order; to undo one you placed wrong, click it in the list below.
Shuffled lines
Your order
Now run the student from the case through the network. Be careful: which numbers reach the output neuron?
A student who studies 16 hours a week with 59% attendance goes through the small network. Which two numbers reach the output neuron, and what is the result?
Here the extra study hours can no longer cover the missing attendance. Why? The hours neuron turns both 16 hours and 7 hours into the same answer, 1; how much more than 7 the student studied never reaches the output neuron. Each condition is checked separately in its own neuron, and the output neuron only looks at "how many conditions were met?"
Example 3 (case): Three students go through the network. The table from the start of the lesson is now complete:
| Hours | Attendance | Attendance OK | Hours OK | Network output |
|---|---|---|---|---|
| 10 | 70 | 1 | 1 | 1 (pass) |
| 16 | 59 | 0 | 1 | 0 (fail) |
| 4 | 100 | 1 | 0 | 0 (fail) |
All three students got the result the rule calls for. What a single neuron could not do with any setting, three neurons did together.
The weights decide what the network does, and so do the thresholds. The output neuron has two inputs and both have weight 1, so its total can only be 0, 1, or 2. A threshold of 2 means "both," and a threshold of 1 means "at least one." The code below has four students; the fourth (3 hours, attendance 50) meets neither condition. Change the output neuron's threshold and look at the last line.
Set the output neuron's threshold to 1, 2, and 3. Which rule does each threshold apply?
Pick a value; predict the output first, then look.
1output_threshold = 22# Each student: (hours, attendance)3students = [(10, 70), (16, 59), (4, 100), (3, 50)]4results = []5for hours, attendance in students:6 # Hidden layer: attendance neuron and hours neuron7 attendance_total = 0 * hours + 1 * attendance8 hours_total = 1 * hours + 0 * attendance9 attendance_ok = 1 if attendance_total >= 60 else 010 hours_ok = 1 if hours_total >= 7 else 011 # Output neuron: adds the two clues and compares with its threshold12 total = 1 * attendance_ok + 1 * hours_ok13 result = 1 if total >= output_threshold else 014 print("hours", hours, "attendance", attendance, "clues", attendance_ok,15 hours_ok, "result", result)16 results.append(str(result))17print("Network outputs:", " ".join(results))
hours 10 attendance 70 clues 1 1 result 1 hours 16 attendance 59 clues 0 1 result 0 hours 4 attendance 100 clues 1 0 result 0 hours 3 attendance 50 clues 0 0 result 0 Network outputs: 1 0 0 0
Faded lines didn't run with this value. Threshold 1: 1 1 1 0, anyone who meets at least one condition passes. Threshold 2: 1 0 0 0, only a student who meets both conditions passes; this is Elena's rule. Threshold 3: 0 0 0 0, because the two clues add up to at most 2, nobody can pass. The hidden neurons produce the same clues under all three settings; only the output neuron's threshold changes the rule.
With threshold 2, the network says 1 only for the first student, who meets both conditions: 1 0 0 0. Dropping to threshold 1 lets the second and third students, who each meet one condition, pass as well: 1 1 1 0, which means "at least one is enough." With threshold 3 the total can never reach 3, because there are at most two clues: 0 0 0 0, nobody passes. The code raised no error in any of the three settings. A wrong threshold does not produce an error message; the network quietly applies a different rule. This is why you should always test a network you built by hand with a few examples whose results you already know.
As the number of layers grows, the same idea repeats: the first layer computes simple clues, the next layer computes combinations of the clues, the one after that computes combinations of the combinations. In a network that recognizes photos, the first layers can develop neurons that respond to simple patterns such as edges and color changes, the middle layers to parts such as an eye or an ear, and the last layers to a whole face. Deep learning, a name you heard in the second lesson, means learning with neural networks that have many layers. "Deep" describes the number of layers, not depth of understanding; a deep network is not smarter, only made of more layers.
As you add layers, the number of weights grows quickly. Each neuron connects to every neuron in the previous layer with a weight. Multiplying the neuron counts of two consecutive layers gives the number of weights between them, and adding up those products gives the number of weights in the whole network (thresholds are counted separately).
Example 4 (case): Counting weights
| Network | Layer sizes | Calculation | Number of weights |
|---|---|---|---|
| Our network | 2, 2, 1 | 2 × 2 + 2 × 1 | 6 |
| Medium network | 100, 50, 50, 5 | 100 × 50 + 50 × 50 + 50 × 5 | 7,750 |
| Deep network | 1000, 1000, 1000, 1000, 10 | 3 × (1000 × 1000) + 1000 × 10 | 3,010,000 |
The first number, 2, is the number of inputs: hours and attendance. Our network has 6 weights; with just a few layers and somewhat wider layers, the number passes the millions.
Our network has 6 weights and 3 thresholds; you can read all of them and say what each neuron is for. The neurons even have names: the attendance neuron, the hours neuron. In real networks the number of weights reaches the millions, and no neuron comes labeled "attendance OK." In the last lesson, when you chose a model, you looked at explainability: a decision tree's rule can be read, and a linear model's score and threshold can be stated. For a large neural network, you cannot say why it made a decision by looking at its weights.
One more warning: building a network does not raise accuracy on its own. In the fictional sample data file grades.csv, with 200 students, the single neuron from the start gets 174 students right and the network we built by hand gets 171. The network is slightly less accurate. The difference is elsewhere: 12 students have attendance below 60, and the single neuron says "pass" for one of them while the network says it for none. So the network is faithful to Elena's rule, but the simple rule does not fully describe the real results either: some students meet both conditions and still fail. Which one you choose depends on which kind of error costs more; you cannot know that without measuring. And we wrote these weights by hand; a real network finds them itself from data.
Match each misconception with the fact that corrects it.
For each row, pick its match from the list, then check.
Two more students go through the network. Calculate the hidden neurons first, then the output neuron.
Output(6 hours, attendance 95)
attendance neuron: 0 × 6 + 1 × 95 = 95 → 95 ≥ 60 → 1
hours neuron: 1 × 6 + 0 × 95 = 6 → 6 < 7 → 0
output neuron: 1 × 1 + 1 × 0 = 1 → 1 < 2 → 0 (fails)
(18 hours, attendance 55)
attendance neuron: 0 × 18 + 1 × 55 = 55 → 55 < 60 → 0
hours neuron: 1 × 18 + 0 × 55 = 18 → 18 ≥ 7 → 1
output neuron: 1 × 0 + 1 × 1 = 1 → 1 < 2 → 0 (fails)| Hours | Attendance | Attendance neuron (threshold 60) | Hours neuron (threshold 7) | Output total | Result (threshold 2) |
|---|---|---|---|---|---|
| 6 | 95 | 1 | 0 | 1 | 0 (fails) |
| 18 | 55 | 0 | 1 | 1 | 0 (fails) |
A single neuron would say "pass" for both: 5 × 6 + 95 = 125 and 5 × 18 + 55 = 145, both above 120. In the network, each fails because each misses one condition.
Elena wants to add one more condition to the rule: the homework score must also be 50 or above. A third hidden neuron is added to the network (it looks only at homework, threshold 50). The output neuron now receives three clues; its weights are 1, 1, and 1, and its threshold is 3.
Choosing the wrong threshold for the output neuron. If you write 1 instead of 2, no error message appears, but the network starts saying "at least one" instead of "both," and a student who meets only one condition passes too. In Elena's network, both the student who studies 16 hours with 59% attendance and the student with 100% attendance who studies 4 hours get "pass"; both should have failed. With weights of 1 and 1, threshold 2 means "both clues must be 1" and threshold 1 means "one is enough." Always test a network you built by hand with a few examples whose results you know, especially students who meet only one condition.
Next lesson: In this lesson we chose the weights ourselves. In the next lesson, the model will find them on its own by measuring its error.
Exercises and quiz are for members
This lesson has 7 coding exercises and a 10-question quiz. Sign up free to solve them, save your progress and move on to the next lesson.
OutputHidden layer: attendance neuron (threshold 60) · hours neuron (threshold 7) · homework neuron (threshold 50)
Output neuron: 1 × attendance_ok + 1 × hours_ok + 1 × homework_ok ≥ 3| Hours | Attendance | Homework | Clues | Output total | Result (threshold 3) |
|---|---|---|---|---|---|
| 10 | 70 | 80 | 1, 1, 1 | 3 | 1 (pass) |
| 10 | 70 | 40 | 1, 1, 0 | 2 | 0 (fail) |
The rule generalizes: when the output neuron's threshold equals the number of clues it means "all of them," and when it is 1 it means "at least one." The number of weights grew too: three inputs, three hidden neurons, one output neuron; 3 × 3 + 3 × 1 = 12.
A school library door recognizes two cards: a student card and a staff card. The door gives a neuron two numbers, whether the card read is a student card (1 or 0) and whether it is a staff card (1 or 0); the weights are 1 and 1, and the threshold is 1.
OutputStudent card (1, 0): 1 × 1 + 1 × 0 = 1 → 1 ≥ 1 → door opens
Staff card (0, 1): 1 × 0 + 1 × 1 = 1 → 1 ≥ 1 → door opens
Unknown card (0, 0): 1 × 0 + 1 × 0 = 0 → 0 < 1 → door stays closedHere "at least one" is the right rule. Threshold 1 was a mistake in Elena's network and the right choice at the library door; the rule itself tells you which threshold is right. At this door the inputs are already 1 or 0, so a single neuron is enough. When you need to check two separate conditions on raw numbers, you need a hidden layer first.