# What is the best loss function for convolution neural network and autoencoder?

What is the best choice for loss function in Convolution Neural Network and in Autoencoder in particular - and why?

I understand that the MSE is probably not the best choice, because little difference in lighting can cause a big difference in end loss.

What about Binary cross-entropy? As I understand, this should be used when target vector is composed as 1 at one place and 0 at all others, so you compare only class that should be correct (and ignore others),... But this is an image (although the values are converted in 0-1 values,...)

– nbro
Jul 11 '19 at 17:12

There is no right answer to this. Finding the right loss function is a tough and difficult problem. So your goal as the architect is to try to find one that best suits your needs. So lets think about your needs.

You mention that you dont want lighting shifts to cause large error, so ill take a leap and assume you care more about the shapes and style of the image more than the coloring. To deal with this, maybe consider using difference of the gram matrices (this is considered common place in style transfer literature: A Neural Algorithm of Artistic Style) Note that you could use the encoder to get the representation of the output as well for the loss, $$L(x) = D(Gram(Enc(x)), Gram(Enc(\hat x))$$ where $$D$$ would be some distance metric like euclidian distance.

Maybe the outline is all you care about. You could use some known edge detector filter and compare those, ex: $$D(Edge(x), Edge(\hat x))$$

Maybe you just dont care about color shifts, you could do $$D(x - \mu_x, \hat x - \mu_{\hat x}$$).

Note that you can play around with whatever distance metric you use, whether it be with MSE, RMSE, MAE, etc... Each has their own small pros/cons based on the loss manifolds they create. In your case i dont think the difference there will be night and day though, but you never know.

Also mixing and matching is always nice: ex: $$L(x) = \lambda_1 D(x, \hat x) + \lambda_2D(Gram(Enc(x)), Gram(Enc(\hat x)) + ...$$

Takeaway: MSE might actually be fine, but it really depends on what you prioritize, and once you figure that out you can start getting clever and design the loss that fits your needs and problem