AI Engineer Problems › Applied GenAI

Pairwise Cosine Similarity Matrix

Medium · Coding · Applied GenAI

Concept: Testing the user’s ability to vectorize operations and handle real-world mathematical edge cases (like zero-vectors).

Given a batch of query embeddings A (shape M \times D) and a batch of document embeddings B (shape N \times D), compute the pairwise cosine similarity matrix of shape M \times N. If any vector has a magnitude of 0, the similarity score for that pair should default to 0.0.

Constraints Allowed libraries: numpy No loops allowed (must be fully vectorized)

Solve it in the browser on GenAIDojo