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

expected 4D input, got

Error message

expected 4D input, got {tuple(x.shape)}

What it means

Shape guard in the unfold reference implementation: the manual patch-extraction path requires a 4D (B,C,H,W) tensor, matching the Conv2d it is verified against.

Solutions

  1. Unsqueeze to add the batch dimension before calling
  2. Feed the same 4D input used for the Conv2d comparison
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at phases/19-capstone-projects/58-vision-encoder-patches/code/main.py:156 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/d5e3b7a0e1306076. Report an issue: GitHub.

Appendix: source

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

    """
    rng = np.random.default_rng(seed)
    noise = rng.standard_normal((channels, image_size, image_size)).astype("float32") * 0.1
    y_coords = np.linspace(0.0, 1.0, image_size, dtype="float32")
    x_coords = np.linspace(0.0, 1.0, image_size, dtype="float32")
    gx, gy = np.meshgrid(x_coords, y_coords, indexing="xy")
    gradient = np.stack([gx, gy, (gx + gy) * 0.5], axis=0).astype("float32")
    img = np.clip(gradient + noise + 0.5, 0.0, 1.0)
    return torch.from_numpy(img).unsqueeze(0)


def unfold_then_linear(x: torch.Tensor, weight: torch.Tensor, bias: torch.Tensor, patch_size: int) -> torch.Tensor:
    """Reference implementation of patch projection via unfold + matmul.

    Used by the tests to assert that the Conv2d projection matches the
    flatten-then-linear math.
    """
    if x.dim() != 4:
        raise ValueError(f"expected 4D input, got {tuple(x.shape)}")
    patches = x.unfold(2, patch_size, patch_size).unfold(3, patch_size, patch_size)
    b, c, gh, gw, ph, pw = patches.shape
    flat = patches.permute(0, 2, 3, 1, 4, 5).reshape(b, gh * gw, c * ph * pw)
    w_flat = weight.reshape(weight.shape[0], -1)
    return flat @ w_flat.T + bias


def describe_token_norms(tokens: torch.Tensor, max_show: int = 8) -> str:
    """Print the L2 norm of the first few tokens for sanity inspection."""
    norms = tokens.detach().norm(dim=-1)[0].tolist()
    head = norms[:max_show]
    return ", ".join(f"{v:.3f}" for v in head)


def main() -> None:
    print("=" * 60)
    print("VISION ENCODER PATCHES")
    print("=" * 60)

View on GitHub (pinned to 39ea8a1c6d)