Velamuvi AIAcademy

Lesson 5 of 8 · A First Network

Neurons and Layers

How an artificial neuron works, with weights, a bias and an activation function, and how neurons form input, hidden and output layers you can count.

  • 7 min read
  • Beginner
  • Reviewed
In this lesson you will learn
  • What an artificial neuron computes, in two steps
  • What the weights and the bias do
  • Why every neuron needs an activation function
  • How neurons form layers, and how to count a layer's parameters

One neuron, two steps

An artificial neuron is a small calculation. It was loosely inspired by brain cells, but it is just arithmetic:

  1. Weighted sum. Multiply each input by its weight, add the results, then add the bias.
  2. Activation. Pass that total through an activation function to get the neuron's output.
total  = w1 × x1 + w2 × x2 + w3 × x3 + b
output = activation(total)

A neuron that decides whether to go for a run

Imagine one neuron that decides whether you will go running today. It has three inputs:

  • x1: is it raining? (1 = yes, 0 = no), with weight −4, because you hate rain
  • x2: is a friend coming? (1 = yes, 0 = no), with weight 2, because company helps
  • x3: the temperature in °C, with weight 0.1, because warmer is nicer

The bias is −1: on a cold, lonely, dry day you are slightly against going. A sigmoid turns the total into a probability.

Try it yourselfPython
import math

def sigmoid(z):
    return 1 / (1 + math.exp(-z))

weights = [-4.0, 2.0, 0.1]   # rain, friend, temperature
bias = -1.0

days = {
    "dry, alone, 22 °C": [0, 0, 22],
    "rain, with a friend, 22 °C": [1, 1, 22],
    "dry, with a friend, 8 °C": [0, 1, 8],
    "rain, alone, 5 °C": [1, 0, 5],
}

for name, x in days.items():
    total = sum(w * xi for w, xi in zip(weights, x)) + bias
    print(f"{name:28} total {total:5.1f} -> chance of running {sigmoid(total):.0%}")

Change the weights or the bias and run it again. A person who loves rain would have a positive rain weight. A very keen runner would have a bigger bias.

Weights and bias, visually

For a neuron with one input, the output is sigmoid(w × x + b). Move the sliders: the weight changes how steep the switch is (and flips it if negative), and the bias slides it left or right.

output = sigmoid(1·x + 0)
-8-6-4-20246800.20.40.60.81
output = sigmoid(w·x + b)
Training a network means finding good values for every weight and bias, like moving thousands of these sliders at once.

From neurons to layers

One neuron can only draw a single straight dividing line. Networks become powerful when many neurons are arranged in layers:

  1. Input layer: the raw numbers, such as pixel values. It does no calculation.
  2. Hidden layers: the layers in between. Each turns its inputs into new features. A network with more than one hidden layer is called deep.
  3. Output layer: produces the answer, such as one number for a price or one probability per class.
input layer      hidden layer 1     hidden layer 2     output layer
   x1  ───────►   neuron 1  ─────►   neuron 1  ─────►   P(cat)
   x2  ───────►   neuron 2  ─────►   neuron 2  ─────►   P(dog)
   x3  ───────►   neuron 3  ─────►   neuron 3

In a dense (fully connected) layer, every neuron receives every output of the layer before. Each connection has its own weight, and each neuron has one bias. So a layer of 5 neurons fed by 4 inputs has 4 × 5 = 20 weights plus 5 biases: 25 parameters that training must learn.

Why the activation matters

Without activation functions, every layer would only multiply and add. A stack of multiply-and-add steps is still just one multiply-and-add step, so ten layers would be no more powerful than one. The non-linear activation is what lets each layer bend the result, so the network can learn curved, complicated patterns.

Common mistakes

Practice exercise

Count the weights and biases of a whole network. The second network is a classic design for reading 28 × 28 images of handwritten digits.

Your solutionPython
def count_parameters(layer_sizes):
    # layer_sizes lists every layer, from input to output, for example [784, 128, 64, 10].
    # Each dense layer has (inputs x neurons) weights plus one bias per neuron.
    # Return the total for the whole network.
    return 0

print(count_parameters([2, 2, 1]))
print(count_parameters([784, 128, 64, 10]))

# Output must be exactly:
# 9
# 109386
Knowledge check

What happens if a network with 10 hidden layers has no activation functions at all?

Summary

  • A neuron computes a weighted sum of its inputs, adds a bias, and applies an activation function.
  • Weights say how much each input matters; the bias sets how easily the neuron switches on.
  • Neurons form input, hidden and output layers; more than one hidden layer makes a network deep.
  • A dense layer has inputs × neurons weights plus one bias per neuron.

Next: how a network learns good values for all those weights.

Finished reading? Tick it off to track your progress.