← Questions
Debug a vision transformer notebook
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Applied Intuition's AI-assisted build round, last asked Oct 2025. It tests debugging, transformers and pytorch.
You get a notebook that trains a small vision transformer (ViT) to classify handwritten digits. It runs without errors, but accuracy is far below what such a model should reach. Several implementation bugs are hidden in it. Find and fix them, and explain how you tracked each one down. Small details matter: look closely at masking, padding and shapes.
The exercise
- The notebook has the usual pieces: image-to-patch embedding, a class token, positional embeddings, transformer blocks with multi-head self-attention, a classification head, and a training and evaluation loop.
- Get the model to train properly, fixing bugs one at a time and showing the effect of each fix.
- For each bug, say what symptom pointed you to it and what check confirmed it.
Constraints
- Fix the existing implementation; do not swap in a library model.
- Training runs are short, so use cheap checks (shapes, a single batch, a tiny subset) before full runs.
Clarifying questions
- What accuracy or loss counts as fixed, and on which split?
- How many bugs should I expect, and are they only in the model or also in data loading and training?
- May I add asserts, tests or plots to the notebook?
- How much compute and time do I have per training run?
What a strong answer covers
- A systematic method: check data and labels first, then overfit a single batch, then check shapes and values layer by layer.
- Typical ViT bugs and how each shows up: patches flattened in the wrong order, the class token or positional embedding missing or added to the wrong axis, an attention mask or padding mask applied with the wrong sign or to the wrong dimension, softmax over the wrong axis, a missing scale of
1/sqrt(d), the head reading a pooled token other than the class token. - Training-loop bugs: missing
zero_grad, the model left in eval mode, wrong loss reduction, normalization stats that don't match the data, a learning rate that diverges. - Small unit tests that would have caught each bug (for example, attention ignores masked positions; permuting patches changes the output once positional embeddings are on).
- Explaining the fix and the evidence, not only the code change.
Follow-ups
- After your fixes the model overfits. What do you change, and in what order?
- How would you check that attention is actually looking at sensible patches?
- How would you scale this to larger images without a quadratic blow-up in the number of patches?
Where it's asked
- Applied Intuition · AI-assisted build · Last asked Oct 2025120 min · Build the designed system, or a small web app, in a prepared environment over two to three hours · Claude Code or a provided Claude model expected
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
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
