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

feat(losses): add hinge_embedding_loss to ivy experimental API. #27048

Merged
merged 8 commits into from
Jan 1, 2024

Conversation

akshatvishu
Copy link
Contributor

@akshatvishu akshatvishu commented Oct 17, 2023

PR Description

The test are failing locally due to error while using @with_supported_device_and_dtypes decorator.
For more info regarding the same, please read this discussion at ivy discord.

Update(19 Oct 2023): The above issue is resolved(ref:link)
and the test are also passing locally for all backend but had to turn test_gradients=False otherwise tests fail for JAX and paddle backends.

Related Issue

Closes #26974

Checklist

  • Did you add a function?
  • Did you add the tests?
  • Did you run your tests and are your tests passing?
  • Did pre-commit not fail on any check?
  • Did you follow the steps we provided?

The test are failing locally due to error while using @with_supported_device_and_dtypes
decorator.
@github-actions
Copy link
Contributor

Thank you for this PR, here is the CI results:


This pull request does not result in any additional test failures. Congratulations!

@ivy-leaves ivy-leaves added Array API Conform to the Array API Standard, created by The Consortium for Python Data API Standards Ivy API Experimental Run CI for testing API experimental/New feature or request PaddlePaddle Backend Developing the Paddle Paddle Backend. Ivy Functional API labels Oct 17, 2023
@akshatvishu
Copy link
Contributor Author

Another key thing to note is that although I have chosen ground_truth_backend=torch, there is a subtle difference between the implementation of the hinge embedding loss between PyTorch and PaddlePaddle native API's and I 've gone with the PaddlePaddle approach within my composition for Jax, NumPy and Tensorflow backends for IVY experimental API due to paddle one being much faithful the mathematical definition of Hinge_Loss

Pytorch:

def hinge_embedding_loss(self, target, margin, size_average):
    zeros = at.zeros_like(self)
    margin_clamp = (margin - self).clamp_min_(0)
    output_margin = at.where(target != 1, margin_clamp, zeros)
    output_self = at.where(target != -1, self, zeros)
    output = (output_margin + output_self).sum()

Paddle:

def hinge_embedding_loss(input, label, margin=1.0, reduction='mean', name=None):
    zero_ = paddle.zeros([1], dtype=input.dtype)
    loss = paddle.where(label == 1.0, input, zero_) + paddle.where(
        label == -1.0, paddle.nn.functional.relu(margin - input), zero_
    )

In the PyTorch implementation, if the target is not 1, the input value is clamped to a minimum of 0 after subtracting from the margin. If the target is not -1, the input value is directly taken.

In the PaddlePaddle implementation, for the target/label of 1, the input value is taken directly. For the target/label of -1, the rectified linear unit (ReLU) function is applied to the difference of the margin and input.

However, both implementations do not handle cases where the label/target is neither -1 nor 1. In such scenarios, depending on the input value, the loss might behave differently between the two implementations.

Reference:

@akshatvishu
Copy link
Contributor Author

Hi @MahmoudAshraf97, can you please take a look at this once ?

@Sarvesh-Kesharwani
Copy link
Contributor

Hi @akshatvishu
please resolve the merge conflicts

@Sarvesh-Kesharwani
Copy link
Contributor

LGTM!
image

@Sarvesh-Kesharwani Sarvesh-Kesharwani merged commit a5aea35 into ivy-llc:main Jan 1, 2024
135 of 141 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Array API Conform to the Array API Standard, created by The Consortium for Python Data API Standards Ivy API Experimental Run CI for testing API experimental/New feature or request Ivy Functional API PaddlePaddle Backend Developing the Paddle Paddle Backend.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

hinge_embedding_loss
6 participants