Making a neural network say “I Don’t Know”: Bayesian NNs using Pyro and PyTorch
towardsdatascience.com
towardsdatascience.com
If this was allowed then I'd make an ImageNet classifier which just discards all images except for one I'm really good at classifying, then claim I have 100% accuracy on ImageNet.
Edit: the entire point of bayesian approach is that you can make decisions on a loss function where you can make a tradeoff on the (business) cost of making a wrong decision and (business) regret of not making a decision. For real world applications, accuracy is a useless metric. What matters is the net (business) benefit or loss.
> My final classifier had accuracy of ~97% on MNIST and it refused to classify white noise and the majority of unrelated (non-MNIST) images.
I'm saying that you are making a misleading claim when you say this. It is categorically untrue that you achieve 97% on MNIST with this approach. Accuracy on a standardized test set makes no sense when you select a subset from the test set for maximal accuracy. I'm not saying that your approach doesn't have value or make sense in some business cases, because I think the overall idea is interesting but these statements are not correct.
Basically Softmax will only give you values close to 1 or close to 0, so there's no room for your own interpretation or threshold.
https://medium.com/@paraschopra/softmax-amplifies-small-diff...
There was another method proposed recently for approximating uncertainties: using dropout during evaluation (which effectively makes different runs on the same image as samples). But I came across a discussion on the efficacy of the technique and I wasn't sure. https://www.reddit.com/r/MachineLearning/comments/7bm4b2/d_w...
My next project is to implement dropout and see how it compares with the current approach. I'll also try your suggestion.
For that, you need actual Bayesian techniques.
In ML, you typically have a model f(w;u), and you are trying to fit the parameters w to some data, given some hyperparameters u that represent some kind of prior information. In frequentist techniques, these could be regularization parameters.
In frequentist techniques, the hyperparameters the fitted parameters are assumed fixed, and not random. In contrast, Bayesian methods assume it is random as well. This extra uncertainty is neglected by the predictions of frequentist techniques.
Nonetheless, the uncertainty is real.
Except this should not be surprising, because softmax is not scale invariant. Values on the unit scale is what you would normally expect, so if you feed it values 100x larger, it's going to be way more confident. If you feed it values 100x smaller, it's going to be way less confident.
Say p is the probability an image is of a cat.
p could be low, e.g. 0.05.
However, perhaps p has the 0.9 credible interval (0.01, 0.1). The width of that interval expresses how confident you are of the point estimate.
The correct translation of "I don't know" is a wide credible interval.
Parameters into a model can be modeled as random variables. Probabilities themselves cannot be, unless they are themselves parameters (or happen to coincide with a parameter) into another part of a statistical model. A statistical estimation of an event (in the sense of probability theory) must bottom out at a single number.
Let's take a coin toss as an example. It does not make sense to say I think that the coin has a 0.05 chance of flipping heads with a 0.9 credible interval of (0.01, 0.1). It does not make sense even to have a probability distribution on coming up heads (strictly speaking).
Coming up heads is an event and events do not distributions associated with them, but rather single probabilities.
On the other hand, we can model the outcomes of a coin with a Bernoulli distribution with parameter p where the probability of heads is p and that of tails is 1 - p. Notice here that p is a parameter that just happens to coincide with our probability. It then makes sense to talk of p as a random variable and having a distribution, credible interval, etc.
Note, however, that our estimation of the event of the coin coming up heads is still a single number at the end of the day. Our intermediate calculations might just involve integrating over the space of intermediate values of p, which means that p no longer coincides with the probability of heads (indeed it doesn't really make sense because p is no longer a single number).
But how do we express the intuition of "confidence in my probability estimate?" After all it seems like there's a difference between me saying this coin has a 50% chance of heads when I know it's fair and me saying a 50% chance of heads when you have me an unfamiliar coin I've never inspected (even though reasonable statistical models for either case might both say it's 50%).
In Bayesian statistics this is encoded entirely by the function that updates your posterior probability. If you are very "confident" your posterior probability will remain similar to your prior probability no matter what evidence you see (I know for sure this coin is fair so even if it comes up tails 1000 times I still think the next time will be 50% chance of heads). If you are not very confident your posterior probability might change more dramatically (hmm... I saw a string of tails, I'm pretty sure the next coin flip probably won't be heads again and I'll modify my probability accordingly).
x_i ~ Bernoulli(p) p ~ Beta(1,1)
For which the posterior IIRC:
p | x ~ Beta(n_tails+1, n_heads + 1)
Which, if you look up the variance of the Beta distribution, you will see a credible interval p shrink around the truth of p=0.5 if the coin is indeed fair.
This encapsulates the fact that if we have 5 heads and 5 tails we think the coin is fair. But if we have 500 heads and 500 tails we still think the coin is fair, but we're much more confident that it is so.
A probability can only be assigned to an event. A distribution can only be assigned to the collection of all events. A probability cannot be an event except as a parameter into another part of a model.
Hence statements of the form "This is a picture of a cat with probability X with this credible interval" are category errors.
Notice how what you're talking about in this example is the parameter p not the probability that the coin turns up heads. Indeed to find the latter you must explicitly integrate over your p.
We agree on the analysis of confidence (although I'm not sure I'd say "I think the coin is fair with low confidence" after e.g. a single heads and a single tails; I'd probably say "I don't know, but that's the mode of my distribution").
My hand-wavy statement about updates alludes to the general case, where you can't necessarily describe your model easily with parameters, but instead have an arbitrary joint distribution. With parameters to your model you can talk about distributions and summary statistics about your parameters (e.g. p for Bernoulli), which is a special case of a general update function, but even if you don't have parameters for which to talk about, your update function still has all the information about "confidence" you need.
- We can say that p = Probability(X_i = heads)
- We have a posterior distribution for p (e.g. under Beta(1,1) prior).
- We can define a=icdf_p(0.05), b=icdf_p(0.95) under the posterior.
- (a,b) is an event in the sigma algebra (of the posterior probability space) for p whose mass is 0.9.
- (a,b) is also a central credible interval.
I don't think you'll be able to point to one of those statements above and show that it is wrong.
I think the stumbling point here is the notion of random measures. Random measures have sample spaces, sigma algebras of events, and their own distributions. Events have probabilities, as you say. This is the sense in which we can speak of probabilities (i.e. probability distributions) having probabilities (i.e. probability distributions). Because Binomials are one dimensional it's particularly easy to think about intervals.
So while it is meaningful to say:
"p has this probability distribution."
It is not meaningful to say:
"The probability the coin comes up heads has this probability distribution."
This is because p and the probability the coin comes up heads are not the same thing (this is even more apparent if you have a non-Bernoulli scenario) when p is no longer fixed.
It's not really just a case of marginal vs conditional probabilities either. Even in the case of a conditional probability, it's only defined for a fixed condition.
Even in the case of random measures, while you can reasonably talk about the probability of a probability distribution, it still doesn't really make sense to talk about the probability distribution of a probability.
But that's exactly what it does mean. For the Dirichlet Process, the probability of hitting a set in the partition of a sample space is random. A DP is a couple of generalisations up from the beta distribution on p under discussion [1]. I.e. this specializes to the current situation as:
* The sample space theta is the set {H, T}.
* The only interesting partition to talk about is {A_1, A_2}={{H}, {T}}.
* The Dirichlet distribution on (G(A_1), G(A_2)) is therefore 2 dimensional, which is a Beta.
* G(A_1) is the probability measure of hitting heads.
No specific parametric model involving univariate p needs to be involved.
[1] c.f. eqn 2 of http://www.gatsby.ucl.ac.uk/~ywteh/research/npbayes/dp.pdf
http://henripal.github.io/blog/langevin
And these papers-
1. https://arxiv.org/abs/1806.02855 2. http://arxiv.org/abs/1811.11145
Any idea how sampling compares in performance and computational efficiency?
Paper: http://arxiv.org/abs/1806.01768
Demonstration: https://muratsensoy.github.io/uncertainty.html
Your comment reminded me of this xkcd comic https://xkcd.com/353/
thres = wrong_cost / (wrong_cost+right_gain)
response = (max(out_probs) > thres) ? argmax(out_probs) : "I don't know"
While the bayesian methods can theoretically perform better for out-of-set inputs, it feels like an oversight to not include this simple method as a baseline. My gut feeling is that this would perform equally well, if not better.Softmax which is popular way to transform network outputs to probabilities pushes probabilities either to zero or one.
There may be other ways to transform outputs to probabilities where your approach could work but I'm not aware of them.
Also to reply to OP's comments, to have a class like 'I don't know' you need to have a dataset of things that don't look like your training dataset. And you'll always miss examples of things that don't belong to your input class. Bayesian solves it in a nice way by not requiring this infinite not-input training data.
Bayesian Approach: Pros and Cons
Pros
Can calculate explicit probabilities for hypotheses.Can be used for statistically-based learning.
Can in some cases outperform other learning methods.
Prior knowledge can be (incrementally) combined with newer knowledge to better approximate perfect knowledge.
Can make probabilistic predictions.
Cons
Require initial knowledge of many probabilities.Can become computationally intractable.
http://www.ru.is/faculty/thorisson/courses/v2008/gervigreind...
This is not a really big issue. You can always use uninformative priors, which is simply the Bayesian way of saying "I don't know" what the probability distribution looks like upfront. Then the data can help you infer the right shape of probabilities.
> Can become computationally intractable.
This is a bummer, but modern methods like variational inference and better sampling techniques are making Bayesian analysis on large-scale data and complex models tractable.
I linked a video the article that I highly recommend watching. Variational Bayes and Beyond: Bayesian Inference for Big Data https://www.youtube.com/watch?v=DYRK0-_K2UU
Can you make a two-stage design where the initial probabilities are generated by another machine learning model?
The gist is, you train a single classifier that will work as two classifiers (with certain restrictions): one that says "false/not-false" and another that says "true/not-true".
If the output of the classifier is "not-false" and "not-true", then you consider that as a "I don't know".
edit: does anyone know of any papers that talk about this technique formally?
By providing a good generative model, you are shifting the effort from collecting more data to carefully coding your expert knowledge. In my view, this is an exciting paradigm as it allows combination of human knowledge (as generating process and prjors) that can be overcome (in posterior distributions) if enough data is accumulated against it.