KatInterview

CNN vs Recurrent Models, Plus From-Scratch K-Means

Machine LearningModel answer

Last updated

Compare convolutional and recurrent architectures, derive LSTM gating and tensor shapes, then implement and analyze a vectorized K-means with k-means++ seeding.

This is a two-part question on representation learning and unsupervised clustering.

Part A — Architectures. For a 224×224 RGB image and for variable-length text, compare convolutional networks (CNNs) with recurrent networks (RNNs). Cover:

  • Inductive biases: spatial locality and translation equivariance for images; ordering and sequential state for text.
  • Parameter sharing and receptive-field growth.
  • Ability to capture long-range dependencies and how much sequential (time-step) computation each requires.
  • Situations where a 1D CNN (optionally dilated/causal) can substitute for an RNN or a Transformer.

Then treat LSTMs specifically: write the equations for the input, forget, output, and candidate gates; explain how the cell-state pathway reduces vanishing gradients compared with a vanilla RNN; and report output and final hidden/cell-state shapes for an input of shape (batch=32, seq_len=100, feat=64) with hidden size 128, for both a one-layer unidirectional and a one-layer bidirectional model.

Part B — Clustering. Implement K-means from scratch in Python/NumPy with:

  • k-means++ initialization,
  • vectorized assignment and centroid-update steps,
  • stopping when the within-cluster sum-of-squares objective stops improving.

Also analyze: per-iteration time complexity, memory usage and how to reduce it, handling of empty clusters, the effect of feature scaling and outliers, methods for choosing k (elbow, silhouette, BIC), why the objective cannot increase across an iteration, and how a mini-batch version would differ and when you would use it.

What is expected: a written technical answer for Part A with correct equations, shapes, and parameter counts; working, vectorized code for Part B with a documented interface and sensible defaults; and a discussion of trade-offs and failure modes.

Hints

Hint 1

Think about what assumptions each architecture bakes into the data, and how receptive field grows as a function of depth, kernel size, stride, and dilation.

Hint 2

The cell-state recurrence is a near-identity additive path; expand the gradient of c_t with respect to c_{t-1} to see why.

Hint 3

Use the squared-distance expansion ||x-mu||^2 = ||x||^2 - 2x.mu + ||mu||^2 to turn assignment into one matrix multiplication; also watch for empty clusters and tiny negative values from floating point.