9 Data 102 Discussion 09: Backpropagation, Activations & Trees
9.0.1 Contact Information
| Name | Wesley Zheng |
| Pronouns | He/him/his |
| wzheng0302@berkeley.edu | |
| Discussion | Wednesdays, 11–12 PM @ Wheeler 224 |
| Office Hours | Tuesdays, 9–10 AM & 11-12 PM @ Gateway B1040 |
Contact me by email at ease — I typically respond within a day or so!
9.1 Backpropagation for a Two-Layer Neural Network
A neural network loss is a composition of simple operations. A computation graph unrolls that composition into intermediate nodes \(z_1, z_2, \ldots\) so each arrow is one elementary step.
Forward pass. Evaluate the graph from inputs to the loss, caching every intermediate \(z_i\).
Backward pass (backpropagation). Apply the chain rule from the loss back to each parameter, reusing cached intermediates. If \(u\) depends on \(v\) and \(v\) depends on \(w\), \[ \frac{\partial u}{\partial w} = \frac{\partial u}{\partial v} \cdot \frac{\partial v}{\partial w}. \] Shared prefixes of different chains (for example \(\frac{\partial \mathcal{L}}{\partial z_4}\) and \(\frac{\partial z_4}{\partial z_3}\)) are computed once and reused. That reuse is why backprop scales to deep networks, while recomputing each parameter’s full product from scratch grows much faster as layers are added.
Useful local derivatives for this discussion: \[ \frac{d}{dx}\sigma(x) = \sigma(x)\bigl(1 - \sigma(x)\bigr), \qquad \frac{\partial}{\partial w}(w \cdot z) = z, \qquad \frac{\partial}{\partial z}(z^2) = 2z. \]
Consider a two-layer neural network that computes a real-valued function of the form \(f_{w_1,w_2}(x) = w_2\sigma(w_1x)\) where \(x,w_1,w_2 \in \mathbb{R}\), and \(\sigma\) is the sigmoid function given by \(\sigma(x) = 1 / (1 + \exp(-x))\) (the subscript notation in \(f_{w_1,w_2}\) is used to emphasize that \(w_1\) and \(w_2\) are the parameters of the function). Note that this model is a simplification of what we saw in lecture, since we do not have a bias term.
The neural network \(f_{w_1, w_2}\) can be trained to predict a real-valued output given an \(m\)-dimensional input (a regression problem). Given a data set of \(n\) input-output pairs, \(\{(x_i, y_i)\}_{i = 1}^n\), a common way of training a neural network to perform this task is to find the parameter values \(w_1\) and \(w_2\) that minimize the squared error loss over the data set: \[ \operatorname*{argmin}_{w_1, w_2} \sum_{i = 1}^n (y_i - f_{w_1, w_2}(x_i))^2 = \operatorname*{argmin}_{w_1, w_2} \sum_{i = 1}^n (y_i - w_2\sigma(w_1 x_i))^2 \] To perform this minimization, gradient descent is conducted on the loss with respect to the parameters \(w_1, w_2\).
For simplicity, here we’ll just focus on the partial derivatives of the squared error loss evaluated on a single data point, \((x, y)\): \[ \begin{align} \mathcal{L}(w_1, w_2) = (y - f_{w_1, w_2}(x))^2 = (y - w_2\sigma(w_1 x))^2 \end{align} \] Backpropagation leverages the chain rule, along with dynamic programming, to compute the required partial derivatives \(\frac{\partial \mathcal{L}(w_1, w_2)}{\partial w_1}\) and \(\frac{\partial \mathcal{L}(w_1, w_2)}{\partial w_2}\) in an efficient way. This requires first computing intermediate quantities in the computation graph in what’s called a “forward pass”. That is, backpropagation first computes \(\mathcal{L}(w_1, w_2)\) by computing the quantities \(z_1 = w_1x\), \(z_2 = \sigma(z_1)\), \(z_3 = w_2z_2\), the error \(z_4 = y - z_3\), then finally the loss \(\mathcal{L}(w_1, w_2) = z_4^2\). Backpropagation then performs a “backward pass” to compute the partial derivatives, starting with \(\frac{\partial \mathcal{L}(w_1, w_2)}{\partial w_2}\).
9.1.1 (a)
Drawing a Computation Graph
For the loss function defined in Equation (1), draw the corresponding computation graph. Label intermediate quantities \(z_1\), \(z_2\), \(z_3\), and \(z_4\) in the graph.
Answer
The computation graph for \(\mathcal{L}(w_1, w_2)\) is as follows:

