Back

Deriving Back Propagation in Matrix Form

Roy Varon Weinryb

varonroy@gmail.com

15/12/2021 (dd/mm/yyyy)

Introduction

The forward propagation process of neural networks can be very concisely written using a series of Matrix-Vector multiplications. The same can be said about the back propagation process. In this article I will show step by step how derivatives propagate through Matrix-Vector multiplications and how to use these results in a neural network.

Forward Propagation

Lets consider a portion of a neural network: two layers and the weights between, and an activation function ff. Let the first layer have nn neurons and the second one mm neurons. Since these two layers are nothing more than arrays of numbers, they can be represented using vectors:

first layer:v1∈Rnsecond layer:v2∈Rm\begin{gather} \text{first layer}: & v^1 \in \mathbb{R}^n \\ \text{second layer}:&v^2 \in \mathbb{R}^m \\ \end{gather}

Let Wi,jW_{i,j} be the weight connecting the ithith neuron of the second layer and the jthjth neuron of the first layer. This way, the forward propagation for each of the individual neurons can be written as: vi2=f(∑k=1nWi,kvk1)v^2_i = f\Big(\sum_{k = 1}^{n}W_{i, k} {v^1_k}\Big) Or more compactly: v2=f(Wv1)v^2 = f(W v^1) To simplify things, lets split the above step into two: s2=Wv1s^2 = W v^1 v2=f(s2)v^2 = f(s^2)

So far we have: v1∈Rnv^1 \in \mathbb{R}^n s2∈Rms^2 \in \mathbb{R}^m v2∈Rmv^2 \in \mathbb{R}^m W∈Rm×nW \in \mathbb{R}^{m \times n} s2=Wv1s^2 = W v^1 v2=f(s2)v^2 = f(s^2)

Back Propagation

Let the derivative of the second layer with respect to the loss function be given: ∂L∂v2\frac{\partial L}{\partial v^2} So the partial derivative of the loss with respect to the ithith neuron in the second layer will be the ithith element of the above vector: ∂L∂vi2=(∂L∂v2)i\frac{\partial L}{\partial v^2_i} = \Big(\frac{\partial L}{\partial v^2}\Big)_i

Going a step back, let’s write how the ithith element of s2s^2 is propagation: vi2=f(si2)v^2_i = f(s^2_i)

Then by taking the derivative: ∂vi2∂si2=f′(si2)\frac{\partial v^2_i}{\partial s^2_i} = f'(s^2_i) We can write the following vector: ∂v2∂s2=f′(s2)\frac{\partial v^2}{\partial s^2} = f'(s^2) And apply the chain rule:

∂L∂si2=∂L∂vi2∂vi2∂si2=∂L∂vi2f′(s2)\frac{\partial L}{\partial s^2_i} = \frac{\partial L}{\partial v^2_i} \frac{\partial v^2_i}{\partial s^2_i} = \frac{\partial L}{\partial v^2_i} f'(s^2)

And in vector form: ∂L∂s2=∂L∂v2∘f′(s2)\frac{\partial L}{\partial s^2} = \frac{\partial L}{\partial v^2} \circ f'(s^2) (where ∘\circ represents element wise multiplication).

Next, the propagation towards v1v^1 will be more complicated, because unlike there is no longer a one to one relation between elements in v1v^1 and s2s^2. Each element of v1v^1 depends on each element of s2s^2 in the following way:

si2=∑∗k=1nW∗i,kvk1s^2_i = \sum*{k = 1}^{n} W*{i, k} {v^1_k}

So by taking the derivative of the ithith element of s2s^2 with respect to the jthjth element of v1v^1 we get: ∂si2∂vj1=Wi,j\frac{\partial s^2_i}{\partial v^1_j} = W_{i, j} This means that when we apply the chain rule to get ∂L/∂vi1\partial L / \partial v^1_i , we need to consider how every element of s2s_2 will impact the total derivative:

