Paul's Python CoursePPC

Layers and the forward pass

Not met yet

A neural network is layers of neurons. A batch of examples is one row each, and a layer is a weighted sum for every neuron at once: X @ W + b, where W has one column per neuron and b one bias per neuron, added to every row. (n, k) @ (k, m) gives (n, m): the inner sizes must match, and NumPy never turns a matrix round for you. Each layer's output, after its activation, is the next layer's input.

Try it

main.py
import numpy as np

evenings = np.array([[0.9, 0.2, 0.5, 1.0],
                     [0.2, 0.3, 0.4, 0.0]])   # (2, 4)
W1 = np.ones((4, 5)) * 0.1                    # 4 readings in, 5 neurons
b1 = np.zeros(5)

hidden = np.maximum(0, evenings @ W1 + b1)
print(hidden.shape)

Output

Press Run and what your program prints shows up here.

Easy to get wrong

  • Thinks @ turns a matrix round by itself when the shapes don't line up.
  • Thinks @ multiplies two arrays number by number, like *.
  • Thinks a layer's bias adds one number to each example (row), rather than one number to each neuron (column).

Where it comes up