Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent pairwise hinge loss values when using masked labels #359

Open
Sahandw opened this issue Dec 6, 2023 · 0 comments
Open

Inconsistent pairwise hinge loss values when using masked labels #359

Sahandw opened this issue Dec 6, 2023 · 0 comments

Comments

@Sahandw
Copy link

Sahandw commented Dec 6, 2023

Issue type
Bug

I'm noticing inconsistent behavior in tfr.keras.losses.PairwiseHingeLoss when using labels with negative values (which are masked) or when using precise masks with default reduction.
Expectation is to have identical loss results when masks (labels with negative values) are used, however, I'm noticing inconsistent values. I think the reason for this is that loss is normalized to the sequence length irrespective of masks.

What makes this even more inconsistent is that when a lambda_weight is set for the loss (for example: tfr.keras.losses.NDCGLambdaWeight()), the results will be consistent.

import tensorflow_ranking as tfr
import tensorflow as tf

loss = tfr.keras.losses.PairwiseHingeLoss()
loss_weighted = tfr.keras.losses.PairwiseHingeLoss(lambda_weight=tfr.keras.losses.NDCGLambdaWeight())

y_true = tf.random.uniform(shape=(2, 16))
y_pred = tf.random.uniform(shape=(2, 16))
y_true_masked = tf.concat(
    [y_true, tf.ones(shape=(2, 16)) * -1.0], axis=1
)
y_pred_masked = tf.concat([y_pred, tf.random.uniform(shape=(2, 16))], axis=1)


# Expectation: return same values
loss(y_true, y_pred).numpy(), loss(y_true_masked, y_pred_masked).numpy() # output: (8.201115, 4.1005573)

loss_weighted(y_true, y_pred).numpy(), loss_weighted(y_true_masked, y_pred_masked).numpy() # output: (0.9949929, 0.9949929)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant