Skip to content

Improve the numerical stability of cosine similarity #134

Open
@chenzhekl

Description

@chenzhekl

Related: pytorch/pytorch#31378
Current implementation:

tmp_dist = Cosine<scalar_t>::dot(x, y, n_x, n_y, dim) /

As introduced in the linked PR for PyTorch, normalizing the vectors first and then doing the inner product could prevent overflow for vectors with large norms. In addition, this could ensure the calculated cosine similarity is within the range [-1, +1].

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions