Introduction
The origins of artificial neural networks go back to the early days of machine learning and, as the name suggests, actually stem from the attempt to implement a digital replica of biological neural networks. It is very debatable, though, to which degree this was actually achieved. The variant that turned out to be the most successful one from a statistical point of view, the multilayer perceptron, which is also the one we are going to discuss in this story, is definitely not a very accurate representation of what is going on in our brains. However, from the perspective of practical applications, that does not really matter. It is the capability as a statistical model that counts.
The statistical background
Speaking of statistics, there is the very important notion of a likelihood that comes up in the field of machine learning quite frequently, and hence I want to address it here right at the very beginning. When we talk of probabilities, we will refer to the expression \(p(\mathcal{D}|\mathbf{w})\) as a function that depends solely on \(\mathcal{D}\), where \(\mathcal{D}\) is the observed data and \(\mathbf{w}\) are the parameters of our model. By the likelihood, on the other hand, we mean the very same expression seen as a function of the parameters, \(L(\mathbf{w}) = p(\mathcal{D}|\mathbf{w})\), that is, how the probability changes when we vary the parameters. Note that the likelihood is therefore not a probability distribution over \(\mathbf{w}\). Seen in that way, we can ask the simple question of which parameters would most likely have produced the data.
This is a very important question to ask, because that is essentially how we fit our model to the data. We want to train our model, that is, optimize the parameters \(\mathbf{w}\), in such a way that exactly this likelihood gets maximized. We touched on this notion already briefly when we discussed Gauss' original derivation of the normal distribution, because he kind of used the same idea when he tried to find an equation for its density, and also in his justification of the method of least squares. Here in machine learning, it is more or less the probabilistic backbone of the training process.
There is also the possibility to see it through the lens of Bayesian statistics, where we have
\begin{equation} p(\mathbf{w}|\mathcal{D}) = \frac{p(\mathcal{D}|\mathbf{w})\,p(\mathbf{w})}{p(\mathcal{D})}. \end{equation}Here \(p(\mathbf{w})\) is the prior, which expresses our assumptions about the parameters before we have observed any data, and \(p(\mathbf{w}|\mathcal{D})\) is the posterior, which describes our uncertainty about the parameters after we have observed the data. Both perspectives essentially capture the same idea: the parameters of our model should ideally be chosen such that they explain the observed data as well as possible. If we look for the most probable parameters in the Bayesian view, however, we maximize the posterior instead of the likelihood, which additionally takes the prior into account. If the prior is uniform, i.e. we do not prefer any parameter values beforehand, both approaches lead to the same result.
Fitting a model to data
Now, before moving on to neural networks, I want to outline a little bit how such a model and its parameters can be designed and what fitting the model to the data actually looks like.
One approach to constructing a model is to select a set of basis functions and combine them by a couple of parameters to fit some given data. So let's imagine we have some data points in the plane, that is, \(N\) observations of an input variable \(x\) together with the corresponding target values \(y\)
\begin{equation} \mathbf{x} \equiv (x_1, \dots, x_N)^{\mathrm{T}}, \qquad \mathbf{y} \equiv (y_1, \dots, y_N)^{\mathrm{T}}. \end{equation}We are now going to fit a model to them, where as basis functions we choose the very simple set \([x^0, x^1, \dots, x^M]\) up to a fixed \(M\), which we will refer to as the degree of our model. The parameters define a linear combination of those functions
\begin{equation} \hat{y}(x, \mathbf{w}) = w_0 + w_1 x + w_2 x^2 + \dots + w_M x^M = \sum_{j=0}^{M} w_j x^j. \end{equation}The following figure shows how these weighted basis functions add up to fit the data for \(M = 3\).
Considering now that we want to maximize the likelihood \(L(\mathbf{w})\), we want to choose the coefficients of the linear combination in such a way that the probability of actually observing the target values \(\mathbf{y}\) is maximized, given that our model was the true underlying reality.
For that, we obviously have to make an assumption about how the target values are distributed around our model. The usual assumption is that they follow a normal distribution with mean \(\hat{y}(x, \mathbf{w})\) and precision (inverse variance) \(\beta\), so that the likelihood becomes
\begin{equation} p(\mathbf{y}|\mathbf{x}, \mathbf{w}, \beta) = \prod_{n=1}^{N} \mathcal{N}\left(y_n \,|\, \hat{y}(x_n, \mathbf{w}), \beta^{-1}\right). \end{equation}As Gauss already showed, maximizing this likelihood leads to minimizing the sum of squared deviations, see the following figure.
More precisely, if we take the negative logarithm and drop all terms that do not depend on \(\mathbf{w}\), we obtain the so-called sum-of-squares error function
\begin{equation} E(\mathbf{w}) = \frac{1}{2} \sum_{n=1}^{N} \left\{ \hat{y}(x_n, \mathbf{w}) - y_n \right\}^2. \end{equation}We can take one more thing directly from Gauss. Since we have defined a linear combination for combining our set of basis functions, the function of our model depends only linearly on the parameters. And since we are also seeking the minimization of the squared deviations, we can directly use the method of least squares.
This leads to the following result for an optimal fit of our model to the data
\begin{equation} \mathbf{w}_{\mathrm{ML}} = \left( \boldsymbol{\Phi}^{\mathrm{T}} \boldsymbol{\Phi} \right)^{-1} \boldsymbol{\Phi}^{\mathrm{T}} \mathbf{y}, \end{equation}where \(\boldsymbol{\Phi}\) is the so-called design matrix with the elements \(\Phi_{nj} = x_n^j\), i.e. its \(n\)-th row contains all basis functions evaluated at the data point \(x_n\).
What we just did here is usually referred to as polynomial fitting. In general, this idea of selecting a set of basis functions is also used in many other models. When the model depends only linearly on the parameters and a normal distribution is assumed for the target values, training the model is equivalent to the method of least squares. When it does not depend linearly on the parameters, one may make use of the non-linear extension of least squares, which, however, can only be solved iteratively.
One has to be careful with the choice of the degree \(M\), though: if it is too large, the polynomial starts to fit the noise in the data rather than the underlying trend, which is known as overfitting. This is where the prior from above comes in handy: a Gaussian prior on the weights adds the term \(\frac{\lambda}{2}\|\mathbf{w}\|^2\) to the error function, which keeps the weights small and is known as L2 regularization or weight decay.
The Multilayer Perceptron
Neural networks, however, work a little differently. For problems with many input variables, a set of fixed basis functions is just insufficient, because the number of basis functions we need grows very rapidly with the dimensionality of the input. The alternative is to let the basis functions be adaptive, in other words, to use parametric forms of them, whose parameters are adjusted during training as well.
That comes at the cost of the likelihood function — or more precisely, the error function \(E(\mathbf{w})\), i.e. the negative log-likelihood, is no longer guaranteed to be convex. In other words, there may exist a lot of other (local) minima besides the global one, and hence it is not so easy anymore to find the optimal parameters.
Compare that to the polynomial fitting. There, the model depends only linearly on the parameters. Together with the sum-of-squares error, this makes \(E(\mathbf{w})\) a quadratic function of \(\mathbf{w}\), and we get a neat convex surface as the error function, as illustrated in the following figure.
This in turn means there is one clear global minimum. Additionally, and very conveniently, in this case it is directly given by the method of least squares.
In the case of parametrized basis functions, they will usually not depend linearly on their parameters, otherwise the adaptiveness of the functions would be very limited. So, for a multilayer perceptron we will get an error function that looks something like this.
That is way harder to optimize. However, the trade-off of having to invest more time into training, but then having a very accurate and compact model, is usually worth it.
Before continuing with the issue of optimization, let's take a closer look at how the multilayer perceptron actually parametrizes the basis functions. We will see that the name multilayer perceptron is actually a little misleading, because there are not layers of perceptrons but layers of logistic regression models, at least as long as the hidden units use the logistic sigmoid. The perceptron uses a discontinuous step function, whereas the units of a multilayer perceptron use continuous, differentiable non-linearities.
For those who do not know, the linear models for regression and classification are based on linear combinations of fixed non-linear basis functions \(\phi_j(\mathbf{x})\), as follows
\begin{equation} \hat{y}(\mathbf{x}, \mathbf{w}) = f\left( \sum_{j=0}^{M} w_j \phi_j(\mathbf{x}) \right), \end{equation}where the input \(\mathbf{x}\) can now be a vector of \(D\) input variables, \(\phi_0(\mathbf{x}) = 1\) accounts for the bias, and \(f(\cdot)\) is a non-linear activation function in the case of classification and the identity in the case of regression. Our polynomial from above is a special case of this, with the powers \(x^j\) as basis functions and \(f\) being the identity.
The basic idea will be to make the basis functions \(\phi_j(\mathbf{x})\) depend on parameters and to adjust these parameters, together with the coefficients \(w_j\), during training. This will lead to the architecture of the multilayer perceptron.
First, we construct \(M\) linear combinations of the input variables \(x_1, \dots, x_D\) as follows
\begin{equation} a_j = \sum_{i=1}^{D} w_{ji}^{(1)} x_i + w_{j0}^{(1)}, \qquad j = 1, \dots, M, \end{equation}where the superscript \((1)\) indicates that the corresponding parameters belong to the first layer of the network. We keep the notation \(\mathbf{w}\) for the parameters, which is also the usual notation for neural networks, and we will further refer to the parameters \(w_{ji}^{(1)}\) as weights and to \(w_{j0}^{(1)}\) as biases.
These quantities \(a_j\) are known as activations. Each of them is then transformed by a non-linear activation function \(h(\cdot)\), which, as we will see later, is required to be differentiable
\begin{equation} z_j = h(a_j). \end{equation}In the early days of machine learning, choices for \(h\) were usually sigmoidal functions such as the logistic sigmoid or the \(\tanh\) function. Nowadays, the ReLU, \(h(a) = \max(0, a)\), is the most common choice. We will see why this change happened when we discuss the training process in detail.
The outputs \(z_j\) correspond to the outputs of the basis functions \(\phi_j(\mathbf{x})\) in the equation of the linear models for regression and classification, and in the context of neural networks they are referred to as hidden units. Following the equation above, they are then again linearly combined to
\begin{equation} a_k = \sum_{j=1}^{M} w_{kj}^{(2)} z_j + w_{k0}^{(2)}, \qquad k = 1, \dots, K, \end{equation}which then correspond to the activations of the second layer in the neural network, where \(K\) is the total number of outputs.
Finally, these output activations are transformed by an appropriate activation function to give the network outputs \(\hat{y}_k\).
Putting all of this together, we get the overall network function, which for sigmoidal output units takes the form
\begin{equation} \hat{y}_k(\mathbf{x}, \mathbf{w}) = \sigma\left( \sum_{j=1}^{M} w_{kj}^{(2)}\, h\left( \sum_{i=1}^{D} w_{ji}^{(1)} x_i + w_{j0}^{(1)} \right) + w_{k0}^{(2)} \right), \end{equation}where all weights and biases are grouped together into the vector \(\mathbf{w}\). So a neural network is nothing else than a non-linear function from a set of input variables \(\{x_i\}\) to a set of output variables \(\{\hat{y}_k\}\), controlled by a vector \(\mathbf{w}\) of adjustable parameters. Note that the output activation has to match the error function: for regression we use the identity, which together with the normal distribution from above leads to the sum-of-squares error, whereas a sigmoid output corresponds to a Bernoulli likelihood for binary classification and leads to the cross-entropy error. The following figure shows how, for a small network with three hidden units and a linear output, the weighted hidden units add up just like the basis functions of the polynomial, only that their shape is now learned as well.
The biases can be absorbed into the weights by introducing an additional input variable \(x_0 = 1\) and an additional hidden unit \(z_0 = 1\). This simplifies the network function to
\begin{equation} \hat{y}_k(\mathbf{x}, \mathbf{w}) = \sigma\left( \sum_{j=0}^{M} w_{kj}^{(2)}\, h\left( \sum_{i=0}^{D} w_{ji}^{(1)} x_i \right) \right). \end{equation}Let's visualize the architecture of a neural network, because from the equations alone it is a little hard to see what is going on.
The input variables, the hidden units and the outputs are drawn as nodes, the so-called neurons, which are arranged in layers, and the weights correspond to the links between them. The biases are represented by the additional nodes \(x_0\) and \(z_0\). Since the information flows only in one direction, from the inputs through the hidden units to the outputs, such a network is also called a feed-forward network, and evaluating the network function is referred to as forward propagation.
The training process
As already mentioned, the training of a multilayer perceptron is not as straightforward as for models that depend only linearly on their parameters.
In general, there is no analytical solution anymore. Instead, we use an iterative method that is usually referred to as gradient descent.
The basic idea of it is to compute the gradient \(\nabla E(\mathbf{w})\), that is, the derivative of the error function with respect to each single parameter. Since the gradient points in the direction of the steepest ascent, we then take one tiny step in the opposite direction, that is, we update all parameters by a small amount derived from that gradient
\begin{equation} \mathbf{w}^{(\tau+1)} = \mathbf{w}^{(\tau)} - \eta \nabla E\left(\mathbf{w}^{(\tau)}\right), \end{equation}where \(\tau\) labels the iteration step and the parameter \(\eta > 0\) is known as the learning rate, which determines how big this step is. Then we repeat the process: we determine the gradient again at this new position and again apply a tiny amount of it to the parameters.
To compute the gradient of a neural network efficiently, we make use of the chain rule. Starting at the outputs, the error is propagated backwards through the network, layer by layer, to obtain the derivatives with respect to all weights. If we write \(\delta_j \equiv \partial E_n / \partial a_j\) for the error of unit \(j\) for a single data point \(n\), we get \(\partial E_n / \partial w_{ji} = \delta_j z_i\) (with \(z_i = x_i\) in the first layer), and the errors of the hidden units follow from those of the next layer by
\begin{equation} \delta_j = h'(a_j) \sum_{k} w_{kj} \delta_k. \end{equation}This procedure is known as error backpropagation, and it is exactly the reason why the activation functions \(h\) have to be differentiable (strictly speaking, the ReLU is not differentiable at \(0\), but there one simply uses a subgradient, e.g. \(0\)). It also explains the change from sigmoidal functions to the ReLU: on the way back, the derivatives of the activation functions of all layers get multiplied with each other, as one can see from the factor \(h'(a_j)\) above. The derivative of the logistic sigmoid is at most \(1/4\), and both the sigmoid and the \(\tanh\) have a derivative close to zero for large \(|a|\). So, for networks with many layers, the gradient gets smaller and smaller towards the first layers, and these layers hardly learn anything. This is known as the vanishing gradient problem. The derivative of the ReLU, on the other hand, is simply 1 for all positive activations.
Visually, that means we have the parameter space and an error function over it (remember that minimizing it corresponds directly to maximizing the likelihood). And we have our training data, from which we compute the gradient at the current position of our parameters. At the beginning, the parameters are usually just initialized randomly.
The negative gradient then gives us the direction of steepest descent, and we are going to walk along it a tiny amount. Then we are going to repeat that process until we eventually reach a minimum.
For a convex error function, this will always work, provided the learning rate is chosen suitably. If it is too large, we may overshoot the minimum, and if it is too small, it takes forever. However, as already mentioned earlier, the architecture of a neural network is such that the error function is no longer convex. That means, depending on where we started, we may end up in a local minimum.
Therefore, there are many extensions of this method of gradient descent. One of the most important ones is stochastic gradient descent, where the gradient is not computed on the whole training data at once but only on a small random subset, a so-called mini-batch. This not only makes each step much cheaper, the resulting noise also helps to escape from local minima. Other extensions, such as momentum or the Adam optimizer, basically all try to reach, ideally, the global minimum, or at least a minimum that is the lowest one within a large area of the parameter space, and to get there faster. Fortunately, it turns out that for large networks many local minima are almost as good as the global one, so in practice this is less of a problem than one might think.
However, for this story, we will not go into the details of that. And for now, I think you got a pretty good overview of what the basic idea behind a neural network is and what mathematics is going on behind the scenes.
Summary
Training a model means choosing its parameters such that the likelihood of the observed data gets maximized. For normally distributed target values, this is equivalent to minimizing the sum-of-squares error. With fixed basis functions, as in polynomial fitting, the error function is convex and the optimal parameters are directly given by the method of least squares.
The multilayer perceptron instead makes the basis functions adaptive: linear combinations of the inputs, transformed by a non-linear activation function into hidden units, which are again linearly combined to the outputs. The price is a non-convex error function, so the network is trained iteratively by gradient descent, with the gradient computed by backpropagation.