Neural Networks · Neural Networks · 5 min read
From a neuron to a layer
A single neuron turns a list of inputs into one number. That is rarely enough. A layer is simply many neurons placed side by side, all reading the same inputs at the same time, each producing its own output. The useful trick is that the whole layer can be written as a single equation.
A layer is many neurons reading the same inputs at once. Stacking their weights into a matrix lets one equation describe the entire layer.
Many neurons, same inputs
Give the same input vector to different neurons. Neuron has its own weights and its own bias , so it computes its own sum exactly as before:
Here is neuron ‘s raw sum, is that neuron’s weight vector, and is its bias. Nothing here is new: it is lesson 1 repeated once per neuron.
One equation for the whole layer
Writing separate sums is tedious. Instead, stack every neuron’s weights as the rows of a single weight matrix , and stack the biases into one vector. The whole layer becomes:
Symbol by symbol:
- is the input vector, with inputs shared by every neuron.
- is the weight matrix. It has rows (one per neuron) and columns (one per input). The entry is the weight from input into neuron .
- is the bias vector, one bias per neuron.
- is the vector of raw sums, where row is exactly .
- is applied to each entry on its own, so .
- is the layer’s output: numbers, one per neuron.
A worked example
Take two inputs and a layer of two neurons. Neuron 1 reuses lesson 1’s weights, and neuron 2 gets its own:
The raw sums are and . Applying the sigmoid to each gives the layer output. The same calculation in code:
import math
# two inputs, shared by every neuron in the layer
x = [2.0, 3.0]
# each row is one neuron's weights; this layer has two neurons
W = [[0.5, -1.0],
[1.0, 0.5]]
# one bias per neuron
b = [1.0, -2.0]
# sigmoid activation, applied to one number
def sigmoid(t):
return 1 / (1 + math.exp(-t))
# for each neuron: weighted sum of inputs, add its bias, then activate
a = [sigmoid(sum(w_i * x_i for w_i, x_i in zip(row, x)) + b_j)
for row, b_j in zip(W, b)]
print(a) # [0.2689414213699951, 0.8175744761936437]
The first output reuses the sigmoid value from lesson 2, confirming that a layer really is just the single neuron repeated and gathered into one equation.
In the next lesson, we will feed one layer’s output into another layer, which is what makes a network deep.