The creation of tensors
As we’ll deal with tensors everywhere in this book, we need to be familiar with basic operations on them, and the most basic is how to create one. There are several ways to do this, and your choice might influence code readability and performance.
If you are familiar with the NumPy library (and you should be), then you already know that its central purpose is the handling of multi-dimensional arrays in a generic way. Even though in NumPy, such arrays aren’t called tensors, they are, in fact, tensors. Tensors are used very widely in scientific computations as generic storage for data. For example, a color image could be encoded as a 3D tensor with the dimensions of width, height, and color plane. Apart from dimensions, a tensor is characterized by the type of its elements. There are 13 types supported by PyTorch:
-
Four float types: 16-bit, 32-bit, and 64-bit. 16-bit float has two variants: float16 has more bits...