Bird
Raised Fist0

Implement Cosine Similarity and Euclidean Distance

CODINGbeginnerInterview freq: very_high
Choose your preparation mode6 modes available
▶
Execution Trace
Step 1/5
Dot Product

Dot product calculated as 32 from vectors [1, 2, 3] and [4, 5, 6]

🎯 Understanding dot product is crucial for cosine similarity.
dot_product = sum(a * b for a, b in zip(vector_a, vector_b))
Magnitude A

Magnitude of vector A calculated as 3.74

🎯 Magnitude calculation is essential for normalizing vectors.
magnitude_a = math.sqrt(sum(a * a for a in vector_a))
Magnitude B

Magnitude of vector B calculated as 8.77

🎯 Magnitude helps in scaling vectors to unit length.
magnitude_b = math.sqrt(sum(b * b for b in vector_b))
Cosine Similarity

Cosine similarity calculated as 0.97

🎯 Cosine similarity measures the angle between vectors.
cosine_similarity = dot_product / (magnitude_a * magnitude_b)
Euclidean Distance

Euclidean distance calculated as 5.20

🎯 Euclidean distance measures the straight-line distance between points.
euclidean_distance = math.sqrt(sum((a - b) ** 2 for a, b in zip(vector_a, vector_b)))
📊
Diagram
Diagram
┌───────────────────────────────┐
│ Vector A: [1, 2, 3]          │
│ Vector B: [4, 5, 6]          │
├───────────────────────────────┤
│ Cosine Similarity Calculation │
│ ───────────────────────────── │
│ Dot Product: 1*4 + 2*5 + 3*6 │
│ Magnitude A: sqrt(1^2 + 2^2 + 3^2) │
│ Magnitude B: sqrt(4^2 + 5^2 + 6^2) │
│ Cosine Sim: Dot / (Mag A * Mag B)  │
├───────────────────────────────┤
│ Euclidean Distance Calculation │
│ ────────────────────────────── │
│ Difference: [(1-4), (2-5), (3-6)] │
│ Squared: [9, 9, 9]             │
│ Sum of Squares: 27             │
│ Euclidean Dist: sqrt(27)       │
└───────────────────────────────┘
  • Vector A: Input vector 1
  • Vector B: Input vector 2
  • Dot Product: Sum of element-wise products
  • Magnitude: Length of a vector
  • Difference: Element-wise subtraction
🔑
Key Moments
▲
Q: Why is the dot product important in calculating cosine similarity?
A: The dot product measures the degree of alignment between two vectors, which is central to cosine similarity.
Q: How does the magnitude of a vector affect cosine similarity?
A: Magnitude normalizes the vector, allowing cosine similarity to measure only the angle between vectors, not their length.
Q: Why is Euclidean distance a measure of dissimilarity?
A: Euclidean distance quantifies the straight-line distance between two points, indicating how far apart they are.

This document explains the implementation of Cosine Similarity and Euclidean Distance between two vectors. The diagram provides a visual breakdown of the calculations, showing the dot product, magnitudes, and differences. The execution trace walks through each step, highlighting key operations like dot product calculation and magnitude normalization. Key moments test understanding of these concepts, ensuring a deep grasp of how these metrics quantify similarity and dissimilarity between vectors.

Primary Focus

✓ Understanding the implementation of Cosine Similarity and Euclidean Distance

Practice

(1/5)
1. What is the time complexity of applying a dropout layer to an input of size n during training?
easy
A. O(1)
B. O(log n)
C. O(n^2)
D. O(n)

Solution

  1. Step 1: Analyze Dropout Operation

    Applying dropout involves generating a mask and element-wise multiplication, both O(n) operations.
  2. Step 2: Eliminate Wrong Options

    O(1) suggests constant time, which is incorrect. O(n^2) suggests quadratic time, which is excessive. O(log n) suggests logarithmic time, which is not applicable.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Dropout involves element-wise operations. [OK]
Hint: Dropout involves element-wise operations. [OK]
Common Mistakes:
2.
import numpy as np

X = np.array([[1, 2], [3, 4], [5, 6]])
X_meaned = X - np.mean(X, axis=0)
cov_matrix = np.cov(X_meaned, rowvar=False)
print(cov_matrix)
What does this code output?
easy
A. A 2x3 covariance matrix
B. A 3x3 covariance matrix
C. A 2x2 covariance matrix
D. A 3x2 covariance matrix

Solution

  1. Step 1: Understand the covariance matrix calculation

    The covariance matrix is calculated for the features (columns) of the dataset.
  2. Step 2: Eliminate wrong options

    A 3x3 covariance matrix is incorrect because the covariance matrix is not 3x3 for a 2-feature dataset. A 2x3 covariance matrix and D are incorrect because covariance matrices are square.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Covariance matrix is square, size = features. [OK]
Hint: Covariance matrix is square, size = features. [OK]
Common Mistakes:
3.
import numpy as np

# Backpropagation for a single neuron
inputs = np.array([1.0, 2.0, 3.0])
weights = np.array([0.2, 0.8, -0.5])
bias = 2.0
learning_rate = 0.01

d_output = np.array([1.0])  # Gradient of loss w.r.t output

# Incorrect gradient calculation
weights_gradient = d_output * inputs
weights -= learning_rate * weights_gradient
print(weights)
medium
A. Weights are updated correctly.
B. Weights are updated incorrectly due to incorrect learning rate.
C. Weights are not updated; learning rate is too small.
D. Weights are updated incorrectly due to missing chain rule.

Solution

  1. Step 1: Identify error in gradient calculation

    The code incorrectly applies the gradient directly without considering the chain rule.
  2. Step 2: Correct approach

    Should multiply by the derivative of the activation function.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Always apply chain rule in backpropagation [OK]
Hint: Always apply chain rule in backpropagation [OK]
Common Mistakes:
4. How can you optimize the backward pass of batch normalization to improve computational efficiency?
hard
A. Fuse batch normalization with preceding linear layers.
B. Pre-compute gradients for all layers.
C. Use a smaller batch size to reduce computation.
D. Use a higher learning rate to speed up convergence.

Solution

  1. Step 1: Identify optimization techniques

    Fusing operations can reduce computational overhead.
  2. Step 2: Evaluate each option

    Fusing batch normalization with linear layers reduces redundant computations.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Fuse layers to optimize batch norm [OK]
Hint: Fuse layers to optimize batch norm [OK]
Common Mistakes:
5. How can you optimize the memory usage of the Skip-Gram model with negative sampling when dealing with a large vocabulary?
hard
A. Use float64 for embeddings.
B. Implement sparse embeddings.
C. Reduce the embedding dimension.
D. Use a larger batch size.

Solution

  1. Step 1: Identify memory bottlenecks

    Large vocabularies lead to large embedding matrices, consuming significant memory.
  2. Step 2: Evaluate optimization strategies

    Sparse embeddings store only non-zero entries, significantly reducing memory usage compared to dense matrices.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Sparse embeddings save memory for large vocabularies [OK]
Hint: Sparse embeddings save memory for large vocabularies [OK]
Common Mistakes:
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard