1. Home
  2. Explore
  3. Computer Science
  4. Backpropagation and Neural Networks

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Backpropagation and Neural Networks

Backpropagation and Neural Networks

1. Optimization Background

A classifier can be represented by a function fff parameterized by weights WWW:

fW(x)f_W(x)fW​(x)
  • xxx is the input data.

  • WWW contains the learnable parameters.

  • The output is a vector of scores, with one score for each possible class.

A loss function measures how well the classifier’s scores agree with the desired labels. For example, the SVM hinge loss penalizes incorrect classifications and classifications that do not have a sufficiently large score margin.

The total loss usually combines two terms:

L(W)=Ldata(W)+λR(W)L(W) = L_{\text{data}}(W) + \lambda R(W)L(W)=Ldata​(W)+λR(W)
  • Data loss, LdataL_{\text{data}}Ldata​, measures classification performance.

  • Regularization loss, R(W)R(W)R(W), expresses a preference for simpler models.

  • λ\lambdaλ controls the strength of regularization.

The goal is to find weights that minimize the total loss:

W∗=arg⁡min⁡WL(W)W^* = \arg\min_W L(W)W∗=argminW​L(W)

Gradient Descent

To minimize the loss, the gradient of the loss with respect to the weights is computed:

∇WL\nabla_W L∇W​L

The gradient points in the direction of steepest increase. Therefore, the negative gradient points in the direction of steepest decrease. Gradient descent repeatedly updates the weights according to:

W←W−η∇WLW \leftarrow W - \eta \nabla_W LW←W−η∇W​L

where η\etaη is the learning rate.

Each update moves the parameters through the loss landscape toward a region of lower loss.

Numerical and Analytic Gradients

There are two main ways to compute gradients.

Numerical Gradient

A numerical gradient approximates a derivative using finite differences:

∂L∂x≈L(x+h)−L(x−h)2h\frac{\partial L}{\partial x} \approx \frac{L(x+h)-L(x-h)}{2h}∂x∂L​≈2hL(x+h)−L(x−h)​

Numerical gradients are:

  • Easy to implement.

  • Useful for checking other gradient implementations.

  • Slow because they require repeated evaluations of the function.

  • Approximate rather than exact.

  • Sensitive to the choice of hhh.

Analytic Gradient

An analytic gradient is derived directly using calculus.

Advantages:

  • Fast once implemented.

  • Exact up to numerical precision.

  • Efficient for large models.

Disadvantages:

  • Deriving the expression can be difficult.

  • Complex expressions create many opportunities for mistakes.

In practice, an analytic gradient is normally used during training, while a numerical gradient is used to verify that the analytic implementation is correct.


2. Computational Graphs

A computational graph represents a function as a sequence of smaller computations.

A computational graph is a graph whose nodes represent individual computational operations and whose edges represent values flowing between those operations.

Each node receives one or more inputs, performs a local operation, and produces an output.

Example: Linear Classifier Loss

A simple classifier might include the following operations:

  1. Multiply the data xxx by the weight matrix WWW.

  2. Produce class scores.

  3. Compute a data loss, such as hinge loss.

  4. Compute a regularization loss.

  5. Add the data and regularization losses.

The total loss is the output of the graph.

The main advantage is modularity. Instead of differentiating one large expression, the function is decomposed into simple operations such as:

  • Addition.

  • Multiplication.

  • Exponentiation.

  • Division.

  • Maximum.

  • Nonlinear activation functions.

Each operation only needs a simple local derivative. The derivatives are then combined using the chain rule.

Why Computational Graphs Are Useful

Computational graphs are particularly important for complex models such as:

  • Convolutional neural networks.

  • Recurrent neural networks.

  • Neural Turing machines.

  • Deep networks with many layers.

  • Models whose computation is unrolled across time.

Manually deriving the complete gradient of such a model would be impractical. Computational graphs allow gradients to be computed locally and combined systematically.


3. Backpropagation

Backpropagation is a recursive application of the chain rule used to compute gradients throughout a computational graph.

The process has two stages:

  1. Forward pass: Compute the output of every node.

  2. Backward pass: Starting at the output, compute gradients moving backward through the graph.

General Chain Rule

Suppose a node computes:

