3
$\begingroup$

I am implementing a custom loss in keras, for example, a sum:

def custom_loss(y_true, y_pred):
    K.sum(y_true, y_pred)

Now, I want to normalize it by the batch size. Is it possible, to retrieve the batch size from y_true or y_pred?

$\endgroup$

3 Answers 3

2
$\begingroup$

I would say this highly depends on how you have your code set up. What type of model are you creating? Are you not setting batch-size as a global parameter? Can you not just use that? Also, assuming y_true is a numpy matrix, can you just use: y_true.shape[dimension_representing_batch_size] as a means of getting the size?

$\endgroup$
2
$\begingroup$

Your batch size is y_true.shape[0]

To normalized, which I assume you are looking for loss per observations what you need is below,

def custom_loss(y_true, y_pred):
    return K.sum(y_true, y_pred) / tf.constant(y_true.shape[0], dtype=tf.int32)

Or why not just take the mean?

def custom_loss(y_true, y_pred):
    return K.mean(y_true, y_pred)
$\endgroup$
1
$\begingroup$

You can use :

def custom_loss(y_true, y_pred):
    return K.sum(y_true, y_pred) / tf.shape(y_true)[0], dtype=tf.int32)
$\endgroup$

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.