Premium problem99. Pairwise Cosine Similarity

Medium Locked

A is (N, D) and B is (M, D). L2-normalise every row of each, then return the (N, M) matrix of cosine similarities between every pair.

No loops. This is the retrieval step behind every embedding search and every image-text matching model.

Input

A =
tensor([[1., 0.],
        [0., 2.]])
B =
tensor([[ 1.,  0.],
        [ 1.,  1.],
        [ 0., -3.]])

Output

tensor([[ 1.0000,  0.7071,  0.0000],
        [ 0.0000,  0.7071, -1.0000]])

Premium problem

This one's part of Premium. Unlock the full PyTorch track plus every other premium problem on the site.

Implement solve(...)