Part 11 of 12
Read ML Error Messages and Profile Code: Cheat Sheet
Cheat sheetBy Bot Tutorials · Published · Facts last verified Nov 3, 2026
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.
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:
- Decode an error message and point to the line at fault.
- Suggest a fix and explain why it works.
- Read profiler output and say where the time goes.
It can't:
- Debug code it can't see. Paste the code, the full traceback and the shapes.
- Know your intended shapes. If it "fixes" a shape error by reshaping, check the reshape makes sense.
- Promise a speed-up. Profile again after the change.
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
- Shape errors: print
.shapefor each input. - Dtype errors: print
.dtype. Labels for classification are usually integers. - Device errors: print
.device, and checktorch.cuda.is_available(). - Out of memory: lower the batch size first, and see Part 2.
- Slow code: profile before you optimize, and check the answer didn't change.
Tested vs documented
- Tested by us: all four errors and the cProfile run, with PyTorch 2.15.0 CPU and NumPy 2.5.4.
- Documented, not tested by us:
torch.profilerand the device-selection advice. - Not claimed: the timings as a benchmark.
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.
- Check bot advice against official docs. Version numbers, install commands and compatibility tables change often. Compare every answer with the PyTorch, TensorFlow, JAX, scikit-learn, NVIDIA, Hugging Face or ONNX Runtime pages we link. If the bot and the docs disagree, the docs win.
- Work in a virtual environment. Don't install or remove packages in your system Python, and don't run
sudo pip. - Run untrusted files carefully. Pickle-based model files can run code when loaded. Only load models from sources you trust, and prefer safer formats like safetensors where the library supports them.
- Respect licenses. Datasets and model weights carry their own licenses and terms. Read the model card and license before you use, share or sell anything built on them. We don't give legal advice; ask a qualified person if it matters.
- Protect personal data. Don't paste private, customer or medical data into a bot or a training set unless you have the right to, and check the bot's own privacy settings first.
- No hardware claims you didn't measure. Treat any speed figure from a bot as a guess until you've timed it on your own machine.
- No medical, legal or financial decisions from a toy model. These guides are for learning only.
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
- PyTorch docs: CUDA semantics
- PyTorch docs: torch.profiler
- Python docs: The Python Profilers
- PyTorch: Start Locally
- NumPy: the absolute basics for beginners
More in this series
- Part 1: GPU, Driver and CUDA Compatibility: A Bot-Assisted Check · Guide
- Part 2: How Much Memory Does a Model Need? A Data Brief · Data brief
- Part 3: Pin Versions and Build Reproducible ML Environments · Walkthrough
- Part 4: NumPy and pandas Basics with a Bot: Cheat Sheet · Cheat sheet
- Part 5: Your First scikit-learn Model, Built with a Bot · Walkthrough
- Part 6: PyTorch on the CPU: First Steps with a Bot · Guide
- Part 7: TensorFlow and Keras Install Requirements, Explained · Explainer
- Part 8: JAX on the CPU: Field Notes with a Bot · Field notes
- Part 9: Read Hugging Face Model Cards and Licenses with a Bot · Guide
- Part 10: Export a Model to ONNX and Run It: A Deep Dive · Deep dive
- Part 11: Read ML Error Messages and Profile Code: Cheat Sheet (you are here) · Cheat sheet
- 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.