Search code examples
kerasobjective-function

Keras: Why do loss functions have to return one scalar per batch item rather than just one scalar?


I'm writing a custom loss function in Keras and just tripped over the following:

Why do Keras loss functions have to return one scalar per batch item rather than just one scalar?

I care about the cumulative loss for the whole batch, not about the loss per item, don't I?


Solution

  • I think I figured it out: fit() has an argument sample_weight with which you can assign different weights to different samples in the batch. In order for this to work you need the loss function to return the loss per batch item.