∂L∂v1∗i=∑_j=0m∂L∂s2∗j∂s2∗j∂vi1=∑∗j=0m∂L∂s2∗jW∗j,i\frac{\partial L}{\partial v^1*i} = \sum\_{j = 0}^{m}\frac{\partial L}{\partial s^2*j} \frac{\partial s^2*j}{\partial v^1_i} = \sum*{j = 0}^{m}\frac{\partial L}{\partial s^2*j} W*{j, i}

Notice how the indices in WW are flipped. We can flip the indices back by transposing the matrix

∂L∂vi1=∑j=0m∂L∂sj2(WT)i,j=∑j=0m(WT)i,j∂L∂sj2\frac{\partial L}{\partial v^1_i} = \sum_{j = 0}^{m}\frac{\partial L}{\partial s^2_j} (W^T)_{i, j} = \sum_{j=0}^{m} (W^T)_{i, j} \frac{\partial L}{\partial s^2_j}

So the ithi^{th} element of ∂L/∂vi1\partial L / \partial v^1_i equals to the dot product of the ithith row of matrix WTW^T with the vector ∂L/∂sj2\partial L / \partial s^2_j. This is exactly what matrix multiplication does, so we can write: ∂L∂v1=WT∂L∂s2 \frac{\partial L}{\partial v^1} = W^T \frac{\partial L}{\partial s^2}

Finally, we need to calculate the derivatives with respect to each of the weights in the matrix WW. Using the above forward propagation formula: si2=∑k=1nWi,kvk1s^2_i = \sum_{k = 1}^{n}W_{i, k} {v^1_k} We can find the partial derivative: ∂s2∗i∂W∗i,j=vj1 \frac{\partial s^2*i}{\partial W*{i, j}} = v^1_j Notice that the index of the element of s2s^2 has to match the index of the row of WW. Since only the ithith row has an effect on that element.

And now by applying the chain rule:

∂L∂Wi,j=∂L∂si2∂si2∂Wi,j=∂L∂si2vj1\frac{\partial L}{\partial W_{i, j}} = \frac{\partial L}{\partial s^2_i} \frac{\partial s^2_i}{\partial W_{i, j}} = \frac{\partial L}{\partial s^2_i} v^1_j

It is a bit hard to see at first, but we can rewrite this expression as a multiplication of a column vector by a row vector: ∂L∂W=∂L∂s2(v1)T\frac{\partial L}{\partial W} = \frac{\partial L}{\partial s^2} (v^1)^T

Here is a small example of how that multiplication would work:

[a1a2][b1b2b3]=[a1b1a1b2a1b3a2b1a2b2a2b3]\begin{bmatrix} a_1 a_2 \end{bmatrix} \begin{bmatrix} b_1 & b_2 & b_3 \end{bmatrix} = \begin{bmatrix} a_1b_1 & a_1b_2 & a_1b_3 \\\\ a_2b_1 & a_2b_2 & a_2b_3 \\\\ \end{bmatrix}

Summary

To Summarize, given the following structure of a neural network: v1∈Rnv^1 \in \mathbb{R}^n s2∈Rms^2 \in \mathbb{R}^m v2∈Rmv^2 \in \mathbb{R}^m W∈Rm×nW \in \mathbb{R}^{m \times n} s2=Wv1s^2 = W v^1 v2=f(s2)v^2 = f(s^2)

And the derivative of the loss function with respect to the last layer: ∂L∂v2\frac{\partial L}{\partial v^2}

We can back propagate the derivative with the following formulas: ∂L∂s2=∂L∂v2∘f′(s2)\frac{\partial L}{\partial s^2} = \frac{\partial L}{\partial v^2} \circ f'(s^2) ∂L∂v1=WT∂L∂s2 \frac{\partial L}{\partial v^1} = W^T \frac{\partial L}{\partial s^2} ∂L∂W=∂L∂s2(v1)T\frac{\partial L}{\partial W} = \frac{\partial L}{\partial s^2} (v^1)^T