z=f(x)z = f(x)z=f(x)

and a later part of the graph produces a final loss LLL. The chain rule gives:

∂L∂x=∂L∂z∂z∂x\frac{\partial L}{\partial x} = \frac{\partial L}{\partial z} \frac{\partial z}{\partial x}∂x∂L​=∂z∂L​∂x∂z​
  • ∂L∂z\frac{\partial L}{\partial z}∂z∂L​ is the upstream gradient.

  • ∂z∂x\frac{\partial z}{\partial x}∂x∂z​ is the local gradient.

  • Their product is the gradient passed to the previous node.

Thus, every node performs the same basic operation:

  1. Receive an upstream gradient.

  2. Multiply it by the node’s local gradient.

  3. Pass the resulting gradient to its inputs.

Forward and Backward Values

It is useful to distinguish:

  • Forward values: The values produced while evaluating the function.

  • Backward values: The gradients computed while moving backward.

Forward values are often stored because they are needed to calculate local gradients during backpropagation.


4. Scalar Backpropagation Example

Consider the function:

f(x,y,z)=(x+y)zf(x,y,z) = (x+y)zf(x,y,z)=(x+y)z

Introduce an intermediate variable:

q=x+yq = x+yq=x+y

Then:

f=qzf = qzf=qz

Forward Pass

Suppose:

x=−2,y=5,z=−4x=-2,\qquad y=5,\qquad z=-4x=−2,y=5,z=−4

The forward computation is:

q=x+y=−2+5=3q=x+y=-2+5=3q=x+y=−2+5=3

and:

f=qz=3(−4)=−12f=qz=3(-4)=-12f=qz=3(−4)=−12

The computational graph is conceptually:

  1. Compute q=x+yq=x+yq=x+y.

  2. Compute f=qzf=qzf=qz.

Local Gradients

For the addition node:

q=x+yq=x+yq=x+y

the local derivatives are:

∂q∂x=1,∂q∂y=1\frac{\partial q}{\partial x}=1, \qquad \frac{\partial q}{\partial y}=1∂x∂q​=1,∂y∂q​=1

For the multiplication node:

f=qzf=qzf=qz

the local derivatives are:

∂f∂q=z,∂f∂z=q\frac{\partial f}{\partial q}=z, \qquad \frac{\partial f}{\partial z}=q∂q∂f​=z,∂z∂f​=q

Backward Pass

At the output, the gradient of the output with respect to itself is:

∂f∂f=1\frac{\partial f}{\partial f}=1∂f∂f​=1

Gradient with Respect to zzz

Using the local derivative:

∂f∂z=q=3\frac{\partial f}{\partial z}=q=3∂z∂f​=q=3

Gradient with Respect to qqq

Using the local derivative:

∂f∂q=z=−4\frac{\partial f}{\partial q}=z=-4∂q∂f​=z=−4

Gradient with Respect to yyy

The variable yyy affects fff through qqq, so the chain rule gives:

∂f∂y=∂f∂q∂q∂y\frac{\partial f}{\partial y} = \frac{\partial f}{\partial q} \frac{\partial q}{\partial y}∂y∂f​=∂q∂f​∂y∂q​

Substituting:

∂f∂y=(−4)(1)=−4\frac{\partial f}{\partial y} = (-4)(1)=-4∂y∂f​=(−4)(1)=−4

Gradient with Respect to xxx

Similarly:

∂f∂x=∂f∂q∂q∂x\frac{\partial f}{\partial x} = \frac{\partial f}{\partial q} \frac{\partial q}{\partial x}∂x∂f​=∂q∂f​∂x∂q​

Therefore:

∂f∂x=(−4)(1)=−4\frac{\partial f}{\partial x} = (-4)(1)=-4∂x∂f​=(−4)(1)=−4

The complete set of gradients is:

∂f∂x=−4,∂f∂y=−4,∂f∂z=3\frac{\partial f}{\partial x}=-4, \qquad \frac{\partial f}{\partial y}=-4, \qquad \frac{\partial f}{\partial z}=3∂x∂f​=−4,∂y∂f​=−4,∂z∂f​=3

Locality of Backpropagation

