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

shape mismatch image {tuple(image_emb.shape)} vs text {tuple

Error message

shape mismatch image {tuple(image_emb.shape)} vs text {tuple(text_emb.shape)}

What it means

Error "shape mismatch image {tuple(image_emb.shape)} vs text {tuple(text_emb.shape)}" thrown in rohitg00/ai-engineering-from-scratch.

Source

Thrown at phases/19-capstone-projects/60-projection-layer-modality-align/code/main.py:115

def make_pair(seed: int, vocab_size: int, max_len: int) -> tuple[torch.Tensor, torch.Tensor]:
    """One synthetic (image, caption_ids) pair.

    Image is the deterministic 224x224x3 fixture from lesson 58 with a per-pair
    seed. Caption is a length-`max_len` sequence of token ids, again
    deterministic in seed. Token id 0 is reserved as padding.
    """
    img = synthesize_image(seed=seed)
    rng = np.random.default_rng(seed + 10_000)
    length = int(rng.integers(4, max_len + 1))
    ids = np.zeros((max_len,), dtype=np.int64)
    ids[:length] = rng.integers(1, vocab_size, size=length)
    return img, torch.from_numpy(ids).unsqueeze(0)


def cosine_alignment_loss(image_emb: torch.Tensor, text_emb: torch.Tensor) -> torch.Tensor:
    if image_emb.shape != text_emb.shape:
        raise ValueError(
            f"shape mismatch image {tuple(image_emb.shape)} vs text {tuple(text_emb.shape)}"
        )
    img_n = F.normalize(image_emb, dim=-1)
    txt_n = F.normalize(text_emb, dim=-1)
    cos = (img_n * txt_n).sum(dim=-1)
    return (1.0 - cos).mean()


def freeze(module: nn.Module) -> None:
    for p in module.parameters():
        p.requires_grad_(False)


@dataclass
class TrainStats:
    initial_loss: float
    final_loss: float
    final_cos: float

View on GitHub (pinned to 39ea8a1c6d)

When it happens

Trigger: Thrown at phases/19-capstone-projects/60-projection-layer-modality-align/code/main.py:115 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/2bea1e05f5ff9c29. Report an issue: GitHub.