Data Types and Devices¶
Module 1 | Lesson 3
Ah, the Alchemist’s Arsenal: PyTorch Data Types!
Behold, apprentice! Not all tensors are forged alike. The essence of a tensor—its .dtype
—determines what kind of numbers it can hold, and thus, what arcane computations it can perform. Choose wisely, for the wrong dtype
can turn your beautiful model into a bubbling cauldron of errors!
Floating-Point Types
The lifeblood of neural networks! For when you wish to summon real numbers, gradients, and the very stuff of learning itself.
torch.float32
ortorch.float
: 32-bit floating-point. The default elixir for most tensor incantations. Trusty, reliable, and the backbone of deep learning!torch.float64
ortorch.double
: 64-bit floating-point. For those moments when you crave precision—perhaps to impress your rival, Dr. Hammer, with your numerical exactitude.torch.float16
ortorch.half
: 16-bit floating-point. The potion of choice for speed demons and memory misers. Use it to accelerate your experiments (especially on modern GPUs), but beware the lurking specter of numerical instability!torch.bfloat16
: 16-bit “brain” floating-point. Like float16, but with a twist—wider range, less precision. Favored by TPUs and the latest NVIDIA cauldrons.
Integer Types
For when you need to count, index, or encode the world in whole numbers. No fractions allowed!
torch.int8
: 8-bit signed integer. Tiny, but sometimes mighty.torch.uint8
: 8-bit unsigned integer. The pixel pusher’s favorite—perfect for images and masks!torch.int16
ortorch.short
: 16-bit signed integer. Slightly more room for your integers to stretch their legs.torch.int32
ortorch.int
: 32-bit signed integer. The workhorse of integer types.torch.int64
ortorch.long
: 64-bit signed integer. The grandmaster—often used for indices, embedding lookups, and any time you need to count very high.
Boolean Type
For the binary-minded: True or False, 1 or 0, on or off. The stuff of logic gates and comparison spells!
torch.bool
: The result of your tensor comparisons, your maskings, your “is it alive or dead?” queries.
Complex Types
For the truly mad scientist—numbers with both real and imaginary parts! Useful for signal processing, quantum shenanigans, and impressing your colleagues at tensor parties.
torch.complex64
: Complex numbers with 32-bit real and 32-bit imaginary parts.torch.complex128
: Complex numbers with 64-bit real and 64-bit imaginary parts. For when your calculations must be both precise and mysterious.
Choose your dtype as you would choose your wand, apprentice: with care, curiosity, and a dash of reckless ambition! Mwahahaha!