Bot TutorialsGrok BotChatGPTClaudeGeminiCopilotPerplexityMore botsGuidesArticlesPapers
Topics
CookingHome automationFitnessCreative writingMusic productionCodingIndustrialPros (Professional track)
Search

Part 11 of 12

Read ML Error Messages and Profile Code: Cheat Sheet

Cheat sheetBy Bot Tutorials · Published · Facts last verified Nov 3, 2026

ML libraries series complete. "Review Your Training Code with a Bot: Checklist" was the finale. See every part in the series hub.

Part 11: four real PyTorch errors decoded, a cProfile run that finds the slow loop, and prompts that get a bot to fix the cause, not the symptom.

Video: Read ML Error Messages and Profile Code: Cheat Sheet (4:03). Narrated from this guide, with the real code and real output we ran. Captions included (caption file). Prefer reading? Everything in the video is in the guide below.

Machine learning errors look scary, but most of them say exactly what's wrong in the first line. This cheat sheet causes four common PyTorch errors on purpose, shows the real messages, and explains each one. Then it uses Python's built-in profiler to find which part of a script is slow. Throughout, the aim is to get a bot to fix the cause, not just make the error go away.

What the bot can and can't do here

It can:

It can't:

Check first, fix second

Read the first and last lines of the traceback before asking anyone. Then print the shapes and dtypes involved. Half the time that's the whole answer.

Four real errors

import torch
def show(fn):
    try: fn()
    except Exception as e: print(type(e).__name__ + ":", str(e).splitlines()[0])
show(lambda: torch.nn.Linear(4, 2)(torch.rand(8, 3)))
show(lambda: torch.rand(2, 3) @ torch.rand(2, 3))
show(lambda: torch.nn.CrossEntropyLoss()(torch.rand(4, 3), torch.tensor([0.0, 1.0, 2.0, 1.0])))
show(lambda: torch.rand(3, device="cuda"))

Real output:

RuntimeError: addmm: mat1 and mat2 shapes cannot be multiplied (8x3 and 4x2)
RuntimeError: mm: mat1 and mat2 shapes cannot be multiplied (2x3 and 2x3)
RuntimeError: expected target dtype to be Long or Byte, but got Float
AssertionError: Torch not compiled with CUDA enabled

1. Layer input size doesn't match

addmm: mat1 and mat2 shapes cannot be multiplied (8x3 and 4x2). A Linear(4, 2) layer expects 4 features per row, but the input has 3. The batch size of 8 is fine. Fix the data or the layer so the feature counts match. Don't let a bot "fix" this by reshaping the input into 4 columns, which would scramble the data.

2. Matrix multiply with the wrong orientation

mm: mat1 and mat2 shapes cannot be multiplied (2x3 and 2x3). For A @ B, the columns of A must equal the rows of B. Here 3 doesn't equal 2. Usually you meant to transpose one of them, or you meant element-wise *.

3. Wrong target dtype for classification

expected target dtype to be Long or Byte, but got Float. CrossEntropyLoss expects class indices as integers, like torch.tensor([0, 1, 2, 1]), not floats. This often happens when labels are loaded from a CSV as floats. Convert them with .long().

4. Asking for a GPU that isn't there

Torch not compiled with CUDA enabled. This build is CPU-only, which Part 1's report would have shown. PyTorch's CUDA semantics notes recommend choosing the device in one place, for example device = "cuda" if torch.cuda.is_available() else "cpu", and moving tensors with .to(device).

The error prompt

Here's my code, the full traceback and the shapes and dtypes of the tensors involved (pasted). Explain the first line of the error in plain English, point to the line that causes it, and suggest a fix that keeps the data meaning the same. If you'd need to know my intended shapes, ask instead of guessing.

Finding slow code with cProfile

Python's documentation describes cProfile as a profiler suitable for most users. We profiled two ways of computing the length of each row in a 20,000 by 16 array:

import cProfile, pstats, io
import numpy as np
def slow_norm(rows):
    return [sum(v * v for v in r) ** 0.5 for r in rows]
def fast_norm(arr):
    return np.linalg.norm(arr, axis=1)
data = np.random.default_rng(0).random((20000, 16))
pr = cProfile.Profile(); pr.enable()
a = slow_norm(data.tolist()); b = fast_norm(data)
pr.disable()
print("results match:", bool(np.allclose(a, b)))
st = pstats.Stats(pr)
for (fname, line, func), (cc, nc, tt, ct, callers) in st.stats.items():
    if func in ("slow_norm", "fast_norm"):
        print(f"{func}: cumulative {ct:.4f} s")

Real output from one run on our box:

results match: True
fast_norm: cumulative 0.0018 s
slow_norm: cumulative 0.0662 s

Both functions give the same answer, which we check first. The Python loop took about 0.061 seconds, against about 0.0013 seconds for NumPy's vectorized version. That's one run on one machine, not a benchmark, but the gap shows where to look.

Reading profiles with a bot

Here's the cProfile output for my script, sorted by cumulative time (pasted). Which three functions take the most time, and which of them are my code rather than a library? Suggest one change for the slowest, and tell me how to confirm it helped.

For PyTorch models, torch.profiler can show time per operator and on the GPU. Its docs page explains how to set it up. We didn't run it here.

Quick reference

Tested vs documented

Where to go next

The last part of the series reviews training code with a bot and recaps everything.

Safety, licensing and privacy: read this before you run anything

This series is about learning and small personal projects with machine learning libraries. A bot is a helper, not an authority.

FAQ

What does 'mat1 and mat2 shapes cannot be multiplied' mean?

The inner dimensions of a matrix multiply don't match. In our example a layer expecting 4 input features got 3.

Is the profiler timing a benchmark?

No. It's one run on our box, to show which function takes the time. Your numbers will differ.

Were these errors real?

Yes. Every error message on this page came from running the script with PyTorch 2.15.0 CPU on our box.

Sources

  1. PyTorch docs: CUDA semantics
  2. PyTorch docs: torch.profiler
  3. Python docs: The Python Profilers
  4. PyTorch: Start Locally
  5. NumPy: the absolute basics for beginners

More in this series

  1. Part 1: GPU, Driver and CUDA Compatibility: A Bot-Assisted Check · Guide
  2. Part 2: How Much Memory Does a Model Need? A Data Brief · Data brief
  3. Part 3: Pin Versions and Build Reproducible ML Environments · Walkthrough
  4. Part 4: NumPy and pandas Basics with a Bot: Cheat Sheet · Cheat sheet
  5. Part 5: Your First scikit-learn Model, Built with a Bot · Walkthrough
  6. Part 6: PyTorch on the CPU: First Steps with a Bot · Guide
  7. Part 7: TensorFlow and Keras Install Requirements, Explained · Explainer
  8. Part 8: JAX on the CPU: Field Notes with a Bot · Field notes
  9. Part 9: Read Hugging Face Model Cards and Licenses with a Bot · Guide
  10. Part 10: Export a Model to ONNX and Run It: A Deep Dive · Deep dive
  11. Part 11: Read ML Error Messages and Profile Code: Cheat Sheet (you are here) · Cheat sheet
  12. Part 12: Review Your Training Code with a Bot: Checklist · Checklist

See the ML Libraries with a Bot series hub · Latest on Bot Tutorials

Bot advice can be wrong. We cite official docs; check them yourself before acting, especially for anything involving money, safety or your data.