Know what you predict: estimating uncertainty with neural networks — Artificial Intelligence, Data Science, Machine learning

Image for post
Image for post

In most of the practical use cases, data scientists are satisfied by machine learning models that simply make predictions. Given unseen observations

Image for post
Image for post

a model performs the prediction of certain outcome

Image for post
Image for post

. The performance of such a model is usually assessed by comparing the predicted value with the ground truth, whenever available. There is however, another measure that might be of interest: uncertainty. How uncertain is a model in predicting a particular sample?

Image for post
Image for post

As a matter of fact, there are some aspects of a dataset that are learned faster and better with respect to some others. The geometry of a training dataset can differ consistently within the dataset itself. As a consequence, looking at the mere predicted value of a model is not only reductive but can also be misleading. For instance, knowing that a model is quite uncertain about a specific input data can improve the overall decision of an agent (artificial or human) who relies on such prediction. Statisticians know the problem of uncertainty ever since.

To be more specific, Bayesian statisticians know that their framework is solid enough to natively reason about uncertainty. A Bayesian neural network, for instance, is characterized by weights that are statistical distributions rather than constant values (like in the more traditional case). In such a framework the predicted value is also a statistical distribution of which it is possible to calculate statistical properties like means, standard deviations, function shapes and densities.

Unfortunately, Bayesian neural networks (and other Bayesian models in general) are slow to train, much slower than non-Bayesian models, due to sampling that can be prohibitive for real world problems with high number of dimensions.
In this post I describe an alternative method to calculate model uncertainty by using a relatively deep neural network. A similar approach can be generalized to other types of models too.

Very often, especially in classification, predictive probabilities obtained at the end of the pipeline (the softmax output) are interpreted as model confidence. This is a common misinterpretation that can lead to inconsistencies, due to the fact that a model can be uncertain in its predictions even with a high softmax output.

The theoretical framework proposed by Yarin Gal et al. leverages model dropout as a tool to evaluate how uncertain a model is with respect to a certain observed sample.
Model dropout (specific to the neural network case) consists in randomly disconnecting some connections in the layers of a neural network. It is usually applied as a mechanism to increase generalization and mitigate over-fitting.
By randomly switching off some connections a model tends to unlearn some patterns that are only specific of the training set due to the presence of outliers or noise. Such patterns are usually not present in the testing set, leading the model to under-perform and not generalize with unseen observations.
In deep learning, practitioners set dropout rates between 20% and 50% range, meaning that up to half of the connections of a certain layer will be set to zero during inference. This mechanism forces the back-propagation method to tune parameters in the other layers such that the whole network can cope with the dropped rate of connections. The key finding consists in the fact that a neural network with dropout applied before every weight layer, is mathematically equivalent to an approximation to the probabilistic deep Gaussian process.
Such a dropout minimizes the Kullback-Leibler divergence between an approximate distribution and the posterior of a deep Gaussian process.
Below there is a theoretical explanation that leads to the aforementioned approximation. The paragraph below can be skipped if only a practical implementation is needed.

What is a Gaussian process and does it provide a measure of uncertainty?

A deep Gaussian process is a statistical tool that allows one to model distributions over functions. For instance, given the covariance function

Image for post
Image for post

with non-linear elements

Image for post
Image for post

and distributions

Image for post
Image for post

and

Image for post
Image for post

, where

Image for post
Image for post

and

Image for post
Image for post

are the weights and the bias vectors respectively, it is shown that a deep Gaussian process with L layers and co-variance function

Image for post
Image for post

can be approximated by a variational distribution over each component of the spectral decomposition of the GPs’ covariance functions.
If

Image for post
Image for post

is a random matrix for each layer

Image for post
Image for post

and we let each row of

Image for post
Image for post

distribute according to the

Image for post
Image for post

above, we can claim that

Image for post
Image for post

and

Image for post
Image for post

Since the posterior distribution

Image for post
Image for post

is usually intractable, an approximate distribution

Image for post
Image for post

is used.
Such

Image for post
Image for post

is obtained by randomly setting matrices’ columns to zero, by a Bernoulli distribution.

Hence,

Image for post
Image for post

given some probabilities

Image for post
Image for post

and matrices

Image for post
Image for post

as variational parameters.
If

Image for post
Image for post

then the unit

Image for post
Image for post

in layer

Image for post
Image for post

is dropped out. Almost there.

How to implement uncertainty-aware neural networks

The code below implements a stacked LSTM neural network with training dropout layers in Keras.
When in inference mode, this model will randomly configure the dropout layers and perform the forward propagation. In order to have stable results, the network should perform the prediction on the same input multiple times. This number depends on the dropout rate, and can vary between 50 and 100.

https://gist.github.com/fgadaleta/6d1ef988b8c75a04758b57fee61d031c#file-lstm_with_dropout-py

It turns out that the mean of all predictions is a good approximation of the final prediction, while the standard deviation (of all predictions) is a good measure of the uncertainty of the model for that particular input.

This approach is very similar to the one that leverages ensemble methods that perform independent predictions for the same input. In fact this is an ensemble model with shared parameters.
One challenge of this method consists in empirically estimating the dropout rate, that depends on the geometry of the training data. A low dropout rate might lead to models that have very little difference in their configurations, giving very similar results and low variance. Such a result might be misinterpreted as a model that shows high confidence with the prediction (small variance, hence small uncertainty). In contrast, a dropout rate that is too high might lead to inaccurate predictions due to the low number of available connections/neurons during back-propagation.

As in many other models, the dropout rate should be chosen appropriately by trial and error, possibly with known datasets first.

Originally published at https://amethix.com on January 21, 2019.

Written by

Managing Director @ amethix.com Chief Software Engineer & Host @datascienceathome.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store