MLP tensor shapes and PyTorch broadcasting
I was asked this
No reports yet. Sign in to say where you were asked it.
An AI/ML design interview question asked in Nvidia's technical phone screen, last asked Jan 2026. It tests pytorch, tensors and neural networks.
Given a small multi-layer perceptron and a batch of inputs, write down the shape of every tensor in the forward and backward pass, then explain PyTorch's broadcasting rules and the bugs they can hide.
Setup
Unless you agree on a different setup with the interviewer, assume a batch of B = 32 inputs of 784 features, one hidden layer of 256 units with ReLU, and 10 output classes, written with nn.Linear layers.
Part 1: shapes
- Give the shapes of the input, each layer's weight and bias (in PyTorch's
nn.Linearconvention), the hidden activations, the logits, the loss, and the gradient of each parameter. - Write the forward computation of one linear layer as a matrix product, and say how the bias reaches every row of the batch.
For reference: x is (32, 784), the first weight is (256, 784) and its bias (256,), so x @ W1.T + b1 is (32, 256); the second layer's weight is (10, 256) and the logits are (32, 10).
Part 2: broadcasting
- State the broadcasting rules: align shapes from the right; each pair of dimensions must be equal or one of them must be 1 (or missing).
- Explain why
(32, 256) + (256,)works, and what(32, 1) - (32,)produces.
What a strong answer covers
- Every shape correct, including that gradients have the same shapes as their parameters and that the loss is a scalar after mean reduction.
- The
(out_features, in_features)weight layout and whyx @ W.Tis used. - Broadcasting applied to the bias, and the silent bug where predictions of shape
(32, 1)minus targets of shape(32,)give a(32, 32)matrix, so the loss is wrong without any error. - Habits that catch shape bugs: assertions on shapes,
squeeze/unsqueezeused deliberately, and named dimensions oreinsum.
Follow-ups
- Add a second hidden layer of 128 units: what changes?
- The input is now a sequence of shape
(B, T, 784): how doesnn.Linearhandle the extra dimension? - Count the parameters in the model.
- Derive the gradient of the first weight by hand and check its shape.
Where it's asked
- Nvidia · Technical phone screen · Last asked Jan 2026Live technical interview, 45 to 90 minutes; a project discussion, then a small task
What it tests
pytorch · tensors · neural networks
No official answer yet. Draw yours on the right; publish it and it shows under Community.
Nothing published yet. Publish your canvas and it shows here for others to open and remix.
