AI Concepts in Python

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.

  • Intermediate
  • 3 min read
  • 1 runnable example
What you'll learn
  • Weighted sums and bias
  • Sigmoid, ReLU and tanh
  • Why networks need non-linear activations
One neuron
  1. 1x1, x2Inputs
  2. 2z = w·x + bWeighted sum: z = -0.7
  3. 3sigmoid(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.

From a judge to a neuron
inputs🍽️ The scores for each criterion
weights⚖️ How much each criterion counts
dot(inputs, weights) + bias🧮 The weighted total plus a personal bias
sigmoid(z), relu(z)📣 How strongly the judge reacts
Weighted sum first, activation second.
In one sentence

An 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

  1. Weights say how important each input is; the bias shifts the result up or down.
  2. Without an activation, stacking neurons would still give only a straight-line (linear) model.
  3. Sigmoid outputs a value between 0 and 1, so it suits probabilities.
  4. 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

Run code →
main.py
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)}")
Get a Run button for this example

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.

  1. Change the inputs or weights and predict the output before running.
  2. Print the intermediate values to follow each step of the calculation.
  3. Solve the linked challenges below to implement the idea yourself.
Run in the Python compiler →

Common mistakes

Forgetting the activation

Without a non-linear activation, stacked layers collapse into one linear function.

Problem
output = dot(inputs, weights) + bias
Fix
output = relu(dot(inputs, weights) + bias)

Overflow in sigmoid

math.exp(-z) overflows for very negative z such as -1000.

Problem
return 1 / (1 + math.exp(-z))
Fix
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.

All challenges