This section is about the assumptions a model makes before it sees any data, and why a Vision Transformer, which assumes almost nothing about images, can still beat a network built around them. A convolutional network, or CNN, assumes two things about pictures (§0.8). Nearby pixels belong together, so each layer looks only at a small neighborhood. And a pattern means the same thing wherever it appears, so the same small filter is applied at every position. A Vision Transformer, or ViT, cuts the image into square patches, turns each patch into a token, and lets every token attend to every other token from the first layer on (§27.2). It drops the first assumption entirely. It keeps only a weak form of the second, since every patch passes through the same weights but also carries a learned position, so nothing forces a pattern to mean the same thing in every place.
Throwing that knowledge away looks like a mistake, and with a million training images it is. The score here is top-1 accuracy, the share of test images whose highest-scoring class is the right one. A name like ViT-B/16 gives the model's size and patch side, here the Base size (about 86 million parameters) with 16 by 16 pixel patches (§27.1). Trained on ImageNet-1k alone (1.28 million labeled photos in 1,000 classes) and fine-tuned at 384 by 384 pixels, ViT-B/16 reaches 77.9%. ViT-L/16, the Large size with about 3.5 times as many parameters, does worse, 76.5% (Dosovitskiy et al. 2021, Table 5). Two strong CNNs of the time did far better on the same data, 82.9% for RegNetY-16GF at 224 pixels and 84.3% for EfficientNet-B7 at 600 pixels (Touvron et al. 2021, Table 5).
Now pretrain the same ViTs on JFT-300M, a Google dataset of about 300 million labeled images, and the order flips. With the ViT paper's final fine-tuning recipe (its Table 2), ViT-L/16 reaches 87.76%, and ViT-H/14, the Huge size with 14-pixel patches, reaches 88.55%. Both beat BiT-L at 87.54%, a large ResNet pretrained on the same data (Kolesnikov et al. 2020). Removing knowledge hurt with a million images and helped with three hundred million.
This section explains that reversal exactly, and interviewers ask about it in many forms. What does a CNN know that a ViT does not? When is that knowledge worth having? Is a ViT really more robust? We go in five steps. We define an inductive bias and show, in a toy with exact numbers, why a true one is worth data. We state exactly which shifts of an image a convolution and a ViT respect, and prove it. We measure what the missing assumption costs at real scale. We look at what trained ViTs learn in its place, and prove that attention can compute any convolution. And we end with ways to put the assumption back and with the robustness evidence, read carefully.
An inductive bias is an assumption that picks the answer
A learner sees finitely many examples and must still answer for inputs it has never seen. Many different functions fit the same training set, so something besides the data has to pick one. Take three points on a line, (0,0), (1,1), and (2,2). A learner that fits only straight lines returns y=x. A learner that fits any polynomial may return y=x+5x(x−1)(x−2), which passes through the same three points but predicts 33 at x=3 instead of 3. The data did not make that choice. The assumption "the answer is a line" did.
In these terms, a CNN's locality and weight sharing are hard biases. A ViT has far fewer hard biases about space, and its training recipe adds soft ones back (§27.4).
Why a true assumption is worth data
A larger class contains every function a smaller one does. So how can shrinking it help? The answer is that the learner never gets the best function in its class. It gets an estimate fit to finite, noisy data. The error of that estimate has two parts, and shrinking the class moves them in opposite directions.
In plain words, the approximation error is what the class gets wrong even with infinite data. The estimation error is what we lose by learning from the data we have. Shrinking the class to H′⊂H can only raise the first term, because the best member of a smaller set is no better. It usually lowers the second, because fewer free parameters are fit to the same noise. This is the bias-variance trade-off of §0.7, stated for whole classes of models instead of one predictor.
Two cases follow. Call a restriction true when the best function really lies in H′. A true restriction leaves the first term where it was and lowers the second, so it is pure gain. A restriction that is only approximately true wins while estimation error dominates, which is when data is scarce. It loses once data is so plentiful that the approximation error is all that remains. That is the argument of this whole section in one identity. The rest measures the two terms for images.
A toy where you can compute what an assumption is worth
Here is the trade-off with numbers you can check. Each input is a row of 32 random numbers. The target at each position is a local slope, the left neighbor minus the right neighbor, times a gain that drifts slowly along the row, plus noise. One model assumes the rule is identical at every position. The other assumes nothing. The first is slightly wrong, and the second has much more to learn.
Precisely, the input is x∈Rn with n=32 entries drawn independently from N(0,1). Indexing is circular, so position −1 means position n−1 and the row wraps around. The target at position t is
with gain strength β=0.3 and noise level σ=0.5. The difference xt−1−xt+1 is a convolution with the kernel (1,0,−1), the slope detector of §0.8. The gain 1+βgt is 1.3 at position 0, 1.0 at position 8, and 0.7 at position 16, so the true rule is close to the same everywhere but not exactly. We fit two linear models by least squares, the weights that minimize the squared error on the training pairs, from N training rows.
The convolution has k=3 weights shared by every position, y^t=w0xt−1+w1xt+w2xt+1. It can represent only maps that apply one three-tap kernel everywhere.
The full map is an arbitrary linear map y^=xW with W∈Rn×n, 1,024 weights. Its class contains the true map.
We can compute both error terms for both models.
Approximation error. The full map has none, since the true map is in its class. For the convolution, the best kernel is the true one, (1,0,−1), because the drifting part averages out (∑tgt=0). What it misses at position t is βgt(xt−1−xt+1), whose variance is 2β2gt2, since xt−1 and xt+1 are independent with variance 1. Averaged over positions, with n1∑tgt2=21, that is β2=0.09.
Estimation error. This term needs one standard fact about least squares.
In plain words, every coefficient you fit costs about σ2/N of test error. More coefficients cost more, and more equations cost less. The two models differ in both counts.
The full map solves a separate regression for each of the 32 outputs, each with p=n=32 coefficients and N equations. Its excess per output is σ2n/(N−n−1).
The convolution's three weights are shared by all 32 positions, so each training row gives it n equations instead of one. Its excess is about σeff2k/(Nn), where σeff2=σ2+β2, because the part it cannot represent acts like extra noise.
∂ Why least squares has excess error σ² p / (N − p − 1)
Write the regression as y=Xw+ε with X∈RN×p of independent N(0,1) entries and ε∼N(0,σ2I) independent of X. Least squares gives w^=(X⊤X)−1X⊤y=w+(X⊤X)−1X⊤ε.
A fresh test input x∼N(0,I) with fresh noise ε has squared error (x⊤(w−w^)+ε)2. Its expectation over x and ε is ∥w^−w∥2+σ2, since E[xx⊤]=I. The σ2 is the noise floor, and the excess is ∥w^−w∥2. Take the expectation over the training noise first, with M=(X⊤X)−1X⊤,
The matrix S=X⊤X has a Wishart distribution with N degrees of freedom and scale Ip. Its inverse has an inverted Wishart distribution, whose mean is E[S−1]=Ip/(N−p−1) for N>p+1 (Anderson 2003, in its treatment of the inverted Wishart distribution). For identity scale the proof is short enough to give in full.
The mean is a multiple of the identity. For any orthogonal Q∈Rp×p, the matrix XQ also has independent N(0,1) entries (each row x⊤Q has covariance Q⊤IQ=I). So Q⊤SQ has the same distribution as S, and Q⊤E[S−1]Q=E[S−1]. A symmetric matrix that commutes with every rotation and reflection is a multiple of Ip, so E[S−1]=γIp with γ=E[(S−1)11].
The multiple. Split X=[x1X2] into its first column and the other p−1. The Schur complement formula for the top-left entry of an inverse gives
where P⊥=IN−X2(X2⊤X2)−1X2⊤ is the orthogonal projection onto the complement of the column span of X2. Given X2, that complement has dimension N−p+1, and x1∼N(0,IN) is independent of it. So the denominator is a chi-squared variable with m=N−p+1 degrees of freedom. For V∼χm2 with m>2, E[1/V]=1/(m−2). (Integrate v−1 against the density vm/2−1e−v/2/(2m/2Γ(m/2)), which gives Γ(m/2−1)/(2Γ(m/2))=1/(m−2).) So γ=1/(N−p−1), finite exactly when N>p+1.
The trace of E[S−1] is therefore p/(N−p−1), and the expected excess is σ2p/(N−p−1). For N≫p it is about σ2p/N. That also follows directly from X⊤X≈NI, since each entry of X⊤X/N is an average of N products with mean 1[i=j].
For the convolution, the design rows are the windows (xt−1,xt,xt+1) of every position of every training row, Nn rows in all. They are not independent, because neighboring windows overlap. But for white inputs their Gram matrix still concentrates at NnI3, because E[xt+axt+b]=1[a=b]. The large-N step then gives about σeff2⋅3/(Nn). The cell below checks both formulas against simulation.
Add the noise floor σ2, and the two models' test errors per output are about
conv≈σ2+β2+Nn(σ2+β2)k,full≈σ2+N−n−1σ2n.
In plain words, the convolution reaches its floor almost at once and stays there, a little above the noise. The full map starts much worse and keeps improving toward the noise itself. They cross where σ2n/(N−n−1)=β2, that is at N⋆=n+1+σ2n/β2=33+0.25⋅32/0.09≈122 training rows.
Python · cell 1
The same formulas say how much data shared weights are worth when their assumption is exactly right.
A ViT sits between these two extremes. Its patch embedding is shared across all patch positions, and its attention and MLP weights are shared across all tokens, so it is nothing like a dense layer on pixels. What it lacks is one specific assumption, that a token's update depends mostly on its spatial neighbors. The rest of the section measures what that one assumption is worth. In an interview, the split into approximation and estimation error answers "why would a model with fewer assumptions ever win?" It wins once data shrinks its estimation error below the approximation error that its rival's assumptions cost.
Which shifts a convolution and a ViT respect
A model can respond to a moved image in two ways. It can move its output the same way, which is called being equivariant. A map of where the edges are should move when the edges move. Or it can leave its output unchanged, which is called being invariant. The label "cat" should not change when the cat moves. §0.8 proved that a stride-1 convolution is equivariant, and that averaging a feature map over all positions turns that into an invariant summary. Here we need the exact version, because it says precisely what a ViT gives up.
Start with one short signal and three layers you can check by hand. A stride-2 layer computes the same outputs as a stride-1 layer and keeps every second one. A patch embedding cuts the signal into pieces that do not overlap and maps each piece to one number.
Python · cell 2
That example contains the whole story. A stride-1 convolution respects every shift. A stride-s layer respects only shifts by multiples of s. A patch embedding respects only shifts by whole patches. Now the general statements. We write an image as X∈RH×W×C, with height H, width W, and C channels, channels last in the math as in §27.1. Code uses PyTorch's (C,H,W) and says so. Rows are indexed by r and columns by c.
Circular shifts are a mathematical convenience. They form a group, since shifting by δ and then by δ′ is shifting by δ+δ′, so every statement below is exact. Real images have borders, and zero padding breaks equivariance in a band of width ⌊k/2⌋ along them. Everything we prove holds everywhere with circular padding, and away from the borders with zero padding.
We use the convolution of §0.8 in its cross-correlation form, the one deep learning libraries compute, with circular padding and a centered k×k kernel (k odd, half-width a=⌊k/2⌋). The weights are Wconv∈RCout×C×k×k in PyTorch's layout, and the bias is b∈RCout. Write c′ for the input channel, to keep it apart from the column c, and o for the output channel, both counted from 0. Then
In plain words, output channel o at pixel (r,c) is a weighted sum of the k×k neighborhood around that pixel, with the same weights at every pixel. The output channel comes last here, so that one shift operator Tδ acts on inputs and outputs alike. (§27.1 put it first, as PyTorch does. The entries are the same numbers.) A convolution with stride s, for s dividing H and W, is the stride-1 output kept on every s-th row and column, Convs(X)[r,c,:]=Conv(X)[sr,sc,:] for 0≤r<H/s and 0≤c<W/s.
In plain words, a stride-s layer is equivariant only to shifts that move its sampling grid onto itself. Stack such layers and the periods multiply, just as strides multiply in the receptive-field arithmetic of §0.8. ResNet-50's final feature map has a total stride of 32. So even with circular padding, the network is guaranteed to be equivariant only to shifts by multiples of 32 pixels, and its globally pooled output is guaranteed to be invariant only to those.
∂ Proof of the stride theorem
Part (a). Substitute the shifted input into the definition. Entry (r,c,o) of Conv(TδX) is
because the shift subtracts δ from the index before the kernel offset is added. (Both are taken mod H and W, so the order of the two additions does not matter.) This is the definition of Conv(X) at the point ((r−δr)modH,(c−δc)modW,o), which is entry (r,c,o) of TδConv(X). The only fact used is that the weights do not depend on (r,c). ∎
using (a) in the middle step. Indices are mod H and W, and mod H/s and W/s on the right, which agree because s divides both. The right side is TμConvs(X) at (r,c). If instead δr is not a multiple of s, then Convs(TδX) samples the stride-1 output on a grid offset by δrmods rows from the grid Convs(X) keeps. The two subsampled maps are made of different entries of Conv(X), so in general no shift of one equals the other. The stride-2 case of the worked example is a concrete instance. ∎
This is not a technicality. Shifting an image by a single pixel often changes a trained CNN's prediction (Azulay and Weiss 2019). The cause is aliasing, which a four-number example shows. Take the row (1,−1,1,−1) and keep every second entry. You get (1,1). Shift the row by one pixel first and you get (−1,−1). Detail finer than the coarse grid can hold has turned into a false constant whose sign depends on where the grid falls. The sampling theorem says to blur before subsampling, and standard CNNs do not.
Zhang (2019) traced the lost equivariance layer by layer. It holds with period 1 before the first downsampling, and the period doubles at each one. Adding a low-pass filter before every subsampling restored much of it. ResNet-50's predictions under shifts became more consistent by up to 2.1 points, and its ImageNet accuracy rose by 0.7 to 0.9 points. In practice this is why random-crop augmentation matters even for CNNs. In an interview, "a CNN is translation invariant" is a wrong answer.
The symmetry a ViT keeps
A patch embedding is a convolution whose kernel size and stride both equal the patch size P (§27.1). So part (b) of the theorem says it respects only shifts by whole patches. Such a shift moves every patch to another slot of the patch grid. The ViT receives the same tokens in a different order, and a Transformer without position information does not care about order.
Here is that in the smallest case. Take a 2 by 2 grid of patches, numbered 0 and 1 in the top row and 2 and 3 below, with tokens (t0,t1,t2,t3). Shift the image right by one whole patch, circularly. Patch 0's content moves to slot 1, patch 1's wraps around to slot 0, and the same happens in the bottom row. The sequence becomes (t1,t0,t3,t2), the same four tokens reordered.
In general, the patch grid has Hp=H/P rows, Wp=W/P columns, and Np=HpWp patches. Stack the flattened patches in raster order (patch (r,c) in row rWp+c) as Xp∈RNp×P2C. The patch tokens are then PE(X)=XpWE+1bE⊤∈RNp×d, with weights WE∈RP2C×d and bias bE∈Rd, as in §27.1. A translation by δ∈PZ2 moves whole patches, so it acts on the token matrix as a permutation,
PE(TδX)=ΠδPE(X),
where Πδ is the Np×Np permutation matrix that sends grid cell (r,c) to ((r+δr/P)modHp,(c+δc/P)modWp).
Two facts from §4.6 finish the argument. First, a Transformer encoder f without positions is permutation equivariant, f(ΠX)=Πf(X) for every permutation matrix Π. Reordering the tokens reorders the outputs and changes nothing else. Second, a fixed matrix B added to every head's attention scores keeps that property for every permutation with ΠBΠ⊤=B. The scores still transform as ΠSΠ⊤, which is all the proof in §4.6 uses, and it is the same reason a mask preserves the symmetries that leave it unchanged.
∂ Proof of the ViT symmetry theorem
(1) The class token is the same vector for both images, so the input for TδX is [xcls;ΠδPE(X)]=Π~δ[xcls;PE(X)]. Permutation equivariance of the encoder (§4.6) gives the claim. Row 0 of Π~δF is row 0 of F, and a mean over rows is unchanged by permuting them.
(2) Let B hold b(gm−gqmod(Hp,Wp)) for patch pairs q,m with grid coordinates g, and a constant for pairs involving the class token. A grid shift adds the same vector to every g, which leaves every circular offset unchanged. So Π~BΠ~⊤=B, and the §4.6 argument goes through layer by layer.
(3) Write F0 for the encoder without positions and Z=[xcls;PE(X)] for the input before positions are added. The shifted image gives Π~Z+Ppos=Π~(Z+Π~⊤Ppos), because Π~Π~⊤=I. Item (1) moves Π~ outside, F0(Π~(Z+Π~⊤Ppos))=Π~F0(Z+Π~⊤Ppos). This equals Π~F0(Z+Ppos) for every image when Π~⊤Ppos=Ppos. When it does not hold, the encoder receives a different input, so any equality would be a coincidence of particular weights, not a property of the architecture. Shifts by one patch down and one patch right generate every grid shift, so the condition for all of them is that the patch rows of Ppos are all equal. ∎
In plain words, a ViT's symmetry lives on the patch grid, never on the pixel grid. With relative positions, or none, it is exactly equivariant to shifts by whole patches. With learned absolute positions, the ViT paper's default, it is not even that. The content moves to new slots while the position vectors stay with the slots, so the moved content meets different positions. Any approximate equivariance the model shows was learned from data. Exact equivariance would force all patch rows of Ppos to be equal, which is the same as having no positions.
This is the precise content of the remark in the ViT paper that only its MLPs are local and translation equivariant. The MLPs act on each token separately, so they commute with every reordering. The attention layers are global, and their only spatial structure comes from positions the model must learn.
The cell measures all of this on a random image, for horizontal shifts of 0 to 8 pixels with P=8. When the stride s divides the shift, the error is max∣f(TδX)−Tδ/sf(X)∣/max∣f(X)∣. Otherwise it is the smallest such error over every possible output shift, so a nonzero value means no output shift works.
Python · cell 3
The stride-1 convolution has zero error at every shift. The stride-2 convolution is exact at even shifts, and at odd ones it is wrong by more than the size of its own output (an error of 1.12). The patch embedding is exact only at 0 and 8 pixels. Adding positions breaks even the shift by a whole patch, with an error of 0.85 at 8 pixels. The widget shows the same pattern on a picture. Pick a model and a patch size, then move the shift slider.
Which shifts does each model respect?
Shift the image right. Equivariance means shifting before the model or after it gives the same map.
Loading interactive figure…
Core
Measure equivariance, and find the shifts a model respects
Write equivariance_error(f, x, shift, stride=1). The input x has shape (C, H, W), and f maps it to an output of shape (D, H // stride, W // stride). With shift = (dr, dc), shift the input circularly with np.roll(x, (dr, dc), axis=(1, 2)). If both dr and dc are multiples of stride, shift the output by (dr // stride, dc // stride) and return
max∣f(x)∣max∣f(Tδx)−Tδ/sf(x)∣.
Otherwise no output shift corresponds to the input shift. In that case return the smallest value of the same ratio over every output shift (a, b) with 0 <= a < H // stride and 0 <= b < W // stride.
Then write which_shifts_commute(f, x, stride=1, tol=1e-8). It returns the sorted list of row shifts dr in range(H), with dc = 0, whose error is below tol.
Tests, verified solutions, and AI help, open once you add your API key.
The data-scale crossover, measured
The toy predicted a crossover, and the symmetry theorem says exactly what a ViT gives up. The ViT paper measured the crossover directly, in two experiments.
In the first, the same ViTs were pretrained on three datasets of growing size and then fine-tuned on ImageNet at 384 by 384. The middle dataset is ImageNet-21k, about 14 million images in about 21,800 classes. Their Table 5 gives the top-1 accuracies.
pretraining data
images
ViT-B/16
ViT-L/16
ViT-H/14
ImageNet-1k
1.28M
77.91
76.53
not run
ImageNet-21k
14M
83.97
85.15
85.13
JFT-300M
303M
84.15
87.12
88.04
Read the columns from top to bottom. On ImageNet alone the bigger model is worse, which is the signature of estimation error. With 14 million images the sizes are close. Only with 300 million does size pay off. The convolutional BiT models that win on the small datasets are overtaken there (their Figure 3).
The second experiment separates the model from the training recipe. They pretrained on random subsets of JFT of 9M, 30M, 90M, and 300M images, with the same hyperparameters for every size. The small subsets got no extra regularization, only early stopping. Then they measured 5-shot linear accuracy on ImageNet. The network is frozen, a linear classifier is fit on its features from five labeled images per class, and that classifier is tested. Nothing is fine-tuned, so the score measures the pretrained features alone.
The same paper compared three families at equal compute, with data no longer the bottleneck because all were pretrained on JFT-300M. The third family is a hybrid, the first stages of a ResNet feeding their feature map, as tokens, to a ViT. Their Table 6 lists ImageNet accuracy against total pretraining compute in exaFLOPs (1018 operations). The cell plots it. For each ResNet it then asks how much compute the ViTs need to reach the same accuracy. It interpolates linearly in log compute between the two nearest ViTs on the frontier, the ViTs that beat every cheaper ViT.
Python · cell 4
Two later results keep this from becoming a slogan. Purely convolutional networks pretrained on four billion images match ViTs at comparable compute, and a hybrid of convolution and attention matches a JFT-pretrained ViT-H/14 with 23 times less data. The box gives the numbers.
So the honest summary has two parts. A good prior is worth a large amount of data. At very large scale, the choice between a well-tuned CNN and a ViT matters much less than compute. ViTs won the frontier mostly for other reasons. They scale smoothly on accelerators. The same tokens feed language models and multimodal systems (CLIP's image tower is a ViT). And masked and self-supervised pretraining fit them naturally, which is another way to buy what labels cannot (self-supervised vision). An interviewer who asks "when does a ViT beat a ResNet?" wants both parts, the crossover with its numbers and the caveat that well-tuned CNNs at scale close the gap.
What trained ViTs learn in place of locality
A CNN's early layers look at small neighborhoods because they have no choice. A ViT's attention can reach anywhere. If a ViT must learn locality from data, does it? The ViT paper answered with one number per attention head, how far away, in pixels, the head's attention lands on average.
To define it we need the grid position of each token. The class token, and any register tokens (extra learned tokens that give the model scratch space, §27.4), come first and have no position. We call them prefix tokens. The patches follow in raster order.
In plain words, for every patch, average how far away its attention lands, weighted by the attention weights. Then average over patches and images. It is the attention analog of a receptive field size, with one difference. A receptive field is where a unit can look. Attention distance summarizes where the weights do point.
Python · cell 5
Measured on trained models, the statistic tells a consistent story.
Two more findings sharpen the picture. In the hybrid R50+ViT-L/16, where a ResNet has already done the local processing, the early local heads are much less pronounced. That suggests those heads do the job early convolutions do. And ViT-B/32 still learns local heads when trained on ImageNet alone. It also depends less on large-scale pretraining. In the ViT paper's Table 5, JFT-300M pretraining adds 7.4 points to ViT-B/32 on ImageNet and 10.6 points to ViT-L/16. Raghu et al. also found that lower ViT layers are most similar to lower ResNet layers when only the most local heads are kept. Put together, locality is a useful thing to compute early. A ViT learns it when it has enough data, and failing to learn it goes with failing to train well.
Interview
Mean attention distance from attention maps
Write mean_attention_distance(A, H_p, W_p, P, n_prefix). The input A holds the attention weights of one layer for one image, with shape (h, T, T), queries on rows, and T=npre+HpWp. The first n_prefix tokens are prefix tokens, and the rest are patches in raster order. Return an array of shape (h,) with the mean attention distance of each head in pixels, as defined above. Use patch queries and patch keys only, and renormalize each row.
Tests, verified solutions, and AI help, open once you add your API key.
One caution before you read too much into such plots. Attention distance summarizes weights, not influence. A head can put large weight on a token whose value vector is small. And the residual stream of §5.1 carries every token's own information around each attention layer. Influence measures can tell a different story. The most direct ones are gradients of an output with respect to the input patches, like the effective receptive field of §0.8. Attention rollout, next, is a cheap compromise that stays within the attention maps. Use MAD for what it is, a cheap and comparable summary of where single heads look.
Attention rollout, following weights through the layers
A single layer's attention map says where a token looked in the layer below. But the token it looked at had already mixed in other tokens in earlier layers. And every token also passes its own state up the residual path. Abnar and Zuidema (2020) proposed following both effects through the whole network.
In plain words, treat each layer as moving information along the edges of a graph, half through attention and half straight up the residual path. Multiply the weights along every path from an input patch to the class token at the top, and add over paths. The order of the product follows from writing tokens as rows. If layer ℓ mixes its input rows as X(ℓ)≈A^(ℓ)X(ℓ−1), then X(L)≈A^(L)⋯A^(1)X(0), with the last layer on the left.
The 21 comes from the residual connection. A layer computes X+AˉX=(Aˉ+I)X, and the paper divides by 2 so that the rows sum to 1, which gives the residual path and attention equal shares. Attention rows are nonnegative and sum to 1, so 21Aˉ+21I already has rows that sum to 1, and so does the product. The renormalization matters only for maps whose rows do not, for example after a visualization tool zeroes the smallest weights. The class token's patch entries then sum to 1 minus the share that ends on prefix tokens.
Python · cell 6
Rollout is a heuristic, not a measurement of influence. It ignores the value vectors and the MLPs, averages the heads, and fixes the residual's share at one half. So, like a raw map, it cannot see that a heavily weighted token may carry little. Unlike a single map, it accounts for depth and for the residual path. Abnar and Zuidema tested it on a small six-layer text classifier. From the second layer up, rollout agreed better than raw attention with two other measures of token importance, blanking out input tokens and input gradients. In the first layer, raw attention agreed better. It is popular because it costs L matrix products and needs no backward pass. Methods that also propagate gradients were built to address what it leaves out (Chefer, Gur and Wolf 2021).
Interview
Attention rollout for a ViT
Write attention_rollout(A_layers, n_prefix, H_p, W_p). The input A_layers has shape (L, h, T, T). It holds the attention weights of every layer (layer 1 first) and every head for one image, with queries on rows and T=npre+HpWp. The class token is at index 0, the next n_prefix - 1 tokens are other prefix tokens (registers), and the patches follow in raster order. Some weights were pruned, so the rows may not sum exactly to 1.
For each layer, average over heads, form 21Aˉ+21I, and divide each row by its sum. Multiply the layers with the last on the left. Return the class token's row over the patch tokens only, reshaped to (H_p, W_p), without renormalizing it.
Tests, verified solutions, and AI help, open once you add your API key.
Attention can compute any convolution
Early ViT heads that attend to a few nearby patches look a lot like the taps of a convolution. How far does the resemblance go? A ViT lacks a convolution's built-in symmetry, but is it unable to compute what a convolution computes? No. Attention can compute any convolution exactly, in a limit, and the proof is a construction (Cordonnier, Loukas and Jaggi 2020).
The idea fits in three sentences. A 3×3 convolution adds up nine terms, each one neighbor of the pixel times one weight matrix, at a fixed offset. An attention head whose scores depend only on the offset from query to key can put almost all its weight on one neighbor at a fixed offset. Nine such heads, one per offset, each with its value and output weights set to that offset's kernel tap, add up to the convolution.
Here is one head in numbers, in 1-D. It should read the neighbor one step to the right, offset +1. Score each key by −α(δ−1)2, where δ is the key's offset from the query and α is a sharpness. With α=2, the target key gets score 0 and its two neighbors get −2, so the softmax puts 1/(1+2e−2)≈0.79 on the target and 0.11 on each neighbor. With α=10, it puts 0.99991 on the target. As α grows, the head becomes a lookup of exactly one neighbor.
Now the general case. First, write multi-head attention so the heads are visible. Take h heads with per-head value projections WV(j)∈RC×dv, and the output projection WO∈Rhdv×dout split into row blocks WO(j)∈Rdv×dout. Weights are stored (input, output) and applied to row-vector tokens, as in §4.5. The output for query token q is
In plain words, concatenating the heads and multiplying by WO is the same as multiplying each head by its own block and adding. So each head contributes its attention-weighted inputs times one matrix W(j), of rank at most dv. Now write the convolution the same way. Let WΔ∈RC×Cout be the kernel tap at offset Δ=(u−a,v−a), with entries WΔ[c′,o]=Wconv[o,c′,u,v]. Then
Conv(X)q=b+Δ∈{−a,…,a}2∑xq+ΔWΔ.
The two formulas have the same shape. A convolution is k2 "heads" that each read one fixed neighbor. So the only question is whether an attention head can read exactly one neighbor at a fixed offset, for every query.
∂ Proof of the theorem, by construction
Index tokens by grid position, so token q sits at gq∈Z2. Write δ=gm−gq for the offset from query to key (circular, wrapped to the representative nearest zero). Let k=h be odd, a=⌊k/2⌋, and assign the heads one-to-one to the offsets, head j to Δj∈{−a,…,a}2.
Step 1, hard attention to fixed offsets gives the convolution (their Lemma 1). Suppose head j puts weight exactly 1 on the key with δ=Δj and 0 elsewhere, for every query. Then the multi-head formula becomes bO+∑jxq+ΔjW(j). Choose W(j)=WΔj and bO=b, and the layer is the convolution. That choice must be realizable as a product WV(j)WO(j). If Cout≤min(dv,dout), take WV(j)=[WΔj0]∈RC×dv and WO(j) the dv×dout matrix with ICout in its top-left corner and zeros elsewhere. Their product is WΔj in the first Cout output columns. With more output channels than dv, the product has rank at most dv and a general tap cannot be matched, which is where min(dv,dout) comes from.
Step 2, a three-number relative encoding gives that hard attention in the limit (their Lemma 2). Set WQ=WK=0, so the content part of every score vanishes. Let the score be a relative-position term only, sqm(j)=v(j)⊤rδ, with the fixed encoding rδ=(∥δ∥2,δr,δc)∈R3 and the learned per-head vector v(j)=−α(1,−2Δj,r,−2Δj,c). Expanding,
sqm(j)=−α(∥δ∥2−2⟨δ,Δj⟩)=−α∥δ−Δj∥2+α∥Δj∥2.
The last term is the same for every key, and a softmax is unchanged by adding a constant to every score (§0.4). So the weights are Aqm(j)=e−α∥δ−Δj∥2/Z. The target key has exponent 0. Every other key has δ−Δj a distinct nonzero integer vector, so its exponent is at most −α, and
where the double sum factors because e−α(u12+u22)=e−αu12e−αu22. The four nearest neighbors dominate, so ρ(α)≈4e−α, which tends to 0 as α→∞.
Step 3, the error at finite sharpness. Head j's output is ∑mAqm(j)ym(j) with ym(j)=xmW(j), while the convolution needs ytarget(j). Because the weights sum to 1,
Summing over the h heads gives the bound in the theorem. ∎
Two limits keep the theorem honest. First, the sizes are real. ViT-B has 12 heads of width 64, so the construction fits at most a 3 by 3 convolution (nine heads) with 64 output channels into one of its layers, not a full 768-channel one. Second, expressing is not learning. The theorem says the class of attention layers contains the class of convolutions, which is exactly the sense in which a ViT has the weaker inductive bias. Whether gradient descent finds the convolution is an empirical question. Cordonnier et al. trained a network of six attention-only layers with nine heads each on CIFAR-10 images, after a 2 by 2 space-to-channel downsampling. Some heads in the first two layers did learn to attend to single positions at fixed offsets from the query, both with the quadratic encoding and with a freely learned one.
How fast does the construction converge? The one-dimensional version needs one head per tap and a two-number encoding (δ2,δ). The cell builds it for a three-tap kernel on a circular signal and compares it with the convolution as α grows. Next to the error it prints the bound's ρ(α), which in 1-D is 2∑z≥1e−αz2.
Python · cell 7
At α=1 the heads are still blurry, and the error is as large as the outputs themselves. By α=10 it has fallen to the 10−4 range, and by α=20 it is near 10−8. It falls like ρ(α)≈2e−α, as the bound predicts. In float32, whose rounding unit is 2−24≈6×10−8, a sharpness of about 18 already makes the off-target weights invisible, so a moderate, finite α is enough in practice. The widget shows the two-dimensional version, nine heads around one pixel. Press Sharpen, or raise the sharpness by hand, and watch each head's weight collapse onto one neighbor.
How do nine heads add up to a convolution?
Each attention head scores keys by offset alone and is aimed at one neighbor. Sharpen the scores and compare the outputs.
Loading interactive figure…
The practical reading is short. A ViT layer is strictly more expressive than a convolution of the same width, so whatever a CNN layer computes, a ViT layer with relative positions can compute too. What it lacks is the guarantee that training will find that solution from little data. That distinction, expressive power against inductive bias, is the heart of the interview question "can self-attention represent a convolution?"
Research
A 3 by 3 convolution from nine attention heads
Write conv_via_attention(X, W_conv, alpha). The input X is a feature map of shape (H, W, C_in) on a circular grid, and W_conv holds convolution weights of shape (C_out, C_in, 3, 3) in PyTorch's layout. The target is the circular cross-correlation Y[r,c,o]=∑c′,u,vWconv[o,c′,u,v]X[(r+u−1)modH,(c+v−1)modW,c′].
Build nine heads, one for each tap (u,v), with offset Δ=(u−1,v−1). Head (u,v) scores key m for query q by −α∥δqm−Δ∥2. Here δqm is the grid offset from query to key, wrapped into [−H/2,H/2) for rows and [−W/2,W/2) for columns. Take a softmax over keys. Use the values XW(u,v) with W(u,v)=W_conv[:, :, u, v].T, and sum the nine head outputs. Return shape (H, W, C_out). You may loop over the nine heads but not over tokens. The tests compare with the convolution at α=50 (error below 10−6) and check that α=0.1 is far from it.
import numpy as np
def conv_via_attention(X, W_conv, alpha):
raise NotImplementedError
Tests, verified solutions, and AI help, open once you add your API key.
Putting the assumption back, and what "no inductive bias" gets wrong
A ViT is often described as having "no inductive bias". That is false, and the correction is a useful interview answer. Cutting the image into patches is a strong locality assumption at the scale of P pixels. The patch embedding is shared across positions, so it is equivariant to whole-patch shifts. The MLPs treat every token identically. When the resolution changes, the position table is resized by 2-D interpolation, an explicit statement that positions live on a grid (§27.1). And the residual architecture, the optimizer, and the initialization all prefer some functions over others. What a ViT lacks is the specific prior that a token's update depends mostly on its spatial neighbors, and exact translation symmetry at pixel resolution.
When data is limited, there are four ways to buy the missing prior back, and the rest of this chapter uses all of them.
Hard locality. Mask attention so each patch sees only patches within a small grid distance, or attend inside local windows. This brings back the convolution's restricted class, and it cuts the cost from quadratic to linear in the number of tokens. Swin's shifted windows and ViTDet's windowed blocks, in §27.5, are this idea. The 1-D version for language is the sliding window of §12.6.
Soft locality. Initialize or bias attention toward local offsets and let training undo it if the data disagree. Relative position biases also restore the whole-patch symmetry of the theorem above, exactly for circular offsets and away from the border otherwise.
Convolutional stems and hybrids. Let convolutions process the first few stages and attention the rest, as in the ViT paper's hybrids and in CoAtNet.
Substitute data for the prior. Use heavy augmentation, regularization, and distillation from a convolutional teacher, which is how DeiT trained a ViT on ImageNet-1k alone (§27.4). Or pretrain on unlabeled images with a self-supervised objective.
The cleanest controlled measurement of a soft prior at small scale comes from ConViT (d'Ascoli et al. 2021). Its attention layers start as convolutions, with each head initialized to attend around one offset, as in the theorem. A learned gate per head then decides how much of this positional attention to keep and how much content-based attention to mix in. The experiment compares ConViT-S with DeiT-S, a ViT of the Small size (22 million parameters) trained with the DeiT recipe. Both are trained with that recipe on class-balanced subsets of ImageNet-1k, with epochs scaled so the number of images seen stays constant.
The table puts the options side by side.
design
prior built in
data it needs
where it wins
examples
CNN
locality and translation equivariance in every layer, a pyramid of strides
least
small data, high-resolution dense prediction, edge devices
ResNet, ConvNeXt, NFNet
plain ViT, absolute positions
patches and per-token weight sharing only
most, or a recipe or pretraining that stands in for it
large-scale pretraining, global context from layer 1, shared tokens with language
ViT, CLIP and SigLIP towers, DINOv2
ViT with relative positions or RoPE
scores depend on patch offsets, so whole-patch shifts are respected away from the border
slightly less
transfer across resolutions and aspect ratios
DINOv3 (RoPE), Swin within each window
soft local initialization
locality at the start, removable
much less at small scale
low-data training from scratch
ConViT
local masks or windows
hard locality per layer, linear cost
less
high resolution, dense tasks
Swin, ViTDet, SAM's encoder
hybrid
convolution early, attention late
moderate
best accuracy per image at mid scale
R50+ViT, CoAtNet
The first route is a mask. Building it correctly, with the prefix tokens left free, is a small but common interview task.
Core
A local attention mask on the patch grid
Write grid_local_mask(H_p, W_p, radius, n_prefix=1). It returns a boolean array of shape (T, T) with T=npre+HpWp, where True means "may attend", the PyTorch convention of §4.4. Patches come in raster order after the prefix tokens. Patch q may attend to patch k when their Chebyshev distance on the grid, the larger of the row gap and the column gap, max(∣rq−rk∣,∣cq−ck∣), is at most radius, with no wraparound. Prefix tokens attend to everything and are attended to by everything.
Tests, verified solutions, and AI help, open once you add your API key.
Robustness and shape bias, read carefully
The claim you will hear most often is that ViTs are "more robust" than CNNs. Some of the evidence is striking, and some of it dissolves under a controlled comparison. You need both halves for an interview.
The best-known behavioral test asks whether a model recognizes objects by their shape or by their texture. Geirhos et al. (2019) used style transfer to make 1,280 cue-conflict images in 16 categories. Each has the shape of one category and the texture of another, such as a cat's outline in elephant skin. The test is which category a model, or a person, picks.
The definition is a two-line count. Here it is on six hand-made trials. In the fourth the model names neither the shape nor the texture, so it does not count, and three shape decisions against two texture decisions give a shape bias of 0.60.
Python · cell 8
Before you credit that 87% to attention, look at two facts from the same literature. Geirhos et al. trained the same ResNet-50 on Stylized-ImageNet, where each image's texture is replaced by a random painting style, and its shape bias rose from 22% to 81%. And ViT-22B differs from a typical model in data (billions of images), size, and training, not only in architecture. Shape bias responds strongly to data and objective, so a high number tells you less about the architecture than it seems to.
Occlusion gives the same lesson. Naseer et al. (2021) zeroed random 16 by 16 patches of ImageNet validation images. With half the image removed, DeiT-S kept 70% top-1 accuracy while ResNet-50 fell to 0.1%, and DeiT-B still had 37% with 90% removed. That is a real property of the models as trained. But three things favor the ViT. The occlusion is aligned to the ViT's own patch grid. A zeroed patch is one constant token to a ViT but a set of sharp artificial edges to a convolution. And DeiT was trained with CutMix and random erasing, augmentations that paste over or blank out image regions, which a standard ResNet-50 recipe does not use.
Bai et al. (2021) ran the controlled experiment. They compared DeiT-S (22 million parameters, 76.8% on ImageNet) with ResNet-50 (25 million, 76.9%) and swapped training ingredients between them. Three kinds of robustness came out differently. Adversarial robustness is accuracy under small worst-case pixel changes. Patch attacks paste small adversarial texture patches onto the image. And out-of-distribution sets test photos unlike the training data. Two standard ones are ImageNet-A, natural photos that standard classifiers get wrong, and ImageNet-C, photos with synthetic corruptions (noise, blur, weather, digital artifacts) scored by mean corruption error (mCE, lower is better). The box gives the result and the numbers behind it.
So a matched comparison turns two of the three famous robustness gaps into differences of recipe. The lesson reaches well beyond this one paper, because most robustness claims in the literature change the architecture and the recipe at the same time.
In PyTorch
Here are the same symmetry facts, checked with real PyTorch modules. A Conv2d with padding_mode="circular" commutes with torch.roll. A ViT-style stack, a Conv2d patch embedding with kernel and stride 8 followed by a two-layer pre-LN TransformerEncoder, commutes with a shift by one whole patch when it has no position embeddings. The output tokens move one grid column and are otherwise unchanged. Add an absolute position table and that equivariance is gone. Shifts by a fraction of a patch match no grid shift at all.
🔥 In PyTorchfor a machine with PyTorch
Read more
Dosovitskiy et al. (2021). An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale. Section 3.1 states the inductive-bias argument, Section 4.3 and Figure 4 measure the data crossover, Table 6 and Figure 5 compare compute, and Figure 7 and Appendix D.7 introduce mean attention distance.
Abnar & Zuidema (2020). Quantifying Attention Flow in Transformers. Attention rollout and attention flow, the residual correction 21A+21I, and a comparison with ablation and gradient importance.
🎬 Stanford CS25: V1 I Transformers in Vision: Tackling problems in Computer Vision · Stanford OnlineLucas Beyer, a ViT author, on how Transformers took over vision, why scale of data decides whether a ViT beats a CNN, and what ViTs learn compared with CNNs.