Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow tf.losses.cosine_distance is greater than one

I'm training a Neural network on Tensorflow and I'm using tf.losses.cosine_distance as a loss function.

The training proceeds well, but my concern is that during the training I have values of losses > 1. The cosine distance (if the input tensors are normalized to 1), should always be a value less than one? How is the loss calculated? Is it a sum of losses in a batch?

like image 246
Enrico Beltramo Avatar asked Oct 19 '25 04:10

Enrico Beltramo


1 Answers

Correct, tf.losses.cosine_distance has reduction argument, which equals to reduction=Reduction.SUM_BY_NONZERO_WEIGHTS by default:

cosine_distance(
    labels,
    predictions,
    dim=None,
    weights=1.0,
    scope=None,
    loss_collection=tf.GraphKeys.LOSSES,
    reduction=Reduction.SUM_BY_NONZERO_WEIGHTS      # <-- Here
)

In this case, it computes the sum of all cosine distances across the batch. Change it to Reduction.MEAN and you'll have a mean loss across the batch, which is usually what you want.

like image 131
Maxim Avatar answered Oct 21 '25 18:10

Maxim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!