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

cosine_distance_to_prototypes() and l2_distance_to_prototypes() are falsely named #143

Open
Y-T-G opened this issue Mar 19, 2024 · 2 comments
Labels
enhancement New feature or request question Further information is requested

Comments

@Y-T-G
Copy link

Y-T-G commented Mar 19, 2024

If the model is returning cosine distance:

scores = self.cosine_distance_to_prototypes(query_features)

Does that mean the lower the better?

@Y-T-G Y-T-G added the question Further information is requested label Mar 19, 2024
@ebennequin
Copy link
Collaborator

ebennequin commented Mar 19, 2024

From the code and docstring of the cosine_distance_to_prototypes() method:

    def cosine_distance_to_prototypes(self, samples) -> Tensor:
        """
        Compute prediction logits from their cosine distance to support set prototypes.
        Args:
            samples: features of the items to classify of shape (n_samples, feature_dimension)
        Returns:
            prediction logits of shape (n_samples, n_classes)
        """
        return (
            nn.functional.normalize(samples, dim=1)
            @ nn.functional.normalize(self.prototypes, dim=1).T
        )

The method actually doesn't return cosine distances but predictions logits equal to the cosine similarity, so the higher is actually the better.

Same logic with the other available "distance", which is actually logits as the opposite of the distance:

    def l2_distance_to_prototypes(self, samples: Tensor) -> Tensor:
        """
        Compute prediction logits from their euclidean distance to support set prototypes.
        Args:
            samples: features of the items to classify of shape (n_samples, feature_dimension)
        Returns:
            prediction logits of shape (n_samples, n_classes)
        """
        return -torch.cdist(samples, self.prototypes)

Calling the methods cosine_distance_to_prototypes() and l2_distance_to_prototypes() is a misleading naming. I am marking this as a much needed enhancement to the library.

@ebennequin ebennequin added the enhancement New feature or request label Mar 19, 2024
@ebennequin ebennequin changed the title Cosine Distance cosine_distance_to_prototypes() and l2_distance_to_prototypes() are falsely named Mar 19, 2024
@Y-T-G
Copy link
Author

Y-T-G commented Mar 19, 2024

I see. That makes it clear. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants