๐ŸŽ“ A1Learner
๐Ÿ”
0 of 79 lessons completed

PyTorch Reading Tensor Errors

A tensor error is a message that stops a program and names what went wrong. The last line of the message gives the error type and the clue to the fix.

A shape error

Two tables with different widths cannot be added cell by cell. The program stops with a traceback, a stack of lines that shows where it stopped.

import torch

a = torch.tensor([[1, 2, 3], [4, 5, 6]])
b = torch.tensor([[7, 8, 9, 10], [11, 12, 13, 14]])
print(a + b)

A dtype error

A mean needs decimal values. Whole-number scores carry the whole-number dtype, so the mean fails.

scores = torch.tensor([70, 80, 90])
print(scores.mean())

A device error

A device is the hardware a tensor lives on, like the CPU or a GPU. The name "cuda" in code asks for a GPU. This build of torch cannot use a GPU, so the request fails.

x = torch.tensor([1, 2, 3], device="cuda")
print(x)

Example

import torch

# Shape error: the inner sizes do not match.
try:
    a = torch.zeros(2, 3)
    b = torch.zeros(2, 4)
    print(a + b)
except RuntimeError as error:
    print("Shape error:", type(error).__name__)
    print(error)

# Dtype error: mean needs float values, not whole numbers.
try:
    scores = torch.tensor([70, 80, 90])
    print(scores.mean())
except RuntimeError as error:
    print("Dtype error:", type(error).__name__)
    print(error)

# Device error: this build of torch has no CUDA support.
try:
    x = torch.tensor([1, 2, 3], device="cuda")
    print(x)
except AssertionError as error:
    print("Device error:", type(error).__name__)
    print(error)

Key points

  • Traceback: the stack of lines printed when a program stops on an error
  • Use the last line to read the error type and its message
  • Shape error: the tensors do not fit together in size
  • Dtype error: the values have a type the operation cannot use
  • Device error: a tensor sits on a device the program cannot use
  • Reading the error slowly, line by line, turns the message into the fix
  • Error wording: the exact words can change between torch versions

In this example, one program triggers all three errors on purpose. Each try block runs code that fails. Each except block catches the error instead of stopping the program. The shape block adds a (2, 3) tensor to a (2, 4) tensor. The last row sizes, 3 and 4, do not match. The dtype block asks for the mean of whole-number scores. The mean needs decimal values, so torch refuses. The device block asks for a tensor on "cuda". This torch build has no CUDA support, so the request fails. On a machine with a GPU, the same kind of error appears when two tensors sit on different devices. Each block prints the error type first, then the message. The type and the message together name the fix.

Try it Yourself

Edit the code, then press Run.

Running is simulated ยท Python Playground

Exercise

A program stops with a long traceback. Where is the fastest clue to the fix?