The trick to coming up with computation graphs is to “unroll” the expression into a series of operations on different quantities, starting from the innermost nested expression. Here that means reading \(w_2\sigma(w_1 x)\) from the inside out: multiply \(w_1\) by \(x\) to get \(z_1\), apply \(\sigma\) to get \(z_2\), multiply by \(w_2\) to get \(z_3\), subtract from \(y\) to get the residual \(z_4\), then square.
9.1.2 (b)
Updating \(w_2\)
Using the chain rule, write down an expression for \(\frac{\partial \mathcal{L}(w_1, w_2)}{\partial w_2}\). Use intermediate quantities from the forward pass (the \(z\) variables) listed above wherever possible, since these have already been computed after the forward pass.
Answer
\[ \begin{align*} \frac{\partial \mathcal{L}(w_1, w_2)}{\partial w_2} &= \frac{\partial \mathcal{L}(w_1, w_2)}{\partial z_4} \cdot \frac{\partial z_4}{\partial z_3} \cdot \frac{\partial z_3}{\partial w_2} \\ &= 2z_4 \cdot -1 \cdot z_2. \end{align*} \]
9.1.3 (c)
Updating \(w_1\)
Using the chain rule, write down an expression for \(\frac{\partial \mathcal{L}(w_1, w_2)}{\partial w_1}\). Once again, use the intermediate quantities from the forward pass wherever possible.
Hint: Note that the derivative of the sigmoid function is \(\frac{d}{dx}\sigma(x) = \sigma(x) (1 - \sigma(x))\).
Answer
\[ \begin{align*} \frac{\partial \mathcal{L}(w_1, w_2)}{\partial w_1} &= \frac{\partial \mathcal{L}(w_1, w_2)}{\partial z_4} \cdot \frac{\partial z_4}{\partial z_3} \cdot \frac{\partial z_3}{\partial z_2} \cdot \frac{\partial z_2}{\partial z_1} \cdot \frac{\partial z_1}{\partial w_1} \\ &= 2z_4 \cdot -1 \cdot w_2 \cdot z_2 (1 - z_2) \cdot x \end{align*} \]
If you expand without using the \(z_i\)’s first, the same calculation looks like \[ \begin{align*} \frac{\partial \mathcal{L}(w_1, w_2)}{\partial w_1} &= 2(y - w_2\sigma(w_1 x)) \cdot -\frac{\partial w_2\sigma(w_1 x)}{\partial w_1} \\ &= 2(y - w_2\sigma(w_1 x)) \cdot - w_2 \sigma(w_1 x) (1 - \sigma(w_1 x)) \cdot x. \end{align*} \] Then identify \(y - w_2\sigma(w_1 x) = z_4\) and \(\sigma(w_1 x) = z_2\) to recover the answer above. The five factors in the chain are, in order: derivative of the square, derivative of the residual \(y - z_3\), derivative of the product \(w_2 z_2\), derivative of the sigmoid, and derivative of the product \(w_1 x\).
9.1.4 (d)
Why use Backpropagation?
Go back to the computation graph you drew in Part (a). If you were to compute the derivatives for weights \(w_1\) and \(w_2\) one at a time, how many total derivatives would you need to compute to perform one round of weight updates? In comparison, how many derivatives does the backpropagation algorithm actually compute? How would these numbers change as the neural network becomes more complex (e.g. adding more layers)?
Answer
A naive algorithm which computes derivatives one at a time would find \(8\) total derivatives. Backpropagation recognizes and eliminates repetitive steps \(\frac{\partial \mathcal{L}(w_1, w_2)}{\partial z_4}\) and \(\frac{\partial z_4}{\partial z_3}\) so it uses 6 derivatives.
As the neural network becomes more complex, the number of derivatives the naive algorithm computes would grow exponentially large compared to the backpropagation algorithm.
Write out the two chains from parts (b) and (c): \[ \begin{align*} \frac{\partial \mathcal{L}(w_1, w_2)}{\partial w_2} &= \frac{\partial \mathcal{L}(w_1, w_2)}{\partial z_4} \cdot \frac{\partial z_4}{\partial z_3} \cdot \frac{\partial z_3}{\partial w_2} \\ \frac{\partial \mathcal{L}(w_1, w_2)}{\partial w_1} &= \frac{\partial \mathcal{L}(w_1, w_2)}{\partial z_4} \cdot \frac{\partial z_4}{\partial z_3} \cdot \frac{\partial z_3}{\partial z_2} \cdot \frac{\partial z_2}{\partial z_1} \cdot \frac{\partial z_1}{\partial w_1}. \end{align*} \] Counting every local factor separately gives \(3 + 5 = 8\). The first two factors appear in both products, so a naive pass that recomputes each chain from scratch does those two twice. Backpropagation computes each local derivative once and reuses the shared prefix, so the total is \(8 - 2 = 6\).
With more layers, each new parameter’s chain shares a longer prefix with the others. A naive algorithm pays for those shared prefixes again and again (roughly growing like the sum of chain lengths), while backprop still pays once per edge of the graph.
9.2 Activation Functions for Neural Networks
An activation function is the nonlinearity applied after a linear transform in a neural network layer. Without one, stacking layers would still just produce another linear map.
Two common choices:
- Sigmoid: \(\sigma(x) = \frac{1}{1 + \exp(-x)}\), with \(\sigma'(x) = \sigma(x)(1-\sigma(x)) \le \tfrac14\). Far from \(0\), \(\sigma'(x)\) is nearly \(0\).
- ReLU: \(\operatorname{ReLU}(x) = \max(0, x)\), with derivative \(0\) for \(x < 0\) and \(1\) for \(x > 0\) (undefined / discontinuous at \(0\)).
Vanishing gradients. Backprop multiplies many local derivatives along a path. If any factor is near \(0\), the whole product collapses and the corresponding weights barely move — the network stops learning along that path. Sigmoid is especially prone to this when pre-activations are large in magnitude. ReLU avoids decay on the positive side because its derivative stays at \(1\) there.
We saw in lecture that neural networks can learn complex, non-linear patterns from data using activation functions. One simple activation function which you’ve seen in this class before is the sigmoid function used in Logistic Regression, defined as: \[ \sigma(x) = \frac{1}{1 + \exp(-x)} \] However, most modern-day implementations of neural networks avoid using sigmoid functions for the nonlinearity, and instead favor functions like the Rectified Linear Unit, commonly abbreviated as ReLU. This function is defined as follows: \[ \text{ReLU}(x) = \max(0, x) \] We can visualize what these activation functions look like in the plot below:

9.2.1 (a)
Sketching the Derivatives
Referencing the plot above, sketch the derivatives of the sigmoid and ReLU activation functions, overlaid on the same plot.
Answer
The plot is given below:

Note that the derivative of ReLU is discontinuous at \(0\) since the original ReLU function has a corner at that point.
The ReLU derivative is \(0\) for negative inputs and \(1\) for positive inputs. For sigmoid, the derivative approaches \(0\) at both extremes and peaks at \(x = 0\), where \(\sigma'(0) = \tfrac14 = 0.25\).
9.2.2 (b)
Behavior of Activation Functions
What happens to the derivative for each of the activation functions as the input gets large?
Answer
For values of its input far away from \(0\) (positive or negative), the derivative of the sigmoid function approaches \(0\). In comparison, the derivative of the ReLU function stays constant at either \(0\) or \(1\), depending if the input is positive or negative. The derivative of ReLU does not “decay” to \(0\) like the derivative of sigmoid.
9.2.3 (c)
Vanishing Gradients
When optimizing neural networks, why is it bad for the gradient values to be \(0\)? What can you do to avoid this problem?
Answer
Since backpropagation involves multiplying many derivatives together, as soon as we have one zero value in our computation graph, the whole gradient will vanish to \(0\). This is problematic because when updating the network’s weights, a gradient value of \(0\) means that the weight will not be changed; in other words, the network will not learn!
To avoid this problem, selecting an activation function which avoids vanishing gradients like ReLU can work.
Other methods that help with vanishing gradients (not in scope for this class) include more robust network architectures, batch normalization, and careful weight initialization.
9.3 Trees, Forests, Bias, and Variance
Frequentist risk under squared error decomposes as \[ R(\theta) = \underbrace{\mathbb{E}\bigl[(\delta(x) - \mathbb{E}[\delta(x)])^2\bigr]}_{\text{Variance}} + \underbrace{\bigl(\mathbb{E}[\delta(x)] - \theta\bigr)^2}_{\text{Bias}^2}. \] The same split applies when \(\delta(x) = \hat{y}(x)\) predicts a response \(y\).
Rough intuition for this discussion:
- An unrestricted decision tree can fit the training data very closely (low bias) but changes a lot if the sample changes (high variance).
- A random forest averages many trees grown on bootstrap samples with random feature subsets. Averaging reduces variance; the price is a modest increase in bias. Lower total risk usually means better generalization.
Recall that we can express the Frequentist Risk of a decision procedure \(\delta(x)\) with respect to parameter \(\theta\) as: \[ R(\theta) = \mathbb{E}\left[ (\delta(x) - \theta)^2 \right] = \underbrace{\mathbb{E}\left[ \left( \delta(x) - \mathbb{E}[\delta(x)] \right)^2 \right]}_{\text{Variance of }\delta(x)} + \underbrace{\left( \mathbb{E}[\delta(x)] - \theta \right)^2}_{\text{Bias}^2 \text{ of }\delta(x)} \]
If our decision is a prediction for \(y\) that we call \(\hat{y}\) and \(\delta(x) = \hat{y}(x)\), then we can re-write the above expression as: \[ E[(\hat{y}(x) - y)^2] = \underbrace{E\left[\left(\hat{y}(x) - E[\hat{y}(x)]\right)^2\right]}_{\text{Variance of prediction } \hat{y}(x)} + \underbrace{(E[\hat{y}(x)] - y)^2}_{\text{Bias}^2 \text{ of }\hat{y}(x)} \]
In this question, we will consider the bias-variance decomposition for two non-parametric models: Decision Trees and Random Forests.
9.3.1 (a)
Bias-Variance Decomposition for Decision Trees
Consider a Decision Tree trained without a limit on depth. Describe this model’s bias and variance.
Answer
A Decision Tree with no depth limit will have low bias and high variance. They have low bias because decision trees can capture complex structures in the data, leading the “average decision tree” to be pretty close to the true parameter. They have high variance because as the data shifts, the fitted tree can be quite different.
9.3.2 (b)
Bias-Variance Decomposition for Random Forests
Compare a Random Forest’s bias and variance to those of a Decision Tree. Which model would you expect to generalize better to unseen data and why?
Answer
Compared to Decision Trees with no depth limits, Random Forests tend to have higher bias, but lower variance. Random Forests are typically preferred over singular Decision Trees because they tend to achieve a lower Frequentist Risk than Decision Trees by reducing variance significantly, while only slightly increasing bias. As a result, we’d expect the Random Forest to generalize better to unseen data.
If the low-bias claim for an unrestricted tree feels abstract, think of the extreme case where the tree can put a split on every feature until each leaf is pure. That fitted tree matches the training labels closely, so its average behavior can sit near the true regression function — but small changes in the sample can rearrange those splits a lot, which is the high variance.