Each node only needs to know:

  • Its immediate inputs.

  • Its immediate output.

  • Its local derivatives.

  • The upstream gradient received from later computations.

A node does not need to know the entire function. This is what makes backpropagation scalable.


5. A More Complex Scalar Example

Consider a sigmoid-like function:

f(w,x)=11+e−sf(w,x) = \frac{1}{1+e^{-s}}f(w,x)=1+e−s1​

where:

s=w0x0+w1x1+w2s=w_0x_0+w_1x_1+w_2s=w0​x0​+w1​x1​+w2​

The computation can be decomposed into:

  1. Multiplications such as w0x0w_0x_0w0​x0​ and w1x1w_1x_1w1​x1​.

  2. Addition of the terms to form sss.

  3. Multiplication by −1-1−1.

  4. Exponentiation.

  5. Addition of 111.

  6. Reciprocal operation.

This decomposition avoids manually differentiating the entire expression at once.

Backward Pass Through the Final Reciprocal

For:

f(u)=1uf(u)=\frac{1}{u}f(u)=u1​

the local derivative is:

∂f∂u=−1u2\frac{\partial f}{\partial u}=-\frac{1}{u^2}∂u∂f​=−u21​

If the forward value of uuu is 1.371.371.37, and the upstream gradient is 111, then:

∂f∂u=−11.372≈−0.53\frac{\partial f}{\partial u} = -\frac{1}{1.37^2} \approx -0.53∂u∂f​=−1.3721​≈−0.53

Backward Pass Through Addition

For:

v=u+1v=u+1v=u+1

the local derivative with respect to uuu is:

∂v∂u=1\frac{\partial v}{\partial u}=1∂u∂v​=1

Therefore, the gradient remains approximately:

−0.53⋅1=−0.53-0.53 \cdot 1=-0.53−0.53⋅1=−0.53

Backward Pass Through Exponentiation

For:

v=euv=e^uv=eu

the derivative is:

∂v∂u=eu\frac{\partial v}{\partial u}=e^u∂u∂v​=eu

The upstream gradient is multiplied by this local derivative. If the relevant forward value is u=−1u=-1u=−1, then:

−0.53e−1≈−0.20-0.53e^{-1}\approx -0.20−0.53e−1≈−0.20

Backward Pass Through Multiplication by −1-1−1

For:

v=−uv=-uv=−u

the local derivative is:

∂v∂u=−1\frac{\partial v}{\partial u}=-1∂u∂v​=−1

Therefore:

(−0.20)(−1)=0.20(-0.20)(-1)=0.20(−0.20)(−1)=0.20

Backward Pass Through Addition Branches

When the computation adds several branches, the same upstream gradient is passed to each branch because the derivative of a sum with respect to each input is 111.

Thus, an upstream gradient of 0.200.200.20 produces a gradient of 0.200.200.20 along each addition branch.

Backward Pass Through Multiplication Branches

For a multiplication:

q=abq=abq=ab

the derivatives are:

∂q∂a=b,∂q∂b=a\frac{\partial q}{\partial a}=b, \qquad \frac{\partial q}{\partial b}=a∂a∂q​=b,∂b∂q​=a

The upstream gradient is multiplied by the value of the other input.

For example, if the upstream gradient is 0.200.200.20, x0=−1x_0=-1x0​=−1, and w0=2w_0=2w0​=2, then:

∂L∂w0=0.20(−1)=−0.20\frac{\partial L}{\partial w_0} = 0.20(-1)=-0.20∂w0​∂L​=0.20(−1)=−0.20

and:

∂L∂x0=0.20(2)=0.40\frac{\partial L}{\partial x_0} = 0.20(2)=0.40∂x0​∂L​=0.20(2)=0.40

The calculation uses only local derivatives and forward values cached during the forward pass.


6. Common Computational Gates

Certain operations have useful interpretations during backpropagation.

Addition Gate: Gradient Distributor

For:

z=x+yz=x+yz=x+y

the local derivatives are:

∂z∂x=1,∂z∂y=1\frac{\partial z}{\partial x}=1, \qquad \frac{\partial z}{\partial y}=1∂x∂z​=1,∂y∂z​=1

Therefore, the addition gate sends the same upstream gradient to every input.

An addition gate distributes the upstream gradient equally to all of its inputs.

