Every tensor carries three pieces of identity
When you call print(t), PyTorch shows you the values. When something is broken, the values rarely matter — the metadata does. Three attributes, in order of debugging importance:
- shape — the dimensions. The first thing to check on any error.
- dtype — the element type. The second thing to check, especially with mixed-precision training.
- device — where the data physically lives. The "expected cuda:0 got cpu" error.
Internalize the dtype family because mixed-precision training is now the default for serious work:
torch.float32(aliastorch.float) — the universal default for activations and weights.torch.float16(aliastorch.half) — half the memory, half the bandwidth, but a narrow dynamic range that requires a GradScaler.torch.bfloat16— same exponent range as float32, less mantissa precision. The modern mixed-precision dtype on Ampere+ GPUs and Apple Silicon. No GradScaler usually needed.torch.float64(double) — full precision. Use for scientific computing or when you suspect numerical issues, never as a default for training.torch.int64(long) — the default for class labels, indices, embedding lookups.torch.bool— masks.
Casting
.to() is the universal mover/caster. It accepts a dtype, a device, or both, and is a no-op if the tensor is already in the requested form. There are also short forms (.float(), .half(), .cpu(), .cuda()) — use whichever reads cleanest in context.