Neural Networks

Perceptrons and Linear Layers

A shape-aware introduction to perceptrons and linear layers, connecting affine maps, decision boundaries, activations, and trainable parameters.

Lesson
40

Goal

Turn the affine maps you already know into trainable units.

A perceptron or linear layer does not introduce new linear algebra. It assigns machine-learning roles to familiar objects:

input vector
→ weighted sum
→ bias shift
→ optional activation
→ output

The core computation is an affine map:

z=wTx+b.z=w^Tx+b.

A neural network is built by composing many such maps with nonlinear functions between them.

Why Start Here

Suppose an input has two features:

x=[x1x2].x= \begin{bmatrix} x_1\\x_2 \end{bmatrix}.

We want a model that produces a score saying which side of a boundary the point lies on. Choose weights:

w=[w1w2]w= \begin{bmatrix} w_1\\w_2 \end{bmatrix}

and a bias bb. The score is:

z=wTx+b=w1x1+w2x2+b.z=w^Tx+b=w_1x_1+w_2x_2+b.

The model may classify using the sign:

y^={1,z0,0,z<0.\hat y= \begin{cases} 1,&z\ge 0,\\ 0,&z<0. \end{cases}

The boundary itself is where the model is undecided:

wTx+b=0.w^Tx+b=0.

That equation defines a line in two dimensions, a plane in three dimensions, and a hyperplane in higher dimensions.

Notation / Symbol Table

SymbolPlain-English nameMeaning
xRnx\in\mathbb R^ninput vectorThe nn features supplied to one unit.
wRnw\in\mathbb R^nweight vectorCoefficients controlling how each input feature affects the score.
bRb\in\mathbb RbiasA scalar offset that moves the boundary without rotating it.
z=wTx+bz=w^Tx+bpre-activationThe affine score before applying a nonlinear function.
ϕ\phiactivation functionA scalar function applied to the pre-activation.
a=ϕ(z)a=\phi(z)activationThe unit’s output after the activation function.
WRm×nW\in\mathbb R^{m\times n}weight matrixStacks mm output-unit weight vectors as rows.
bRmb\in\mathbb R^mbias vectorOne bias for each of the mm output units.
z=Wx+bz=Wx+blayer pre-activationThe vector of all unit scores in a linear layer.

A Perceptron Is An Affine Map Plus A Decision Rule

The word linear is used loosely in neural-network programming. The map:

xwTx+bx\mapsto w^Tx+b

is technically affine because it includes the constant term bb. It is linear only when b=0b=0.

The distinction matters geometrically:

The original perceptron used a hard threshold. Modern neural networks more often use differentiable or piecewise-differentiable activations, but the affine core is the same.

The Weight Vector Is Normal To The Boundary

Consider the decision boundary:

wTx+b=0.w^Tx+b=0.

Let x1x_1 and x2x_2 be any two points on that boundary. Then:

wTx1+b=0w^Tx_1+b=0

and:

wTx2+b=0.w^Tx_2+b=0.

Subtract the equations:

wT(x1x2)=0.w^T(x_1-x_2)=0.

So every displacement lying along the boundary is orthogonal to ww. Therefore ww points straight across the boundary.

This gives the weights two simultaneous interpretations:

  1. As coefficients in a weighted sum.
  2. As the normal direction of the decision boundary.

The score changes most rapidly when moving parallel to ww.

What The Bias Does

Holding ww fixed preserves the boundary’s orientation. Changing bb translates the boundary.

For a one-dimensional input:

wx+b=0wx+b=0

has boundary location:

x=bw,x=-\frac{b}{w},

provided w0w\ne0.

In higher dimensions, the signed score scaled by the weight norm gives signed distance from the boundary:

wTx+bw.\frac{w^Tx+b}{\lVert w\rVert}.

The sign tells which side of the boundary contains xx. The magnitude tells how far away it is in the normal direction.

Worked Example: A Two-Feature Perceptron

Let:

w=[21],b=1.w= \begin{bmatrix} 2\\-1 \end{bmatrix}, \qquad b=-1.

For input:

x=[32],x= \begin{bmatrix} 3\\2 \end{bmatrix},

compute:

z=wTx+b=[21][32]1=621=3.z=w^Tx+b = \begin{bmatrix} 2&-1 \end{bmatrix} \begin{bmatrix} 3\\2 \end{bmatrix} -1 =6-2-1 =3.

A hard threshold would classify this point as positive because z>0z>0.

The boundary is:

2x1x21=0,2x_1-x_2-1=0,

or:

x2=2x11.x_2=2x_1-1.

The vector w=(2,1)Tw=(2,-1)^T is perpendicular to this line.

From One Unit To A Linear Layer

A single unit produces one scalar. A layer with mm units produces mm scores.

Let each unit have its own weight vector wiRnw_i\in\mathbb R^n. Stack their transposes as rows:

W=[w1Tw2TwmT]Rm×n.W= \begin{bmatrix} w_1^T\\ w_2^T\\ \vdots\\ w_m^T \end{bmatrix} \in\mathbb R^{m\times n}.

Stack the biases into:

b=[b1b2bm]Rm.b= \begin{bmatrix} b_1\\b_2\\\vdots\\b_m \end{bmatrix} \in\mathbb R^m.

Then every unit can be evaluated at once:

z=Wx+b.z=Wx+b.

The shape check is:

zm×1=Wm×nxn×1+bm×1.\underbrace{z}_{m\times1} = \underbrace{W}_{m\times n} \underbrace{x}_{n\times1} + \underbrace{b}_{m\times1}.

Each row of WW asks a different linear question about the same input.

Worked Example: A Three-Output Layer

Let:

W=[121130],b=[012],x=[21].W= \begin{bmatrix} 1&2\\ -1&1\\ 3&0 \end{bmatrix}, \qquad b= \begin{bmatrix} 0\\1\\-2 \end{bmatrix}, \qquad x= \begin{bmatrix} 2\\-1 \end{bmatrix}.

Then:

Wx=[1(2)+2(1)1(2)+1(1)3(2)+0(1)]=[036].Wx= \begin{bmatrix} 1(2)+2(-1)\\ -1(2)+1(-1)\\ 3(2)+0(-1) \end{bmatrix} = \begin{bmatrix} 0\\-3\\6 \end{bmatrix}.

Adding the bias gives:

z=Wx+b=[024].z=Wx+b = \begin{bmatrix} 0\\-2\\4 \end{bmatrix}.

The layer has transformed a two-dimensional input into a three-dimensional representation.

Why Activations Are Needed

Composing affine maps without nonlinear activations does not create a more expressive class of functions.

Suppose:

z1=W1x+b1z_1=W_1x+b_1

and:

z2=W2z1+b2.z_2=W_2z_1+b_2.

Substitute the first into the second:

z2=W2(W1x+b1)+b2=(W2W1)x+(W2b1+b2).z_2 =W_2(W_1x+b_1)+b_2 =(W_2W_1)x+(W_2b_1+b_2).

The composition is still one affine map.

A nonlinear activation prevents this collapse:

z2=W2ϕ(W1x+b1)+b2.z_2=W_2\phi(W_1x+b_1)+b_2.

The nonlinearity is what lets depth create genuinely new functions.

Common Activation Examples

Hard Threshold

ϕ(z)={1,z0,0,z<0.\phi(z)= \begin{cases} 1,&z\ge0,\\ 0,&z<0. \end{cases}

Useful for understanding the historical perceptron, but unsuitable for ordinary gradient-based training because its derivative is zero almost everywhere and undefined at zero.

Sigmoid

σ(z)=11+ez.\sigma(z)=\frac{1}{1+e^{-z}}.

Maps scores into (0,1)(0,1). It is useful for probabilities and binary outputs, though deep networks often avoid it in hidden layers because large positive or negative inputs produce small gradients.

ReLU

ReLU(z)=max(0,z).\operatorname{ReLU}(z)=\max(0,z).

Piecewise linear and inexpensive. Its derivative is:

ReLU(z)={1,z>0,0,z<0.\operatorname{ReLU}'(z)= \begin{cases} 1,&z>0,\\ 0,&z<0. \end{cases}

At z=0z=0, software chooses a convention, commonly zero.

The Local Derivatives Of A Single Unit

Let:

z=wTx+b.z=w^Tx+b.

Treat xx, ww, and bb as separate inputs to this computation.

The derivative with respect to the input is:

xz=w.\nabla_x z=w.

The derivative with respect to the weights is:

wz=x.\nabla_w z=x.

The derivative with respect to the bias is:

zb=1.\frac{\partial z}{\partial b}=1.

These formulas are symmetric for a reason. The scalar score is the dot product pairing between ww and xx. Holding either side fixed makes the other side’s gradient equal to the fixed vector.

For:

a=ϕ(z),a=\phi(z),

the chain rule gives:

wa=ϕ(z)x,\nabla_w a=\phi'(z)x, xa=ϕ(z)w,\nabla_x a=\phi'(z)w,

and:

ab=ϕ(z).\frac{\partial a}{\partial b}=\phi'(z).

The forward pass stores xx, ww, bb, and zz. The backward pass multiplies the incoming sensitivity by these local derivatives.

Gradients For A Linear Layer

For:

z=Wx+b,z=Wx+b,

suppose a scalar loss LL produces an incoming sensitivity:

zˉ=zLRm.\bar z=\nabla_zL\in\mathbb R^m.

Then:

xL=WTzˉ,\nabla_xL=W^T\bar z, WL=zˉxT,\nabla_WL=\bar z x^T,

and:

bL=zˉ.\nabla_bL=\bar z.

Check the shapes:

WTn×mzˉm×1=xLn×1,\underbrace{W^T}_{n\times m} \underbrace{\bar z}_{m\times1} = \underbrace{\nabla_xL}_{n\times1},

and:

zˉm×1xT1×n=WLm×n.\underbrace{\bar z}_{m\times1} \underbrace{x^T}_{1\times n} = \underbrace{\nabla_WL}_{m\times n}.

The weight gradient is an outer product. Each output sensitivity scales the input vector to produce one row of the matrix gradient.

A Useful Programming View

A linear layer can keep forward data and backward behavior together:

type Vector = readonly number[];
type Matrix = readonly (readonly number[])[];

type LinearCache = Readonly<{
  input: Vector;
}>;

type LinearLayer = Readonly<{
  weights: Matrix;
  bias: Vector;
}>;

type LinearBackward = Readonly<{
  inputGradient: Vector;
  weightGradient: Matrix;
  biasGradient: Vector;
}>;

The layer’s forward pass computes Wx+bWx+b and stores the input. Its backward pass receives zˉ\bar z, then computes the three gradients above.

This is the first reusable trainable operation in the neural-network implementation path.

Connection To Category Theory

The useful connection is composition, not terminology.

A linear layer is a morphism-like computational component with a typed input and output:

R^n → R^m

Layers compose only when adjacent shapes match. The forward maps compose in the ordinary direction. Reverse-mode sensitivities travel backward through the transposed local linear maps.

The nonlinear activation is crucial because a category containing only affine maps remains closed under composition: composing more arrows does not escape the same family of functions.

Why This Matters For Machine Learning

Linear layers appear throughout modern models:

Understanding this layer precisely means much of neural-network architecture becomes repeated composition rather than new mathematics each time.

Common Mistakes

Calling The Bias Part Of The Weight Vector Without Saying So

It is possible to append a constant feature 11 to xx and absorb bb into an enlarged weight vector. That representation is valid, but it hides the distinct role of the bias unless stated explicitly.

Stacking Weight Vectors In The Wrong Orientation

For z=Wx+bz=Wx+b, each output unit’s weights are a row of WW. Columns correspond to input features.

Treating Wx+bWx+b As Nonlinear

The bias makes the map affine, not nonlinear. Nonlinearity comes from ϕ\phi.

Assuming Several Linear Layers Automatically Add Expressiveness

Without nonlinear activations between them, they collapse into one affine map.

Forgetting That Parameters Are Inputs To Differentiation

During inference, WW and bb are fixed. During training, they are variables whose gradients must be computed.

Using A Hard Threshold During Gradient-Based Training

The threshold’s derivative does not provide useful gradients. Smooth or piecewise-linear surrogate activations are used instead.

Exercises

  1. In plain language, explain the separate roles of ww, xx, and bb in z=wTx+bz=w^Tx+b. Which quantities are data, and which are trainable parameters?

  2. Let w=(3,2)Tw=(3,-2)^T and b=4b=4. Write the decision boundary in slope-intercept form. State a normal vector and give one point on each side of the boundary.

  3. Show algebraically that changing bb while holding ww fixed translates a decision boundary without changing its orientation.

  4. For:

    W=[112031],b=[21],x=[211],W= \begin{bmatrix} 1&-1&2\\ 0&3&1 \end{bmatrix}, \qquad b= \begin{bmatrix} 2\\-1 \end{bmatrix}, \qquad x= \begin{bmatrix} 2\\1\\-1 \end{bmatrix},

    compute z=Wx+bz=Wx+b and annotate every object’s shape.

  5. Let z=wTx+bz=w^Tx+b and a=σ(z)a=\sigma(z). Derive wa\nabla_w a, xa\nabla_x a, and a/b\partial a/\partial b using the chain rule.

  6. Prove that two affine layers without an intervening nonlinearity can be represented by one affine layer. Identify the combined weight matrix and bias vector.

  7. For z=Wx+bz=Wx+b, derive xL\nabla_xL, WL\nabla_WL, and bL\nabla_bL from the differential:

    dz=dWx+Wdx+db.dz=dW\,x+W\,dx+db.

    Use the Frobenius inner product to identify the matrix gradient.

  8. A ReLU unit receives z=0.4z=-0.4. What output and local derivative does it produce? What practical training problem can occur if a unit remains in this region for every example?

  9. Diagnose the claim: “The bias controls how important the input features are.” Explain what the bias actually controls and which object controls feature coefficients.

  10. Implement a TypeScript linear layer with:

    • dimension checks,
    • a forward pass returning both output and cache,
    • a backward pass returning input, weight, and bias gradients,
    • tests using finite differences for every parameter and input coordinate.

Readiness Check

You are ready for the next lesson when you can:

Summary

Next Lesson

Lesson 41 introduces activation functions as geometric gates. It will compare sigmoid, tanh, and ReLU, study their derivatives and saturation behavior, and show how nonlinearities change the functions a network can represent.