Maximum Gate: Gradient Router

For:

z=max⁡(x,y)z=\max(x,y)z=max(x,y)

only the input that achieved the maximum affected the output.

If x>yx>yx>y, then:

∂z∂x=1,∂z∂y=0\frac{\partial z}{\partial x}=1, \qquad \frac{\partial z}{\partial y}=0∂x∂z​=1,∂y∂z​=0

If y>xy>xy>x, then:

∂z∂x=0,∂z∂y=1\frac{\partial z}{\partial x}=0, \qquad \frac{\partial z}{\partial y}=1∂x∂z​=0,∂y∂z​=1

The upstream gradient is routed entirely through the input that was selected during the forward pass.

At equal inputs, the derivative is not uniquely defined; an implementation must choose a convention.

Multiplication Gate: Gradient Scaler

For:

z=xyz=xyz=xy

the gradients are:

∂z∂x=y,∂z∂y=x\frac{\partial z}{\partial x}=y, \qquad \frac{\partial z}{\partial y}=x∂x∂z​=y,∂y∂z​=x

Each input scales the upstream gradient by the value of the other input.

A multiplication gate switches and scales gradients: the gradient with respect to one input is scaled by the other input.

Branching and Gradient Addition

If one variable influences multiple later nodes, the total gradient with respect to that variable is the sum of the gradients arriving through all paths.

If xxx affects q1,q2,…,qnq_1,q_2,\ldots,q_nq1​,q2​,…,qn​, then:

∂L∂x=∑k=1n∂L∂qk∂qk∂x\frac{\partial L}{\partial x} = \sum_{k=1}^{n} \frac{\partial L}{\partial q_k} \frac{\partial q_k}{\partial x}∂x∂L​=∑k=1n​∂qk​∂L​∂x∂qk​​

This is the multivariate chain rule.

The gradients add because changing xxx changes every downstream quantity connected to it, so all effects must be combined.


7. Grouping Operations into Larger Gates

A computational graph can be built at different levels of granularity.

A sigmoid function can either be represented as several primitive operations:

  1. Negation.

  2. Exponentiation.

  3. Addition.

  4. Reciprocal.

Or it can be represented as one sigmoid gate:

σ(x)=11+e−x\sigma(x)=\frac{1}{1+e^{-x}}σ(x)=1+e−x1​

The derivative of the sigmoid has the convenient form:

dσ(x)dx=σ(x)(1−σ(x))\frac{d\sigma(x)}{dx} = \sigma(x)\left(1-\sigma(x)\right)dxdσ(x)​=σ(x)(1−σ(x))

If the sigmoid output is 0.730.730.73, then its local gradient is approximately:

0.73(1−0.73)≈0.200.73(1-0.73)\approx 0.200.73(1−0.73)≈0.20

If the upstream gradient is 111, the gradient passed backward is approximately 0.200.200.20.

Using one sigmoid gate produces exactly the same result as using all of its smaller component operations.

Granularity Trade-Off

Grouping operations involves a trade-off:

  • Finer-grained graph: Simpler local derivatives, but more nodes.

  • Coarser-grained graph: Fewer nodes and a more concise graph, but more complex local derivatives.

Any grouping is valid as long as the local gradient for the larger operation is known.


8. Vector-Valued Backpropagation

The same principles apply when variables are vectors or matrices rather than scalars.

The main difference is that gradients become Jacobians.

Jacobian Matrix

Suppose xxx is an nnn-dimensional vector and zzz is an mmm-dimensional vector. The Jacobian of zzz with respect to xxx contains every partial derivative:

∂zi∂xj\frac{\partial z_i}{\partial x_j}∂xj​∂zi​​

It has shape:

m×nm \times nm×n

Each row corresponds to one output dimension, and each column corresponds to one input dimension.

Why Explicit Jacobians Are Often Impractical

Suppose a function maps a 409640964096-dimensional input vector to a 409640964096-dimensional output vector. Its full Jacobian has shape:

4096×40964096 \times 40964096×4096

This contains more than 16 million entries. With batches of inputs, the computational and memory requirements become even larger.

Fortunately, most implementations do not explicitly construct the entire Jacobian.

Element-Wise Operations

