When having real valued entries (e.g. floats between 0 and 1 as normalized representation for greyscale values from 0 to 256) in our label vector, I always thought that we use MSE(R2-loss) if we want to measure the distance/error between input and output or in general input and label of the network. On the other hand, I also always thought, that binary cross entropy is only used, when we try to predict probabilities and the ground truth label entries are actual probabilities.

Now when working with the mnist dataset loaded via tensorflow like so:

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

Each entry is a float32 and ranges between 0 and 1.

The tensorflow tutorial for autoencoder uses R2-loss/MSE-loss for measuring the reconstruction loss.

Where as the tensorflow tutorial for variational autoencoder uses binary cross-entropy for measuring the reconstruction loss.

Can some please tell me WHY, based on the same dataset with same values (they are all numerical values which in effect represent pixel values) they use R2-loss/MSE-loss for the autoencoder and Binary-Cross-Entropy loss for the variational autoencoder.

I think it is needless to say, that both loss functions are applied on sigmoid outputs.


3 Answers 3


I don't believe there's some kind of deep, meaningful rationale at play here - it's a showcase example running on MNIST, it's pretty error-tolerant.

Optimizing for MSE means your generated output intensities are symmetrically close to the input intensities. A higher-than-training intensity is penalized by the same amount as an equally valued lower intensity.

Cross-entropy loss is assymetrical.

If your true intensity is high, e.g. 0.8, generating a pixel with the intensity of 0.9 is penalized more than generating a pixel with intensity of 0.7.

Conversely if it's low, e.g. 0.3, predicting an intensity of 0.4 is penalized less than a predicted intensity of 0.2.

You might have guessed by now - cross-entropy loss is biased towards 0.5 whenever the ground truth is not binary. For a ground truth of 0.5, the per-pixel zero-normalized loss is equal to 2*MSE.

This is quite obviously wrong! The end result is that you're training the network to always generate images that are blurrier than the inputs. You're actively penalizing any result that would enhance the output sharpness more than those that make it worse!

MSE is not immune to the this behavior either, but at least it's just unbiased and not biased in the completely wrong direction.

However, before you run off to write a loss function with the opposite bias - just keep in mind pushing outputs away from 0.5 will in turn mean the decoded images will have very hard, pixellized edges.

That is - or at least I very strongly suspect is - why adversarial methods yield better results - the adversarial component is essentially a trainable, 'smart' loss function for the (possibly variational) autoencoder.

  • 1
    $\begingroup$ So first of all, you are saying that in the toy example with MNIST dataset, the loss function does not make a big difference as it is trainable/converging for both approaches? What about the reconstruction loss function for VAEs with bilevel datasamples (i.e. each point can be either 0 or 1), what loss function would one use here? And would it be feasible to always use the MSE loss? $\endgroup$ Commented Jun 10, 2018 at 14:41
  • 1
    $\begingroup$ Mostly, I'm saying it doesn't make a big difference because it's not a model intended for real-life deployment - it's just a very rough proof of concept, you got some digits to show, close enough for government work. If your data is a binary label, on/off, binary cross-entropy will be just about perfect - that's what it was designed for. MSE is... serviceable, I suppose - mostly if your activation function is constrained to a 0->1 range AND your target is strictly binary; any other case and it starts to tear at the seams. $\endgroup$
    – jkm
    Commented Jun 10, 2018 at 15:20
  • 1
    $\begingroup$ Regarding your actual answer, what do you mean by "cross-entropy loss is biased towards 0.5 whenever the ground truth is not binary" ? And I also did not get what you meant with "a loss function with the opposite bias" ? Thanks already for the great discussion :)! $\endgroup$ Commented Jun 10, 2018 at 18:28
  • 3
    $\begingroup$ Likewise, glad to hear you're enjoying it! It's basically just restating the same thing I said a moment earlier - the loss is higher for outputs further from 0.5 than the inputs. So for ground truth of 0.3, outputting 0.2 is treated as worse than outputting 0.4, and likewise for 0.7, outputting 0.8 is discouraged more than a 0.6. $\endgroup$
    – jkm
    Commented Jun 11, 2018 at 17:55
  • 1
    $\begingroup$ The alternative loss - that would be a function that also has a minimum when p(x) == q(x) and increases assymetrically if they are different, but favors 'overshooting' the probability away from 0.5 in either direction, something along the lines of KLD from a bimodal Gaussian I suppose. $\endgroup$
    – jkm
    Commented Jun 11, 2018 at 18:07

This discussion suggests that binary cross entropy is used in VAE case mainly for better optimization behavior. Another reason it works well is that MNIST dataset roughly follows multivariate Bernoulli distribution - the pixel values are close to either zero or one and binarization does not change it much. For more in-depth explanation of this, see Using a Bernoulli VAE on real-valued observations.


It depends on how you assume the model for the likelihood. In other words, in variational autoencoders you seek to minimize the ELBO (empirical lower bound), which contains $KL(q||p)$ which is managed by the encoder and a second term known as the reconstruction error $E_{q}[log p(x|z)]$ managed by the decoder and requires sampling, here is where the choise of the model of $p(x|z)$ comes into play. If you assume it follows a normal distribution you will end up with a MSE minimization since $p(x|z)$ can be reformulated as $p(x|\hat{x}) \sim \mathcal{N}(\hat{x},\sigma)$, if you assume a multinoully distribution you will use cross entropy.

Just a side note taken from Goodfellow's book: Many authors use the term “cross-entropy” to identify specifically the negative log-likelihood of a Bernoulli or softmax distribution, but that is a misnomer. Any loss consisting of a negative log-likelihood is a crossentropy between the empirical distribution defined by the training set and the probability distribution defined by model. For example, mean squared error is the cross-entropy between the empirical distribution and a Gaussian model.

  • $\begingroup$ Can you elaborate on the cross entropy part? I don't understand why you can even use it for the MNIST data, as for each pixel you don't have a 0 or 1 true value, you can have anything in between. Also - what do you mean to assume multinomial? If $x|z \sim MN$ what does $f(z)$ produces? Probabilities for each pixel? $\endgroup$ Commented Aug 16, 2020 at 20:17
  • $\begingroup$ Ok, for the CE on mnist data, even though the true values are not 0 or 1, the loss is still minimized by setting the output values to be the same as the true values. $\endgroup$ Commented Aug 17, 2020 at 6:45
  • $\begingroup$ I think the multivariate-Bernoulli that Jan Kukacka suggested in his answer is more fitting. The MN pmf doesn't fit to a binary-CE. $\endgroup$ Commented Aug 17, 2020 at 7:11
  • $\begingroup$ Yes, you are right, I just leave it as just cross entropy and not binary cross entropy for the general case, but if it is binary cross entropy you have a bernoulli. $\endgroup$
    – A.Duque
    Commented Jan 24, 2021 at 17:30
  • $\begingroup$ Is there a derivation somewhere where a gaussian p(x|z) leads to MSE? $\endgroup$
    – a12345
    Commented Aug 12, 2021 at 7:12

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.