Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on. You've got this! 🚀
Artificial Neurons
An artificial neuron is the basic unit of a neural network. It receives inputs, performs a calculation, and passes an output to the next layer.
What a Neuron Does
Every artificial neuron does exactly three things:
- 1Receives inputs: numbers passed from the previous layer (or from the raw data)
- 2Calculates a weighted sum: multiplies each input by its weight and adds them together, plus a bias
- 3Applies an activation function: transforms the weighted sum into an output that gets passed to the next layer
This is the same calculation repeated for every neuron in every layer, billions of times during training.
Deep Learning ⊂ Machine Learning ⊂ Artificial Intelligence
A Neuron in Code
import math
class Neuron:
"""A single artificial neuron."""
def __init__(self, weights, bias):
self.weights = weights
self.bias = bias
def forward(self, inputs):
"""
Calculate the neuron's output.
Step 1: Weighted sum
Step 2: Add bias
Step 3: Apply activation (ReLU here)
"""
# Step 1 and 2: weighted sum + bias
z = sum(x * w for x, w in zip(inputs, self.weights)) + self.bias
# Step 3: ReLU activation (explained in the next topic)
output = max(0, z)
return output
def describe(self):
print(f"Weights: {self.weights}")
print(f"Bias: {self.bias}")
# Create a neuron that detects if study time predicts passing
neuron = Neuron(weights=[0.8, 0.3], bias=-2.0)
neuron.describe()
print()
# Test with different students
students = [
([1, 40], "Low effort student"),
([5, 70], "Average student"),
([9, 90], "High effort student"),
]
print("Student predictions:")
for features, description in students:
output = neuron.forward(features)
prediction = "PASS" if output > 0 else "FAIL"
print(f" {description}: output={output:.2f} -> {prediction}")Tip
Tip
Think of a neuron as a tiny decision maker. It looks at its inputs, weighs their importance, and decides how strongly to respond. Neurons in deeper layers look at the outputs of earlier neurons and make more complex decisions.