Neurons and Activation Functions in Python
An artificial neuron is a tiny function. Build one from scratch: weighted inputs, a bias, then sigmoid or ReLU.
- Weighted sums and bias
- Sigmoid, ReLU and tanh
- Why networks need non-linear activations
- 1
x1, x2Inputs - 2
z = w·x + bWeighted sum: z = -0.7 - 3
sigmoid(z)Activation: 0.332
Inputs → weighted sum → activation
The problem
Every neural network, from a spam filter to a chatbot, is built from one tiny unit repeated millions of times. Before layers and training make sense, you need to see what that single unit computes.
Think of it like this
A neuron is like a judge scoring a dish. Each criterion (taste, look, smell) counts for a different amount, the judge adds a personal bias, and then decides how enthusiastic the final verdict is. The activation function is that last step: a lukewarm total gives a weak reaction, a high total a strong one.
inputs🍽️ The scores for each criterionweights⚖️ How much each criterion countsdot(inputs, weights) + bias🧮 The weighted total plus a personal biassigmoid(z), relu(z)📣 How strongly the judge reactsAn artificial neuron multiplies each input by a weight, adds a bias and passes the total through an activation function that decides its output.
Key ideas
- Weights say how important each input is; the bias shifts the result up or down.
- Without an activation, stacking neurons would still give only a straight-line (linear) model.
- Sigmoid outputs a value between 0 and 1, so it suits probabilities.
- ReLU is fast and is the usual choice for the hidden layers of modern networks.
When will you need this?
- Understanding what each unit inside a neural network calculates.
- Choosing activations: sigmoid for probabilities, ReLU inside hidden layers.
- Reading model code in libraries such as PyTorch, where these steps are built in.
When not to use it
- Real projects: libraries compute neurons in fast batches with NumPy or GPU tensors, not Python loops.
- Problems a simple rule or formula already solves without learning.
See it in action
Move the sliders to change the inputs, weights and bias. Watch the weighted sum z move along the sigmoid and ReLU curves.
Example code
import math
# A neuron multiplies each input by a weight, adds them up and adds a bias.
def dot(a, b):
return sum(x * y for x, y in zip(a, b))
def sigmoid(z):
return 1 / (1 + math.exp(-z)) # squashes any number into (0, 1)
def relu(z):
return max(0.0, z) # keeps positives, turns negatives into 0
inputs = [0.5, -1.0, 2.0]
weights = [0.8, 0.2, -0.5]
bias = 0.1
z = dot(inputs, weights) + bias # weighted sum
print("weighted sum z =", round(z, 3))
print("sigmoid(z) =", round(sigmoid(z), 3))
print("relu(z) =", relu(z))
# The activation decides how strongly the neuron "fires".
for value in [-2, 0, 2]:
print(f"z={value:>2} sigmoid={sigmoid(value):.3f} relu={relu(value)}")
Result of running the code
weighted sum z = -0.7
sigmoid(z) = 0.332
relu(z) = 0.0
z=-2 sigmoid=0.119 relu=0.0
z= 0 sigmoid=0.500 relu=0.0
z= 2 sigmoid=0.881 relu=2
Code explanation
dot() computes the weighted sum z. Sigmoid squashes z into a value between 0 and 1, while ReLU keeps positive values and turns negatives into 0. The loop shows how each activation responds to a negative, zero and positive input.
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Change the inputs or weights and predict the output before running.
- Print the intermediate values to follow each step of the calculation.
- Solve the linked challenges below to implement the idea yourself.
Common mistakes
Forgetting the activation
Without a non-linear activation, stacked layers collapse into one linear function.
output = dot(inputs, weights) + bias
output = relu(dot(inputs, weights) + bias)
Overflow in sigmoid
math.exp(-z) overflows for very negative z such as -1000.
return 1 / (1 + math.exp(-z))
z = max(-500, min(500, z))
return 1 / (1 + math.exp(-z))
Finished this lesson?
Mark it as learned to track your progress.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
- Dot Product of Two Vectors Multiply two vectors element by element and add the results: the core operation of every neuron. Easy
- Activation Functions Apply sigmoid, ReLU, Leaky ReLU or tanh to every value in a list. Easy
- Single Artificial Neuron Compute the output of one neuron: weighted sum of the inputs plus a bias, passed through sigmoid. Easy