KatInterview

ML Fundamentals: Bayes, Neural Nets, KNN, and Transformers

Machine LearningModel answer
EasyModel answerMachine Learningbayes-theoremneural-networksknn

Last updated

Work through four sets of ML fundamentals: a Bayes' rule posterior with a rare event, neural network arithmetic, KNN concept checks, and Transformer mechanics.

You are preparing for a timed ML engineering assessment. The multiple-choice section covers four topic areas. For each part below, show enough intermediate work that you could confidently eliminate wrong options under time pressure.


Part 1 — Bayes' Rule

A fraud-detection system screens transactions. 3% of transactions are fraudulent. The system correctly flags 92% of fraudulent transactions and incorrectly flags 4% of legitimate ones.

  1. A transaction is flagged. What is the probability it is actually fraudulent?
  2. A second, independent screening tool (same detection and false-positive rates, conditionally independent of the first given the true label) also flags the same transaction. What is the probability now?

Your answer should identify the prior, both likelihoods, and the normalizing constant, and explain why the low base rate matters so much.


Part 2 — Forward-Pass Arithmetic and Parameter Counts

  1. A single neuron has weights (1.0, -0.5, 2.0) and bias 0.5. It receives input (3, 2, 1). Compute its output under a ReLU activation and under a sigmoid activation.

  2. A fully connected feedforward network takes 5 input features, passes them through a hidden layer of 4 units, then produces 3 output values. Every layer includes bias terms. How many trainable parameters does the network have?

  3. A small network has two inputs, two ReLU hidden units, and one linear output unit.

    • Hidden unit 1: weights (1, -2), bias 0
    • Hidden unit 2: weights (0.5, 1), bias -1
    • Output: 3·h1 + (-1)·h2 + 0

    For input (1, 1), compute both hidden activations and the final output. Which parameters receive a zero gradient from this single example during backpropagation, and why?


Part 3 — KNN Concept Checks

1. Mark each statement true or false, with a one-sentence reason.

  • (a) KNN requires almost no computation during training, but prediction time scales with the size of the training set.
  • (b) A larger k always reduces test error.
  • (c) With k = 1, KNN achieves 100% accuracy on the training set, provided no two identical points carry different labels.
  • (d) Multiplying one feature's values by 1,000 (e.g. converting meters to millimeters) does not affect KNN's predictions.
  • (e) KNN generalizes just as well in 1,000-dimensional space as in 10-dimensional space, given the same training set size.
  • (f) KNN can handle regression tasks by returning the mean of the k nearest neighbors' target values.

2. Worked prediction.

One-dimensional training set: class A at positions 0, 1, 3; class B at positions 4 and 6. Using absolute distance and majority vote, classify a query point at 3.5 with k = 1 and with k = 3.


Part 4 — Transformer Concept Checks

1. Mark each statement true or false, with a one-sentence reason.

  • (a) The compute and memory cost of standard scaled dot-product attention grows quadratically with sequence length.
  • (b) Without positional encodings or an attention mask, self-attention is permutation-equivariant: shuffling the input tokens only shuffles the outputs in the same way.
  • (c) Dividing attention scores by √d_k is done primarily to keep the number of learnable parameters small.
  • (d) A causal (autoregressive) mask prevents each token from attending to any token that appears later in the sequence.
  • (e) Using h attention heads with the same model dimension multiplies the total parameter count of the attention projection matrices by h compared to single-head attention.
  • (f) Layer normalization normalizes each feature dimension across all examples in the current batch.

2. Parameter count.

A multi-head attention module has model dimension d_model = 256 and 4 heads. Query, key, value, and output projections each have a bias term. What is the dimension per head? How many total trainable parameters do the four projection matrices (plus their biases) contain?

Hints

Hint 1

For Part 1, write out P(flagged) as a weighted sum over both classes first; this denominator is the piece most often dropped under time pressure. After the first posterior, treat it as the new prior before applying the second observation.

Hint 2

For Part 2 question 3, check the sign of each hidden unit's pre-activation before applying ReLU. Any unit that is inactive (output = 0) passes a zero gradient back to all of its incoming weights and its bias for this example.

Hint 3

For Part 4 question 2, each head works in a d_model/h dimensional subspace. The four projection matrices are W_Q, W_K, W_V (each d_model × d_head) and W_O (d_model × d_model when the heads are concatenated back); count rows × columns for each, then add biases separately.