MLCrack

    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

    a⋅b  =  ∑i=1naibi  =  ∥a∥ ∥b∥cos⁡θa \cdot b \;=\; \sum_{i=1}^{n} a_i b_i \;=\; \|a\|\,\|b\|\cos\theta

    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.)