Split Images into ViT Patches

easy
Unit 6 · Augmentation0/3 to milestone

A Vision Transformer treats an image as a sequence of patches. Getting the reshape right is most of the work.

For images x: (N, C, H, W) and patch size p (dividing H and W):

  • patchify(x, p): return (N, (H/p) * (W/p), C * p * p). Patches are ordered row by row across the image, and each patch is flattened in (C, p, p) order.
  • unpatchify(patches, p, C, H, W): the exact inverse
  • embed(patches, W_proj, cls, pos): project each patch with W_proj: (C*p*p, D), prepend the class token cls: (D,) to every sequence, and add the position embeddings pos: (1 + num_patches, D). Return (N, 1 + num_patches, D).
#vision-transformers#tensors#reshaping2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Split Images into ViT Patches · TechGradify