Consider an element-wise maximum operation:

f(x)=max⁡(x,0)f(x)=\max(x,0)f(x)=max(x,0)

applied independently to each component of a vector.

Each output component depends only on the corresponding input component. Therefore, the Jacobian is diagonal:

∂fi∂xj=0when i≠j\frac{\partial f_i}{\partial x_j}=0 \quad\text{when }i\ne j∂xj​∂fi​​=0when i=j

The diagonal contains either 111 or 000, depending on whether each input element is in the active region.

Because of this structure, the gradient can be computed element by element without forming the full Jacobian.


9. Vectorized Computational Graph Example

Consider:

f(W,x)=∥Wx∥22f(W,x)=\|Wx\|_2^2f(W,x)=∥Wx∥22​

where:

  • xxx is an nnn-dimensional vector.

  • WWW is an n×nn\times nn×n matrix.

  • q=Wxq=Wxq=Wx is an intermediate vector.

The squared L2L_2L2​ norm is:

∥q∥22=∑iqi2\|q\|_2^2=\sum_i q_i^2∥q∥22​=∑i​qi2​

Thus:

f=qTqf=q^Tqf=qTq

Forward Pass

The graph consists of:

  1. Matrix-vector multiplication: $$ q=Wx $$

  2. Squared L2L_2L2​ norm: $$ f=q_2^2 $$

For a two-dimensional example:

q1=W11x1+W12x2q_1=W_{11}x_1+W_{12}x_2q1​=W11​x1​+W12​x2​
q2=W21x1+W22x2q_2=W_{21}x_1+W_{22}x_2q2​=W21​x1​+W22​x2​

and:

f=q12+q22f=q_1^2+q_2^2f=q12​+q22​

Gradient with Respect to qqq

For each component:

∂f∂qi=2qi\frac{\partial f}{\partial q_i}=2q_i∂qi​∂f​=2qi​

Therefore, in vector form:

∇qf=2q\nabla_q f=2q∇q​f=2q

The gradient has the same shape as qqq. If the forward value of qqq is approximately:

q=[0.220.26]q= \begin{bmatrix} 0.22\\ 0.26 \end{bmatrix}q=[0.220.26​]

then:

∇qf=[0.440.52]\nabla_q f= \begin{bmatrix} 0.44\\ 0.52 \end{bmatrix}∇q​f=[0.440.52​]

Gradient with Respect to WWW

The matrix-vector multiplication is:

qk=∑jWkjxjq_k=\sum_j W_{kj}x_jqk​=∑j​Wkj​xj​

The local derivative is:

