Tensor Types
In this lesson, we discuss the different types supported by the tensors in PyTorch and show how to cast them into other types.
Why tensor type is important
Similar to other frameworks, PyTorch too defines its types for its tensor. Some frequently used types are listed in the table below.
Data Type | dtype |
| GPU tensor |
32-bit floating point | torch.float32/torch.float | torch.FloatTensor | torch.cuda.FloatTensor |
64-bit floating point | torch.float64/torch.double | torch.DoubleTensor | torch.cuda.DoubleTensor |
8-bit integer (signed) | torch.int16 | torch.ShortTensor | torch.cuda.ShortTensor |
boolean | torch.bool | torch.BoolTensor | torch.cuda.BoolTensor |
Notice: For
integer
, there are also 16-bit, 32-bit, and 64-bit types, signed and unsigned. Forfloating
, there is also a 16-bit type.
The tensor type is vital. There are two main reasons.
- It affects speed and memory usage because the GPU has video memory limitations. More bits type takes up more memory and requires more computing resources. For example, a matrix
A
with size 1000*1000. If thedtype
istorch.float32
, this matrix would consume about 3.81MB GPU memory (1000*1000*4bytes, eachfloat32
uses 4 bytes.). If thedtype
istorch.double
, this matrix would consume about 7.62MB GPU memory (1000*1000*8bytes, eachdouble
uses 8