rohitg00/ai-engineering-from-scratch · error · ValueError

patch_size {self.patch_size} must divide image_size {self.im

Error message

patch_size {self.patch_size} must divide image_size {self.image_size}

What it means

Error "patch_size {self.patch_size} must divide image_size {self.image_size}" thrown in rohitg00/ai-engineering-from-scratch.

Source

Thrown at phases/19-capstone-projects/58-vision-encoder-patches/code/main.py:32

import math
from dataclasses import dataclass

import numpy as np
import torch
import torch.nn as nn


@dataclass(frozen=True)
class FrontEndConfig:
    image_size: int = 224
    patch_size: int = 16
    in_channels: int = 3
    hidden: int = 768

    @property
    def grid_size(self) -> int:
        if self.image_size % self.patch_size != 0:
            raise ValueError(
                f"patch_size {self.patch_size} must divide image_size {self.image_size}"
            )
        return self.image_size // self.patch_size

    @property
    def num_patches(self) -> int:
        return self.grid_size * self.grid_size


def sinusoidal_2d(grid_h: int, grid_w: int, dim: int) -> torch.Tensor:
    """Build a deterministic 2D sinusoidal position table of shape (grid_h * grid_w, dim).

    Half of dim encodes row position, half encodes column position. Within each
    half, frequencies span the standard Transformer sin/cos band. Identical
    inputs always produce identical outputs, with no learned state.
    """
    if dim % 4 != 0:
        raise ValueError(f"sinusoidal_2d dim must be divisible by 4, got {dim}")

View on GitHub (pinned to 39ea8a1c6d)

When it happens

Trigger: Thrown at phases/19-capstone-projects/58-vision-encoder-patches/code/main.py:32 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of rohitg00/ai-engineering-from-scratch@39ea8a1c6d (2026-08-26). Data as JSON: /api/errors/6a0bb29ff860a861. Report an issue: GitHub.