rohitg00/ai-engineering-from-scratch · error · ValueError
shape mismatch image
Error message
shape mismatch image {tuple(image_emb.shape)} vs text {tuple(text_emb.shape)} What it means
Shape guard in cosine_alignment_loss: the image and text embedding tensors must agree on shape for pairwise cosine similarity, but their dims differ (typically batch or feature size mismatch between projector output and caption embedding).
Solutions
- Project image features to the same dim as text embeddings before the loss
- Ensure both tensors carry the same batch size
- Check that make_pair batches are concatenated consistently
Defensive patterns
Strategy: validation
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.
Appendix: 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: floatView on GitHub (pinned to 39ea8a1c6d)