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?

  • $\begingroup$ Related: stats.stackexchange.com/questions/198445/… $\endgroup$
    – naught101
    Mar 29, 2016 at 0:46
  • $\begingroup$ 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! $\endgroup$ May 22, 2017 at 18:17
  • $\begingroup$ 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? $\endgroup$
    – Mayou36
    May 22, 2017 at 19:09
  • $\begingroup$ @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? $\endgroup$ May 23, 2017 at 16:04
  • 1
    $\begingroup$ "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. $\endgroup$ May 13, 2019 at 1:15

2 Answers 2


$\newcommand{\bx}{\mathbf{x}}$ $\newcommand{\by}{\mathbf{y}}$

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.

  • $\begingroup$ Thank you for the answer @bayerj, this should be very useful to me. I have a follow-up question: In this answer, you've said that p(y|x) is given. But how might I go about creating p(y|x) when all I have is my "noisy version" of x? Do I use point estimates when training? $\endgroup$ Oct 11, 2022 at 14:41
  • $\begingroup$ I also feel that you may have missed the normalization term (1/N) in your approximation function $\endgroup$ Oct 11, 2022 at 19:07

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

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.

  • $\begingroup$ How about regression error? $\endgroup$ May 23, 2017 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.