\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amssymb}
\usepackage{algorithm,algpseudocode}
\usepackage{bm}
\usepackage{hyperref}
\usepackage{float}
\usepackage{subcaption}
\usepackage{enumitem}
\usepackage{xcolor}
\definecolor{darkgreen}{rgb}{0.0, 0.5, 0.0}
\definecolor{aaltoBlue}{RGB}{0, 51, 102}
\definecolor{pinegreen}{cmyk}{0.92,0,0.59,0.25}
\definecolor{royalblue}{cmyk}{1,0.50,0,0}
\definecolor{lavander}{cmyk}{0,0.48,0,0}
\definecolor{violet}{cmyk}{0.79,0.88,0,0}
\definecolor{lightblue}{RGB}{173, 216, 230}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{calc,arrows.meta,positioning,fit,shapes.geometric,shapes.misc,intersections,patterns,decorations.pathreplacing,backgrounds,matrix}
\usepgfplotslibrary{fillbetween,groupplots}
\setcounter{secnumdepth}{-\maxdimen}
% Long unbreakable tokens (the ISO/IEC author names in citations, term names
% in the title) otherwise overflow the margin by a few points; a modest
% emergency stretch lets TeX loosen the paragraph instead.
\emergencystretch=2em
\usepackage{mleftright}
% Project macros used by this entry (auto-extracted).
\newcommand{\actfun}{\sigma}
\newcommand{\feature}{x}
\newcommand{\featurevec}{\vx}
\newcommand{\hypothesis}{h}
\newcommand{\iteridx}{t}
\newcommand{\lossfunc}[2]{L\left(#1,#2 \right)}
\newcommand{\lrate}{\eta}
\newcommand{\mW}[0]{{\bf W}}
\newcommand{\pair}[2]{\left( #1,#2 \right)}
\newcommand{\predictedlabel}{\widehat{\truelabel}}
\newcommand{\sampleidx}{r}
\newcommand{\samplesize}{m}
\newcommand{\stack}[1]{\operatorname{stack}\big( #1 \big)}
\newcommand{\truelabel}{y}
\newcommand{\vw}[0]{{\bf w}}
\newcommand{\vx}[0]{{\bf x}}
\newcommand{\weight}{w}
\newcommand{\weights}{\vw}

\begin{document}

\section{backpropagation}

\subsection*{Author and editors}
Alexander Jung\textsuperscript{*}, Department of Computer Science, Aalto University, Espoo, Finland (ORCID \mbox{\href{https://orcid.org/0000-0001-7538-0990}{0000-0001-7538-0990}}) \\
Editors: Konstantina Olioumtsevits (ORCID \mbox{\href{https://orcid.org/0000-0003-2697-8659}{0000-0003-2697-8659}}), Ekkehard Schnoor (ORCID \mbox{\href{https://orcid.org/0000-0001-8252-1499}{0000-0001-8252-1499}}), Aalto University, Espoo, Finland \\
\textsuperscript{*}Corresponding author: \href{mailto:alex.jung@aalto.fi}{alex.jung@aalto.fi}



\subsection{Abstract}
Backpropagation is an algorithm that computes the
		gradient of the loss with respect to the
		model parameters of an artificial neural network (ANN). The algorithm consists of
		two consecutive phases, referred to as forward and backward pass.
		The forward pass evaluates, for the current model parameters, the
		activations and the resulting prediction errors on a
		given dataset. The backward pass then applies the chain rule
		to compute the partial derivatives of the average loss
		with respect to the model parameters. These
		partial derivatives constitute a gradient, which can be
		used in a gradient step to update the model parameters.

\subsection{Definition}
Consider an artificial neural network (ANN) with a
		billion model parameters and its prediction for a new
		data point. If this prediction is wrong, the
		model parameters can be updated by a gradient step. That update
		requires the partial derivatives of the loss on the new
		data point with respect to all model parameters.
\par
		Backpropagation is an algorithm for computing these
		partial derivatives with as little duplicated computation as
		possible (Goodfellow et al., 2016, Sect.~6.5.3;
		Rumelhart et al., 1986). More generally, it computes the
		gradient $\nabla f(\weights)$ of an objective function
		$f(\weights)$ with respect to the model parameters $\weights$ of
		the ANN. The model parameters consist of the
		edge weights and the offset terms at the nodes of the
		ANN. The objective function is typically
		the average loss
		\[
			f(\weights) = \frac{1}{\samplesize}
			\sum_{\sampleidx=1}^{\samplesize}
			\lossfunc{\pair{\featurevec^{(\sampleidx)}}
				{\truelabel^{(\sampleidx)}}}{\hypothesis^{(\weights)}}
			\text{,}
		\]
		incurred on a dataset of $\samplesize$ data points
		$\pair{\featurevec^{(1)}}{\truelabel^{(1)}}, \ldots,
		\pair{\featurevec^{(\samplesize)}}{\truelabel^{(\samplesize)}}$.
		Backpropagation computes the partial derivatives of
		$f(\weights)$ one layer at a time
		(Fig.~\ref{fig:backprop_schematic_dict}). Its execution consists of
		two consecutive phases, the forward pass and the backward pass.
\par
		In the forward pass, the ANN is applied to each
		data point of the dataset: the neurons of each layer evaluate
		their activations from the activations of the previous
		layer. The final layer then produces the output,
		which is the prediction for the label of a
		data point with features $\featurevec$. The quality of
		this prediction is measured by a loss function
		$\lossfunc{\pair{\featurevec}{\truelabel}}{\hypothesis^{(\weights)}}$,
		where $\hypothesis^{(\weights)}$ is the hypothesis map
		represented by the ANN with model parameters $\weights$. In
		the backward pass, the partial derivatives of $f(\weights)$
		with respect to the edge weights of each layer are
		computed recursively, from the output back to the input.
		These partial derivatives form the whole gradient
		$\nabla f(\weights)$. The
		backward pass requires about as much computation as the forward
		pass: its cost grows in proportion to the number of edges of the
		ANN, one partial derivative, one multiplication, and
		one addition per edge (Goodfellow et al., 2016, Sect.~6.5.3).
		\begin{figure}[H]
		\centering
		\begin{tikzpicture}[>=stealth,
			dot/.style={circle, fill=black, inner sep=1.5pt},
			lbl/.style={font=\small}]
			\foreach \i/\y in {1/1.8, 2/0, 3/-1.8}
				\node[dot, label=left:{$\feature_{\i}$}] (x\i) at (0,\y) {};
			\foreach \i/\y in {1/1.8, 2/0, 3/-1.8}
				\node[dot, label=above:{$a_{\i}$}] (h\i) at (3.2,\y) {};
			\node[dot, label={[font=\small]above right:{$\predictedlabel$}}]
				(yhat) at (6.4,0) {};
			\node[dot, label=right:{$f(\weights)$}] (loss) at (9.0,0) {};
			\foreach \i in {1,2,3}{
				\foreach \j in {1,2,3}{
					\draw[->] (x\i) -- (h\j);
				}
			}
			\foreach \j in {1,2,3}{
				\draw[->] (h\j) -- (yhat);
			}
			\draw[->] (yhat) -- (loss);
			\node[lbl] at (1.6,2.35) {$\mW^{(1)}$};
			\node[lbl] at (4.8,1.45) {$\mW^{(2)}$};
			\draw[<-, dashed] (-0.7,-2.9) -- node[below, lbl]
				{$\partial f / \partial \mW^{(1)}$,
				 $\partial f / \partial \mW^{(2)}$} (10.1,-2.9);
			\node[lbl, anchor=west] at (-0.7,-2.4) {backward pass};
			\node[lbl, anchor=west] at (-0.7,2.75) {forward pass};
			\node[lbl, anchor=east] at (10.1,2.75)
				{$\weights = \stack{\mW^{(1)},\mW^{(2)}}$};
		\end{tikzpicture}
		\caption{Backpropagation through an ANN with one hidden
			layer. The solid arrows indicate the forward pass: the
			features $\feature_{1},\feature_{2},\feature_{3}$ reach
			the neurons $a_{1},a_{2},a_{3}$ through the
			edge weights $\mW^{(1)}$, then the output
			$\predictedlabel = \hypothesis^{(\weights)}(\featurevec)$
			through $\mW^{(2)}$. The output is a
			prediction for the label of a data point and
			incurs the loss $f(\weights)$. The dashed arrow indicates
			the backward pass, which runs the other way and yields the
			partial derivatives of $f(\weights)$ with respect to the
			edge weights of both layers. The model parameters
			$\weights$ collect the edge weights of both layers;
			the offset terms of the neurons are omitted here
			\label{fig:backprop_schematic_dict}}
		\end{figure}
		To illustrate the details of backpropagation, consider a
		simple ANN: it consists of three layers with one
		neuron each. Its model parameters are the three
		edge weights $\weight^{(1)}, \weight^{(2)}, \weight^{(3)}$.
		The gradient of an average is the average of the
		gradients of its summands, so a dataset with a single
		data point already shows the recursion. The objective function
		$f(\weights)$ is then the loss of that data point. Write
		$a^{(0)} = \feature$ for its feature. Layer
		$\ell = 1, 2, 3$ computes
		\[
			z^{(\ell)} = \weight^{(\ell)} a^{(\ell-1)} \text{,} \qquad
			a^{(\ell)} = \actfun\big(z^{(\ell)}\big) \text{,}
		\]
		with the activation function $\actfun$ and no offset terms. The output is
		$\hypothesis^{(\weights)}(\feature) = a^{(3)}$. The
		edge weight $\weight^{(3)}$ enters $f(\weights)$ only through
		$z^{(3)}$, $\weight^{(2)}$ through $z^{(2)}$ and then $z^{(3)}$, and
		$\weight^{(1)}$ through all three.
\par
		The chain rule therefore gives
		\[
		\begin{aligned}
			\frac{\partial f}{\partial \weight^{(3)}}
			&= \frac{\partial f}{\partial a^{(3)}}\,
			   \actfun'\big(z^{(3)}\big)\, a^{(2)} \text{,} \\
			\frac{\partial f}{\partial \weight^{(2)}}
			&= \frac{\partial f}{\partial a^{(3)}}\,
			   \actfun'\big(z^{(3)}\big)\, \weight^{(3)}\,
			   \actfun'\big(z^{(2)}\big)\, a^{(1)} \text{,} \\
			\frac{\partial f}{\partial \weight^{(1)}}
			&= \frac{\partial f}{\partial a^{(3)}}\,
			   \actfun'\big(z^{(3)}\big)\, \weight^{(3)}\,
			   \actfun'\big(z^{(2)}\big)\, \weight^{(2)}\,
			   \actfun'\big(z^{(1)}\big)\, \feature \text{.}
		\end{aligned}
		\]
		Each line repeats the one above it and appends two factors.
		Evaluating the three lines separately would recompute the repeated
		factors twice over. Backpropagation computes them once, from the
		output backwards. The repeated factors form the
		partial derivative of $f(\weights)$ with respect to
		$z^{(\ell)}$. This partial derivative obeys its own recursion,
		\[
			\frac{\partial f}{\partial z^{(3)}}
			= \frac{\partial f}{\partial a^{(3)}}
			\actfun'\big(z^{(3)}\big) \text{,} \qquad
			\frac{\partial f}{\partial z^{(\ell)}}
			= \frac{\partial f}{\partial z^{(\ell+1)}}
			\weight^{(\ell+1)} \actfun'\big(z^{(\ell)}\big)
			\quad \text{for } \ell = 2, 1 \text{.}
		\]
		Each edge weight then costs one multiplication,
		\[
			\frac{\partial f}{\partial \weight^{(\ell)}}
			= \frac{\partial f}{\partial z^{(\ell)}} a^{(\ell-1)}
			\text{.}
		\]
		The backward pass is this recursion, and the activations
		$a^{(\ell)}$ it needs are the ones the forward pass already
		computed.
\par
		The partial derivatives delivered by backpropagation constitute
		the gradient used in a gradient step:
		\[
			\weights^{(\iteridx+1)} = \weights^{(\iteridx)}
			- \lrate \nabla f(\weights^{(\iteridx)})
			\text{, for } \iteridx = 0,1,\ldots \text{,}
		\]
		with step size $\lrate$. Each gradient step requires a new run
		of backpropagation, since the model parameters change after each
		step. These partial derivatives are not only used for a
		gradient step. A class activation map (CAM) explains a
		prediction of a convolutional neural network (CNN) by weighting its feature maps
		with partial derivatives of the output
		(Selvaraju et al., 2017).

\subsection{Cross References}
ANN, deep net, gradient, GD, gradient step, layer, edge weight, model parameter, activation function, loss function, neuron

\subsection{References}
\begin{enumerate}
  \item Goodfellow et al. (2016). Deep Learning. MIT Press, Cambridge, MA, USA.
  \item Selvaraju et al. (2017). Grad-CAM: Visual Explanations from Deep Networks via Gradient-Based Localization. In: 2017 IEEE Int. Conf. Comput. Vis., pp.~618--626.
  \item Rumelhart et al. (1986). Learning Representations by Back-Propagating Errors. Nature, 323, pp.~533--536.
\end{enumerate}

\end{document}
