# How to propagate uncertainty into the prediction of a neural network?

I have inputs $x_1\ldots x_n$ that have known $1\sigma$ uncertainties $\epsilon_1 \ldots \epsilon_n$. I am using them to predict outputs $y_1 \ldots y_m$ on a trained neural network. How can I obtain 1$\sigma$ uncertainties on my predictions?

My idea is to randomly perturb each input $x_i$ with normal noise having mean 0 and standard deviation $\epsilon_i$ a large number of times (say, 10000), and then take the median and standard deviation of each prediction $y_i$. Does this work?

I fear that this only takes into account the "random" error (from the measurements) and not the "systematic" error (from the network), i.e., each prediction inherently has some error to it that is not being considered in this approach. How can I properly obtain $1\sigma$ error bars on my predictions?

• – naught101 Mar 29 '16 at 0:46
• Maybe you can try some error dedicated library such as 'errors' on R. As far as I know, attaches an attribute (please r-lovers correct me if I'm wrong) to vectorized data and maybe it propagates to the output. cran.r-project.org/package=errors good luck! – SantiagoRodriguez May 22 '17 at 18:17
• What do you mean by "systematic" error (from the network)? So there's no systematic error on the measurements? Do you mean an error induced by the network as it is not perfect itself? – Mayou36 May 22 '17 at 19:09
• @Mayou36 Yes, the latter. Let's say that cross validation establishes that the predictions have on average an accuracy of 95%. We therefore know the network isn't perfect. Normally when you measure something, you state not only the measurement but also the uncertainty on the measurement. How do you obtain the uncertainty of a prediction from a neural network? – rhombidodecahedron May 23 '17 at 16:04
• "perturbing" the data probably means generating observations from some distribution, right? I think the error will depend heavily on what distribution you choose to simulate from. If you are doing a simple MLP Neural Network, why not just construct a Bayesian neural network and obtain credible intervals for the predictions? PyMC3 has a notebook on doing just this. – Demetri Pananos May 13 '19 at 1:15


I personally prefer the Monte Carlo approach because of its ease. There are alternatives (e.g. the unscented transform), but these are certainly biased.

Let me formalise your problem a bit. You are using a neural network to implement a conditional probability distribution over the outputs $$\by$$ given the inputs $$\bx$$, where the weights are collected in $$\theta$$:

$$p_\theta(\by~\mid~\bx).$$

Let us not care about how you obtained the weights $$\theta$$–probably some kind of backprop–and just treat that as a black box that has been handed to us.

As an additional property of your problem, you assume that your only have access to some "noisy version" $$\tilde \bx$$ of the actual input $$\bx$$, where $$\tilde \bx = \bx + \epsilon$$ with $$\epsilon$$ following some distribution, e.g. Gaussian. Note that you then can write $$p(\tilde \bx\mid\bx) = \mathcal{N}(\tilde \bx| \bx, \sigma^2_\epsilon)$$ where $$\epsilon \sim \mathcal{N}(0, \sigma^2_\epsilon).$$ Then what you want is the distribution $$p(\by\mid\tilde \bx) = \int p(\by\mid\bx) p(\bx\mid\tilde \bx) d\bx,$$ i.e. the distribution over outputs given the noisy input and a model of clean inputs to outputs.

Now, if you can invert $$p(\tilde \bx\mid\bx)$$ to obtain $$p(\bx\mid\tilde \bx)$$ (which you can in the case of a Gaussian random variable and others), you can approximate the above with plain Monte Carlo integration through sampling:

$$p(\by\mid\tilde \bx) \approx \sum_i p(\by\mid\bx_i), \quad \bx_i \sim p(\bx\mid\tilde \bx).$$

Note that this can also be used to calculate all other kinds of expectations of functions $$f$$ of $$\by$$:

$$f(\tilde \bx) \approx \sum_i f(\by_i), \quad \bx_i \sim p(\bx\mid\tilde \bx), \by_i \sim p(\by\mid\bx_i).$$

Without further assumptions, there are only biased approximations.

It depends on what kind of error you want to determine.

Training data vs applying data differences

A technique used to estimate the errors on the predictions is to train several algorithms using different random seeds. For most algorithms, this will lead to different predictions: the variation may gives you an estimate.

Classification specific

So in order to determine the classification error, there are roughly two methods:

event by event:
You can simply look at the predictions, create (for example) bins and divide label 1 by label 0. Because having 100 events of label 1 with a prediction between 0.6-0.65 and 50 with label 0 with a prediction in the same range simply yields a 2/3 chance for an event to be of class 1. Or, in other words, with a 1/3 change, your events in that bin are not class 1.

Total efficiency:
This approach is the one to use if it fits your case, it is more specific. You first determine where you apply your cut (meaning: what's the threshold on the predictions for an event to be class 1 or 0; this is usually not 0.5 but an optimized figure of merit). Let's say you cut on 0.9 (so <0.9 -> class 0, else class 1). Then you can count:

• how many class 1 events are lost (lower then 0.9)?
• how many class 0 events are still in the sample?

This gives you an estimation of the error on your classifiers output.

Regression specific

tag-and-probe:
You can use known values, enter them and get their error. Then, you may assume that values in between two of those, roughly have the average error. Or in other words, you extrapolate the error from known values.

Simple average: Simply take the average of the errors. If they are roughly equally distributed, this is a good way to go.

• How about regression error? – rhombidodecahedron May 23 '17 at 17:05