addons icon indicating copy to clipboard operation
addons copied to clipboard

NaN Loss bug and a step-by-step code analysis

Open tdrvlad opened this issue 4 years ago • 2 comments

Following on problems I had while training with Triplet Loss, I went through the backend code and gathered some personal / visual explanations. Feel free to use my resources.

https://tudorvladstefan.medium.com/de-mystifying-tensorflows-triplet-loss-deca00ca9479

tdrvlad avatar Apr 09 '21 11:04 tdrvlad

triplet_loss = tf.math.truediv( tf.math.reduce_sum( tf.math.maximum(tf.math.multiply(loss_mat, mask_positives), 0.0) ), num_positives, )

I think they should test if num_positives is equal to zero or not.

yacinemrabet1 avatar Jan 17 '22 20:01 yacinemrabet1

Hi, yes, indeed, the nan error results from there being not tough positives in the batch (one has to take this into account, especially when dealing with small batches) I took the time to elaborate in an article: https://tudorvladstefan.medium.com/de-mystifying-tensorflows-triplet-loss-deca00ca9479 Cheers!

On Mon, 17 Jan 2022 at 22:48, Yacine MRABET @.***> wrote:

triplet_loss = tf.math.truediv( tf.math.reduce_sum( tf.math.maximum(tf.math.multiply(loss_mat, mask_positives), 0.0) ), num_positives, )

I think they should test if num_positives is equal to zero or not.

— Reply to this email directly, view it on GitHub https://github.com/tensorflow/addons/issues/2447#issuecomment-1014876203, or unsubscribe https://github.com/notifications/unsubscribe-auth/AK3BQEHZ5LOSHWXCRQZ7OHLUWR6AFANCNFSM42U2DLIA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you authored the thread.Message ID: @.***>

tdrvlad avatar Jan 18 '22 10:01 tdrvlad