Deep Learning Interview Reference With a Working NumPy Network

CAREER Published Updated 4 mins read By Leon Wei
Deep Learning Interview Reference With a Working NumPy Network cover image

Quick summary

Summarize this blog with AI

This deep learning reference connects architecture choices to a small executable network. The code uses NumPy to expose forward propagation, loss, gradients, and parameter updates. It is a teaching example on four synthetic XOR observations, not evidence that a model generalizes to new data.

Choose an architecture for the structure of the problem

ArchitectureUseful starting contextImportant tradeoff
Multilayer perceptronFixed-length feature vectorsDoes not inherently encode spatial or sequence structure
Convolutional networkLocal spatial patterns, including imagesReceptive field and representation choices affect captured context
Recurrent networkSequential state updatesSequential computation and long-range dependencies can be difficult
TransformerAttention over token or feature sequencesStandard full attention forms a quadratic-size score matrix in sequence length
AutoencoderLearning a representation through reconstructionLow reconstruction error does not guarantee useful semantic features
Generative adversarial networkGeneration through competing generator and discriminator objectivesTraining stability and coverage of modes require care

These are families, not exclusive task assignments. Compare against a simpler baseline and choose using data, compute, latency, and evaluation requirements.

Connect the forward pass, loss, and gradient

For a batch X, the network below computes H = tanh(XW₁ + b₁), logits Z = HW₂ + b₂, and probabilities sigmoid(Z). Binary cross-entropy is calculated directly from logits using logaddexp for numerical stability. Backpropagation applies the chain rule; gradient descent then subtracts a learning-rate-scaled gradient.

Weights start randomly so hidden units need not learn identical features. All gradients are calculated from the same forward pass before any parameters are updated. A single hidden layer is enough for this XOR demonstration; adding depth is not required to illustrate the training mechanism.

import numpy as np

X = np.array([[0., 0.], [0., 1.], [1., 0.], [1., 1.]])
y = np.array([[0.], [1.], [1.], [0.]])
rng = np.random.default_rng(7)
W1 = rng.normal(0, .5, (2, 8))
b1 = np.zeros((1, 8))
W2 = rng.normal(0, .5, (8, 1))
b2 = np.zeros((1, 1))

def forward():
    hidden = np.tanh(X @ W1 + b1)
    logits = hidden @ W2 + b2
    probability = np.exp(-np.logaddexp(0, -logits))
    loss = np.mean(np.logaddexp(0, logits) - y * logits)
    return hidden, probability, loss

initial_loss = forward()[2]
for _ in range(3000):
    hidden, probability, loss = forward()
    dlogits = (probability - y) / len(X)
    dW2 = hidden.T @ dlogits
    db2 = dlogits.sum(axis=0, keepdims=True)
    dhidden = (dlogits @ W2.T) * (1 - hidden**2)
    dW1 = X.T @ dhidden
    db1 = dhidden.sum(axis=0, keepdims=True)
    W1 -= .1 * dW1
    b1 -= .1 * db1
    W2 -= .1 * dW2
    b2 -= .1 * db2

_, probability, final_loss = forward()
print("predictions:", (probability >= .5).astype(int).ravel().tolist())
print("loss decreased:", bool(final_loss < initial_loss))
print("final loss below 0.05:", bool(final_loss < .05))

The expected predictions are [0, 1, 1, 0], and both checks print True. The model is evaluated on its training data only. Do not describe that result as test accuracy or use it to claim generalization.

Explain attention with dimensions

For a sequence of n tokens with query and key width d, Q and K have shape n×d. QKᵀ has shape n×n. Scaled dot-product attention computes softmax(QKᵀ/√d)V, normalizing each query’s scores across keys. A causal mask excludes future positions before normalization for autoregressive use. The original Transformer paper defines this mechanism.

The n×n score matrix explains an important full-attention memory cost; it does not describe every optimized implementation or sparse-attention variant. Multi-head attention uses several learned projections and combines their outputs. Positional information is needed when sequence order matters because content-only attention does not itself encode positions.

Distinguish training behavior from gradient tracking

Dropout randomly masks activations during training under its specified scaling rule and is disabled for ordinary evaluation. Batch normalization has different training and evaluation behavior involving batch statistics and stored estimates. A framework’s evaluation mode and its mechanism for disabling gradient tracking solve different problems; do not assume one automatically performs the other.

Keep preprocessing and class mappings consistent between training and inference. Store model and preprocessing versions together. For imbalanced outcomes, examine precision, recall, calibration, and task-specific error costs rather than accuracy alone. Tune thresholds on appropriate validation data, not on the final held-out evaluation set.

Debug in an order that reveals the problem

  1. Check tensor shapes, label encoding, finite values, and the loss/output pairing.
  2. Verify a tiny forward calculation and compare analytical gradients with finite differences on a small fixture.
  3. Try fitting a tiny training subset; failure can reveal an implementation or optimization problem.
  4. Inspect learning rate, gradient magnitudes, and training versus validation curves.
  5. Audit leakage, preprocessing differences, and the split before attributing results to architecture quality.

Overfitting is a gap between fit and generalization, not simply a large model. Regularization, data quality, augmentation, and model capacity can all matter. State the evidence behind a proposed fix and rerun an appropriate held-out evaluation.

Interview Prep

Begin Your SQL, Python, and R Journey

Master 230 interview-style coding questions and build the data skills needed for analyst, scientist, and engineering roles.

Related Articles

All Articles