ML MATH & LINEAR ALGEBRA · INTERVIEW QUESTION
What is a dot product and why does it matter in ML?
🟢 beginner · Linear Algebra
Hint: Sum of element-wise products; measures alignment.
Answer
Concept
The dot product takes two equal-length vectors and returns one number that captures how much they point in the same direction. It is the atomic operation behind every neuron, every attention score, and every similarity metric in ML.
Formula
Intuition
- Positive → vectors aligned, zero → orthogonal, negative → opposing.
- A neuron's pre-activation is just
dot(weights, inputs) + bias. - Cosine similarity is a normalized dot product.
NumPy
python
import numpy as np
a = np.array([1., 2., 3.])
b = np.array([4., -1., 2.])
print(np.dot(a, b)) # 8.0
print(a @ b) # 8.0 (Python 3.5+ operator)PyTorch
python
import torch
a = torch.tensor([1., 2., 3.])
b = torch.tensor([4., -1., 2.])
print(torch.dot(a, b)) # tensor(8.)
print(a @ b) # tensor(8.)Related questions
- What is the difference between eigenvalues and singular values?
- Explain the role of SVD in PCA. How does truncated SVD relate to dimensionality reduction?
- What is a matrix and why are matrices important in machine learning?
- What is the rank of a matrix? Why does it matter in ML?
- Explain the relationship between eigendecomposition and diagonalization. Why is it useful?