A neural network's forward pass

This guide runs a tiny neural network forward: three samples through a hidden layer of three units, then an output layer of two. It covers the matrix product, adding a bias, the ReLU, picking each sample's winning class and a loss. In NumPy the whole thing is a few lines:

h = np.maximum(inputs @ w + b, 0)
out = h @ w2

The data and the weights

Each row of inputs is one sample with two features. w holds the hidden layer's weights: one row per input feature and one column per hidden unit. b holds one bias per hidden unit.

inputs := [[1, 2], [0, -1], [3, 1]]
w := [[1, -1, 0], [2, 1, -1]]
b := [0, 1, 2]

Whole numbers keep the results easy to check by hand. Decimals work the same way.

The matrix product

둗 is reduce, inner, sum. Between two matrices it is the matrix product, NumPy's inputs @ w:

inputs := [[1, 2], [0, -1], [3, 1]]
w := [[1, -1, 0], [2, 1, -1]]
둗 inputs w
⇒ [[5, 1, -2], [-2, -1, 1], [5, -2, -1]]Try it

Each sample now has three numbers, one per hidden unit. The first is 1×1 + 2×2 = 5.

Adding the bias

The bias is a list of three, and the product is a matrix with rows of three. Garo does not stretch one shape to fit another on its own, the way NumPy broadcasts:

inputs := [[1, 2], [0, -1], [3, 1]]
w := [[1, -1, 0], [2, 1, -1]]
b := [0, 1, 2]
(둗 inputs w) + b
⇒ line 4: !type: + between ranks 2 and 1: only a scalar extends implicitly; align other ranks with lift (ㅂ, §3.1)Try it

You say how to line them up with the lift family, ㅂ. 박 lines a shorter shape up with a longer one from the right, the way NumPy does, and its final ㄱ says what to do if lengths differ (extend the shorter with absent values). The function to apply goes glued on the left:

inputs := [[1, 2], [0, -1], [3, 1]]
w := [[1, -1, 0], [2, 1, -1]]
b := [0, 1, 2]
+박 (둗 inputs w) b
⇒ [[5, 2, 0], [-2, 0, 3], [5, -1, 1]]Try it

The bias row was added to every row. Making this a word you write keeps a mistaken shape from slipping through silently. The same step as a map over rows also works: {x + b}가 adds b to each row.

The ReLU

긱 0 is the larger of each value and 0:

inputs := [[1, 2], [0, -1], [3, 1]]
w := [[1, -1, 0], [2, 1, -1]]
b := [0, 1, 2]
긱 0 (+박 (둗 inputs w) b)
⇒ [[5, 2, 0], [0, 0, 3], [5, 0, 1]]Try it

That is the hidden layer's output, h. Read the line from the right: multiply, add the bias, clamp.

The output layer

A second matrix product takes the three hidden units down to two outputs per sample:

inputs := [[1, 2], [0, -1], [3, 1]]
w := [[1, -1, 0], [2, 1, -1]]
b := [0, 1, 2]
w2 := [[1, 0], [0, 1], [-1, 1]]
h := 긱 0 (+박 (둗 inputs w) b)
둗 h w2
⇒ [[5, 2], [-3, 3], [4, 1]]Try it

The winning class

To classify each sample, take the position of its largest output (NumPy's argmax(axis=1)). 샂 gives the positions that would sort a row, smallest first. Negating the row with 깃 first puts the largest first, and 엄 0 takes that position. The block runs on each row through 가:

out := [[5, 2], [-3, 3], [4, 1]]
{엄 0 (샂 (깃 x))}가 out
⇒ [0, 1, 0]Try it

Samples 0 and 2 pick class 0, and sample 1 picks class 1.

Probabilities

A network usually turns its outputs into probabilities with a softmax, which needs the exponential function. Garo has no exponential yet, so a softmax cannot be written today.

What you can write is a plain share of the total, for outputs that are not negative. Dividing each row by its sum makes the row add up to 1:

{x ÷ 닫 x}가 [[1, 3], [2, 2]]
⇒ [[0.25, 0.75], [0.5, 0.5]]Try it

The loss

The mean squared error compares the outputs with the targets. Subtract, square each difference with ×~, then take the mean of everything with 등 (reduce, whole, balanced):

out := [[5, 2], [-3, 3], [4, 1]]
target := [[3, 1], [0, 3], [6, 0]]
등 ×~ out - target
⇒ 3.1666666666666665Try it

Read it from the right again: subtract, square, average. 당 instead of 등 gives the mean per sample, along each row:

out := [[5, 2], [-3, 3], [4, 1]]
target := [[3, 1], [0, 3], [6, 0]]
당 ×~ out - target
⇒ [2.5, 4.5, 2.5]Try it

Normalizing a layer

강 is map, along rows, balanced. It rescales each row to mean 0 and variance 1, the core of a layer norm:

강 [[1, 2, 3], [2, 4, 6]]
⇒ [[-1.224744871391589, 0.0, 1.224744871391589], [-1.224744871391589, 0.0, 1.224744871391589]]Try it

The whole pass

Here is the forward pass and its loss in one program:

inputs := [[1, 2], [0, -1], [3, 1]]
w := [[1, -1, 0], [2, 1, -1]]
b := [0, 1, 2]
w2 := [[1, 0], [0, 1], [-1, 1]]
target := [[3, 1], [0, 3], [6, 0]]
h := 긱 0 (+박 (둗 inputs w) b)
out := 둗 h w2
등 ×~ out - target
⇒ 3.1666666666666665Try it

What you used

StepGaroNumPy
matrix product둗 a ba @ b
add a row to every row+박 m vm + v
ReLU긱 0 xnp.maximum(x, 0)
argmax of each row{엄 0 (샂 (깃 x))}가 mm.argmax(axis=1)
mean of everything등 xx.mean()
mean of each row당 xx.mean(axis=1)
normalize each row강 x(x - x.mean(1, keepdims=True)) / x.std(1, keepdims=True)