∂qk∂Wij={xjif k=i0otherwise\frac{\partial q_k}{\partial W_{ij}} = \begin{cases} x_j & \text{if } k=i\\ 0 & \text{otherwise} \end{cases}∂Wij​∂qk​​={xj​0​if k=iotherwise​

Using the multivariate chain rule:

∂f∂Wij=∑k∂f∂qk∂qk∂Wij\frac{\partial f}{\partial W_{ij}} = \sum_k \frac{\partial f}{\partial q_k} \frac{\partial q_k}{\partial W_{ij}}∂Wij​∂f​=∑k​∂qk​∂f​∂Wij​∂qk​​

Only the term with k=ik=ik=i contributes, giving:

∂f∂Wij=∂f∂qixj\frac{\partial f}{\partial W_{ij}} = \frac{\partial f}{\partial q_i}x_j∂Wij​∂f​=∂qi​∂f​xj​

Since ∂f∂qi=2qi\frac{\partial f}{\partial q_i}=2q_i∂qi​∂f​=2qi​:

∂f∂Wij=2qixj\frac{\partial f}{\partial W_{ij}} = 2q_i x_j∂Wij​∂f​=2qi​xj​

In matrix form:

∇Wf=2qxT\nabla_W f = 2q x^T∇W​f=2qxT

This has the same shape as WWW.

Gradient with Respect to xxx

For the matrix-vector product:

qk=∑iWkixiq_k=\sum_i W_{ki}x_iqk​=∑i​Wki​xi​

the local derivative is:

∂qk∂xi=Wki\frac{\partial q_k}{\partial x_i}=W_{ki}∂xi​∂qk​​=Wki​

The chain rule gives:

∂f∂xi=∑k∂f∂qk∂qk∂xi\frac{\partial f}{\partial x_i} = \sum_k \frac{\partial f}{\partial q_k} \frac{\partial q_k}{\partial x_i}∂xi​∂f​=∑k​∂qk​∂f​∂xi​∂qk​​

In vector form:

∇xf=WT∇qf\nabla_x f = W^T\nabla_q f∇x​f=WT∇q​f

Since ∇qf=2q\nabla_q f=2q∇q​f=2q:

∇xf=2WTq\nabla_x f=2W^Tq∇x​f=2WTq

and because q=Wxq=Wxq=Wx:

∇xf=2WTWx\nabla_x f=2W^TWx∇x​f=2WTWx

Shape Checking

A useful implementation check is that every gradient must have the same shape as the variable with respect to which it is computed:

  • ∇qf\nabla_q f∇q​f has the shape of qqq.

  • ∇Wf\nabla_W f∇W​f has the shape of WWW.

  • ∇xf\nabla_x f∇x​f has the shape of xxx.

Shape checking often catches errors in vectorized gradient calculations.


10. Forward and Backward APIs

Computational graphs map naturally onto modular software.

Each computational node, or gate, implements two functions.

Forward Function

The forward function:

  • Receives the node’s inputs.

  • Computes the node’s output.

  • Caches any forward values required later.

Backward Function

The backward function:

  • Receives the upstream gradient.

  • Computes the local gradient.

  • Applies the chain rule.

  • Returns gradients with respect to the node’s inputs.

Multiplication Gate Example

For:

z=xyz=xyz=xy

the forward operation computes:

z=xyz=xyz=xy

The backward operation receives ∂L∂z\frac{\partial L}{\partial z}∂z∂L​ and returns:

∂L∂x=∂L∂zy\frac{\partial L}{\partial x} = \frac{\partial L}{\partial z}y∂x∂L​=∂z∂L​y
∂L∂y=∂L∂zx\frac{\partial L}{\partial y} = \frac{\partial L}{\partial z}x∂y∂L​=∂z∂L​x

The values xxx and yyy must be cached during the forward pass because they are needed during the backward pass.

Graph Traversal

The full graph is processed as follows:

  1. Process nodes in topological order during the forward pass.

  2. Compute and cache all required forward values.

  3. Process nodes in reverse topological order during the backward pass.

  4. Call each node’s backward function.

  5. Combine gradients when a variable has multiple downstream paths.

This modular design allows complex networks to be assembled from reusable operations.

Deep-Learning Frameworks

Deep-learning libraries follow this same pattern. Their collections of layers or operations may include:

  • Sigmoid layers.

  • Convolutional layers.

  • Maximum or argmax operations.

  • Fully connected layers.

  • Pooling layers.

  • Normalization operations.

Each layer generally provides forward and backward computations. A neural network is formed by stacking these layers, and the framework performs forward and backward passes through the entire stack.


11. Relationship Between Backpropagation and Parameter Updates

Backpropagation computes gradients, but it does not itself update weights.

The full training process separates into two steps:

  1. Gradient computation: Use backpropagation to compute ∇WL\nabla_W L∇W​L.

  2. Optimization update: Use an optimization rule such as gradient descent to update WWW.

For gradient descent:

W←W−η∇WLW\leftarrow W-\eta\nabla_W LW←W−η∇W​L

Thus, backpropagation supplies the information needed by the optimizer. It does not decide the learning rate or the particular update strategy.


Neural Networks

12. From Linear Classifiers to Neural Networks

A linear classifier computes:

f(x)=Wxf(x)=Wxf(x)=Wx

Each row of WWW can be interpreted as a template for one class. The classifier compares the input with one template per class.

A limitation is that a single template may not represent the many different appearances or configurations of a class. For example, a class such as “car” may include:

  • Red cars.

  • Yellow cars.

  • Cars viewed from different directions.

  • Cars with different shapes or viewpoints.

A neural network addresses this limitation by combining multiple intermediate templates and representations.


13. Two-Layer Neural Networks

A basic two-layer neural network stacks two linear transformations with a nonlinear activation between them:

h=ϕ(W1x)h=\phi(W_1x)h=ϕ(W1​x)
f=W2hf=W_2hf=W2​h

where:

  • W1W_1W1​ is the first weight matrix.

  • hhh is the hidden representation.

  • ϕ\phiϕ is a nonlinear activation function.

  • W2W_2W2​ maps the hidden representation to output scores.

A common activation is the element-wise rectified linear unit:

ReLU⁡(a)=max⁡(0,a)\operatorname{ReLU}(a)=\max(0,a)ReLU(a)=max(0,a)

In that case:

h=ReLU⁡(W1x)h=\operatorname{ReLU}(W_1x)h=ReLU(W1​x)

and:

f=W2hf=W_2hf=W2​h

Bias terms are often included in practice:

h=ϕ(W1x+b1)h=\phi(W_1x+b_1)h=ϕ(W1​x+b1​)
f=W2h+b2f=W_2h+b_2f=W2​h+b2​

Importance of Nonlinearity

Without a nonlinear activation, stacked linear layers collapse into a single linear transformation.

For example:

W2(W1x)=(W2W1)xW_2(W_1x)=(W_2W_1)xW2​(W1​x)=(W2​W1​)x

The product W2W1W_2W_1W2​W1​ is still just one matrix, so no additional expressive power is gained. Nonlinearities prevent this collapse and allow the network to represent complex functions.

Nonlinear activation functions are essential because they allow stacked layers to represent functions that cannot be represented by a single linear transformation.

Template Interpretation

The first layer can learn multiple templates. For example, it may learn templates for:

  • Left-facing horses.

  • Right-facing horses.

  • Different car colors.

  • Different object parts.

  • Different visual patterns.

The hidden vector hhh records how strongly each template is detected in the input.

The second-layer weights combine these intermediate scores. A class can receive a high score when:

  • One relevant template has a very high score.

  • Several related templates have moderately high scores.

  • Different combinations of features are present.

Thus, the second layer builds higher-level concepts from lower-level patterns.


14. Deeper Neural Networks

Additional layers can be stacked:

h1=ϕ(W1x)h_1=\phi(W_1x)h1​=ϕ(W1​x)
h2=ϕ(W2h1)h_2=\phi(W_2h_1)h2​=ϕ(W2​h1​)
f=W3h2f=W_3h_2f=W3​h2​

This creates a deeper network with multiple stages of hierarchical computation.

More generally:

hk+1=ϕ(Wk+1hk+bk+1)h_{k+1}=\phi(W_{k+1}h_k+b_{k+1})hk+1​=ϕ(Wk+1​hk​+bk+1​)

Each layer transforms the representation produced by the previous layer.

Deep Neural Networks

A deep neural network is a neural network containing multiple layers of learned transformations and nonlinearities.

Depth allows the network to construct increasingly complex representations:

  • Early layers can detect simple patterns.

  • Middle layers can combine simple patterns into parts or motifs.

  • Later layers can combine parts into objects or semantic categories.

Layer-Counting Terminology

A network with two linear or fully connected transformations is often called:

  • A two-layer neural network.

  • A one-hidden-layer neural network.

A network with three linear transformations is often called:

  • A three-layer neural network.

  • A two-hidden-layer neural network.

The terminology depends on whether the output layer is included in the count.

Efficient Vectorized Computation

A hidden layer usually represents many neurons simultaneously as a vector. Matrix multiplication computes all neuron outputs efficiently:

h=ϕ(Wx+b)h=\phi(Wx+b)h=ϕ(Wx+b)

Instead of computing each neuron separately, one matrix operation computes the entire layer.

The backward pass uses backpropagation to calculate gradients for:

  • Every weight matrix.

  • Every bias vector.

  • Every hidden activation.

  • The input, if needed.


15. Activation Functions

An activation function introduces nonlinearity into a neural network.

Examples include:

  • Sigmoid:

    σ(x)=11+e−x\sigma(x)=\frac{1}{1+e^{-x}}σ(x)=1+e−x1​
  • ReLU:

    ReLU⁡(x)=max⁡(0,x)\operatorname{ReLU}(x)=\max(0,x)ReLU(x)=max(0,x)

Different activation functions have different numerical and modeling properties. The choice of activation function affects:

  • Gradient flow.

  • Optimization behavior.

  • Representational ability.

  • Computational efficiency.

ReLU

The ReLU function is:

ReLU⁡(x)={0x<0xx≥0\operatorname{ReLU}(x)= \begin{cases} 0 & x<0\\ x & x\ge 0 \end{cases}ReLU(x)={0x​x<0x≥0​

Its derivative is:

ddxReLU⁡(x)={0x<01x>0\frac{d}{dx}\operatorname{ReLU}(x)= \begin{cases} 0 & x<0\\ 1 & x>0 \end{cases}dxd​ReLU(x)={01​x<0x>0​

The derivative at x=0x=0x=0 is technically undefined, but implementations choose a convention.

ReLU is computationally simple and is often considered loosely more similar to neural firing behavior than smooth alternatives.


16. Loose Biological Inspiration

Neural networks are partly inspired by biological neurons, but the analogy is highly simplified.

Biological Neuron Components

A biological neuron includes:

  • Dendrites: Receive signals from other neurons.

  • Cell body: Integrates incoming signals.

  • Axon: Carries the output signal to downstream neurons.

  • Synapses: Connections through which neurons communicate.

Computational Analogy

A simplified artificial neuron can be written as:

a=∑iwixi+ba=\sum_i w_ix_i+ba=∑i​wi​xi​+b

followed by an activation function:

y=ϕ(a)y=\phi(a)y=ϕ(a)

The analogy is:

  • Input values xix_ixi​ resemble incoming neural signals.

  • Weights wiw_iwi​ loosely resemble synaptic strengths.

  • The weighted sum resembles signal integration.

  • The activation function loosely resembles neuronal firing or output.

  • The output is passed to connected units.

The activation value can be interpreted loosely as the strength or rate of the signal passed downstream.

Limits of the Analogy

Artificial neural networks are not literal models of biological neurons.

Biological neurons are substantially more complex because:

  • There are many different types of neurons.

  • Dendrites can perform nonlinear computations.

  • Synapses are dynamic systems rather than fixed scalar weights.

  • Neural communication depends on timing and variability.

  • Spike patterns contain information beyond a simple average firing rate.

  • Biological neurons interact through complex temporal and chemical processes.

Consequently, neural networks should be understood primarily as mathematical function approximators, not as faithful simulations of the brain.


17. Fully Connected Neural Networks

A fully connected network uses matrix multiplication at each layer. Every unit in one layer can connect to every unit in the next layer.

A typical network can be written as:

h1=ϕ(W1x+b1)h_1=\phi(W_1x+b_1)h1​=ϕ(W1​x+b1​)
h2=ϕ(W2h1+b2)h_2=\phi(W_2h_1+b_2)h2​=ϕ(W2​h1​+b2​)
f=W3h2+b3f=W_3h_2+b_3f=W3​h2​+b3​

Here:

  • xxx is the input.

  • h1h_1h1​ and h2h_2h2​ are hidden-layer representations.

  • fff is the final vector of class scores.

  • WiW_iWi​ and bib_ibi​ are learned parameters.

  • ϕ\phiϕ is applied element-wise.

Each fully connected layer can be evaluated efficiently using vectorized operations.

The entire network can be trained by:

  1. Performing a forward pass.

  2. Computing a loss from the output scores.

  3. Performing a backward pass using backpropagation.

  4. Updating all parameters using an optimizer.


18. Core Takeaways

Backpropagation computes gradients by starting at the output of a computational graph and recursively applying the chain rule backward through each operation.

Each computational node needs only its local inputs, its local output, its local derivative, and the upstream gradient.

Addition distributes gradients, maximum routes gradients, and multiplication scales gradients by the value of the other input.

Neural networks stack linear transformations and nonlinear activation functions to construct complex, hierarchical functions.

Nonlinearities are necessary because a stack of linear transformations without nonlinear activations is equivalent to a single linear transformation.

Vector and matrix gradients follow the same chain-rule logic as scalar gradients, but their derivatives can be represented using Jacobians and computed efficiently without explicitly forming large Jacobian matrices.

Backpropagation provides the gradients, while gradient descent or another optimizer uses those gradients to update the network’s parameters.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent