# Local filters and patch attention

Canonical: https://semiaifoundry.com/learn/atlas/spatial-filter-patch-attention/
Revision: 2026-10-07
Rights: https://semiaifoundry.com/ai-atlas/licenses.html

Contrasting computational structures · Atlas v1.3.0

# Local filters and patch attention

Compare fixed local receptive fields with content-weighted patch aggregation.

[Explore in Atlas](https://semiaifoundry.com/ai-atlas/#spatial-filter-patch-attention)Abstract computation with explicit coordinate, sample and operation units

Learning objectiveLocate the receptive field and identify how local boundary coverage differs from content-dependent aggregation.

## The mechanism

A convolutional layer shares a local kernel across spatial locations. The same coefficients can detect a similar pattern in more than one place, while stride, padding and kernel size determine which positions are seen. Patch attention instead calculates weights from the relationship between a query and candidate patches; those weights depend on input content. Both methods process structured arrays, but their connectivity and computation differ. The specimen uses a generated 5×5 line image, two authored 3×3 line filters and one query drawn from the center image patch. Its small arithmetic makes boundary effects and input-dependent attention visible. It deliberately separates a hand-built filter classifier from learned visual representations: fitting and evaluating a CNN or vision transformer would require training data, trainable projections, task losses and independent outcomes.

1.  Generate a vertical or horizontal line at one of five positions and optionally erase the middle row.
2.  Cross-correlate two 3×3 authored filters at each of nine valid locations without padding.
3.  Use the largest filter responses for an explicit line-orientation decision, retaining ties as ties.
4.  Flatten nine overlapping patches, score each against the center query with a scaled dot product, normalize and combine their values.

## Science and assumptions

### Local filters and patch attention · governing computation

$$
Y_{ij}=\sum_{u=0}^{2}\sum_{v=0}^{2}K_{uv}X_{i+u,j+v}
$$

Locate the receptive field and identify how local boundary coverage differs from content-dependent aggregation.

Symbols and units

X

Five by five input image

pixel amplitude

K

Three by three shared kernel

filter coefficient

Y

Three by three valid response

weighted amplitude

i,j

Output location

pixel index

u,v

Kernel coordinate

pixel index

A centered vertical line gives a maximum vertical-filter response of 3 and horizontal response of 1, so the simple classifier returns vertical. At the outermost column, valid convolution never centers its vertical filter there; the maximum vertical response becomes 0 while horizontal response is 1. The wrong label follows from the stated coverage, not from an invented accuracy score. Two filters × nine output positions × nine kernel entries require 162 MACs; nine patch scores × nine coordinates require 81 dot-product MACs before attention normalization and value aggregation.

Assumptions Authored filters and one identity-projection patch query; stride one, no padding; no trained CNN, full ViT, learned positions or recognition benchmark.

## Engineering example

A centered vertical line gives a maximum vertical-filter response of 3 and horizontal response of 1, so the simple classifier returns vertical. At the outermost column, valid convolution never centers its vertical filter there; the maximum vertical response becomes 0 while horizontal response is 1. The wrong label follows from the stated coverage, not from an invented accuracy score. Two filters × nine output positions × nine kernel entries require 162 MACs; nine patch scores × nine coordinates require 81 dot-product MACs before attention normalization and value aggregation.

## Distinctions and tradeoffs

An attention heatmap is not proof that the model understands an image, and authored filters are not a trained vision model.

Local weight sharing imposes useful spatial structure and bounded receptive fields. Content-dependent aggregation changes connectivity, while padding, tokenization and position handling still matter.

Vision, industrial inspection and multimodal systems turn sampled images into representations through concrete spatial and learned operations.

## Specifications and interfaces

Computational contract

Compare fixed local receptive fields with content-weighted patch aggregation.

Authored filters and one identity-projection patch query; stride one, no padding; no trained CNN, full ViT, learned positions or recognition benchmark.

Learning evidence

Inspectable executable specimen

Inputs, intermediate values, exact output and a declared comparison or failure are available in Contrasting AI mechanisms.

### Interfaces

Specified input Input

Generate a vertical or horizontal line at one of five positions and optionally erase the middle row.

Computed output Output

Locate the receptive field and identify how local boundary coverage differs from content-dependent aggregation.

## Check your understanding

### Why does the edge-line fixture fail under this valid-convolution classifier?

1.  The vertical filter is never centered on that outermost column
2.  All vertical lines are inherently ambiguous
3.  More attention heads would prove the target label

Answer and reasoning

The vertical filter is never centered on that outermost column

The kernel placement and classifier are explicit. A boundary position outside the selected receptive-field centers can lose the intended filter response.

## Evidence and scope

Authored filters and one identity-projection patch query; stride one, no padding; no trained CNN, full ViT, learned positions or recognition benchmark. Numerical settings are original teaching fixtures. The named experience executes the stated bounded algorithm; no production performance or independent human review is claimed.

Authored filters and one identity-projection patch query; stride one, no padding; no trained CNN, full ViT, learned positions or recognition benchmark.

## Sources

-   [PyTorch Conv2d, cross-correlation and shape](https://docs.pytorch.org/docs/2.14/generated/torch.nn.Conv2d.html)
-   [Vaswani et al., scaled dot-product attention](https://arxiv.org/abs/1706.03762)

[Published lesson data](https://semiaifoundry.com/ai-atlas/lesson-data/spatial-filter-patch-attention-36a796546c85f406.json) · [Atlas releases and licensing](https://semiaifoundry.com/ai-atlas/licenses.html)

## Continue exploring

### Foundations

-   [Tensor shapes, indices and contractions](https://semiaifoundry.com/learn/atlas/tensor-index-contracts/)
-   [Probability, likelihood and stable information](https://semiaifoundry.com/learn/atlas/probability-conditioning-information/)

### Related lessons

-   [Patches, projection and visual instruction tuning](https://semiaifoundry.com/learn/atlas/visual-language-bridge/)
-   [Pair images and captions in a shared representation](https://semiaifoundry.com/learn/atlas/image-text-contrastive-learning/)
-   [Self-attention](https://semiaifoundry.com/learn/atlas/attention/)
