← Questions

Debug a shape mismatch in a PyTorch U-Net

0
AppleCodingLast asked Mar 2026

A coding interview question asked in Apple's technical phone screen, last asked Mar 2026. It tests pytorch, debugging and computer vision.

A U-Net for image segmentation is written in PyTorch with valid (unpadded) 3x3 convolutions. The forward pass fails with a tensor shape mismatch. Find the cause, fix it, and make the network work for any reasonable input size.

Example

A two-level version on a 1 x 1 x 64 x 64 input, with each convolution shrinking the height and width by 2:

encoder:   64 -> conv -> 62 -> conv -> 60      skip connection (64 channels, 60 x 60)
pool:      60 -> 30
bottom:    30 -> conv -> 28 -> conv -> 26      (128 channels)
up-conv:   26 -> 52                            (64 channels, 52 x 52)
concat:    skip 60 x 60 with 52 x 52           -> RuntimeError: sizes must match except in dimension 1

Center-cropping the skip by 4 pixels on each side gives 52 x 52. The concatenation then has 64 + 64 = 128 channels, so the next convolution must take 128 input channels. Two more convolutions give a 48 x 48 output.

Constraints

  • Keep the U-Net structure: encoder, bottleneck, decoder, skip connections joined by concatenation.
  • You may change padding, cropping, channel counts and the input size, but say what each change does to the output.

Clarifying questions

  • What is the full error message, and at which layer does it fail?
  • Must the output have the same size as the input?
  • Are the input sizes fixed or arbitrary, and can they be odd?
  • Is it the spatial size that mismatches, the channel count, or both?

What a strong answer covers

  • Reading the error and printing the shape after every layer (hooks or a dry run) to find the first mismatch.
  • The arithmetic: a valid 3x3 convolution removes 2 pixels, pooling halves and floors, a stride-2 transposed convolution doubles; so the skip is larger than the upsampled tensor.
  • The fixes and their trade-offs: crop the skip to match, use padding=1 so the sizes line up, or pad the upsampled tensor.
  • Channel bookkeeping: after concatenation the decoder convolution's in_channels is the sum of both inputs.
  • Odd sizes: pooling floors, so cropping becomes uneven (here a 65 x 65 input leaves 4.5 pixels per side). Choose input sizes that divide cleanly, or pad the input and crop the output.
  • A test that runs the model on several input sizes and checks the output shape.

Follow-ups

  • Make the output the same size as the input with valid convolutions.
  • Replace the transposed convolution with bilinear upsampling and a 1x1 convolution: what changes?
  • Add a third level and work out which input sizes are valid.

Where it's asked

  • Apple · Technical phone screen · Last asked Mar 2026

What it tests

pytorch · debugging · computer vision

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding