Being a deep learning framework, PyTorch can be used for numerical computing as well. Here we discuss the basic operations in PyTorch. The basic PyTorch operations in this chapter will make your life easier in the next chapter, where we will try to build an actual neural network for a simple use case. We'll be using Python 3.7 and PyTorch 1.0 for all the programs in the book. The GitHub repository is also built with the same configuration: PyTorch from PyPI instead of Conda, although it is the recommended package manager by the PyTorch team.
Learning the basic operations
Let's start coding by importing torch
into the namespace:
The fundamental data abstraction in PyTorch is a Tensor
object, which is the alternative of ndarray
in NumPy. You can create tensors in several ways in PyTorch. We'll discuss some of the basic approaches here and you will see all of them in the upcoming chapters while building the applications:
The rand
method gives you a random matrix of a given size, while the Tensor
function returns an uninitialized tensor. To create a tensor object from a Python list, you call torch.FloatTensor(python_list)
, which is analogous to np.array(python_list)
. FloatTensor
is one among the several types that PyTorch supports. A list of the available types is given in the following table:
Table 1.1: DataTypes supported by PyTorch. Source: http://pytorch.org/docs/master/tensors.html
With each release, PyTorch makes several changes to the API, such that all the possible APIs are similar to NumPy APIs. Shape was one of those changes introduced in the 0.2 release. Calling the shape
attribute gives you the shape (size in PyTorch terminology) of the tensor, which can be accessible through the size
function as well:
The shape
object is inherited from Python tuples and hence all the possible operations on a tuple are possible on a shape
object as well. As a nice side effect, the shape
object is immutable.
Now, since you know what a tensor is and how one can be created, we'll start with the most basic math operations. Once you get acquainted with operations such as multiplication addition and matrix operations, everything else is just Lego blocks on top of that.
PyTorch tensor objects have overridden the numerical operations of Python and you are fine with the normal operators. Tensor-scalar operations are probably the simplest:
Variables x
and y
being 3 x 2 tensors, the Python multiplication operator does element-wise multiplication and gives a tensor of the same shape. This tensor and the z
tensor of shape 2 x 1 is going through Python's matrix multiplication operator and spits out a 3 x 1 matrix.
You have several options for tensor-tensor operations, such as normal Python operators, as you have seen in the preceding example, in-place PyTorch functions, and out-place PyTorch functions.
Two tensors of the same size can be added together by using the +
operator or the add
function to get an output tensor of the same shape. PyTorch follows the convention of having a trailing underscore for the same operation, but this happens in place. For example, a.add(b)
gives you a new tensor with summation ran over a
and b
. This operation would not make any changes to the existing a
and b
tensors. But a.add_(b)
updates tensor a
with the summed value and returns the updated a
. The same is applicable to all the operators in PyTorch.
Note
In-place operators follow the convention of the trailing underscore, like add_
and sub_
.
Matrix multiplication can be done using the function matmul
, while there are other functions like mm
and Python's @
for the same purpose. Slicing, indexing, and joining are the next most important tasks you'll end up doing while coding up your network. PyTorch enables you to do all of them with basic Pythonic or NumPy syntax.
Indexing a tensor is like indexing a normal Python list. Indexing multiple dimensions can be done by recursively indexing each dimension. Indexing chooses the index from the first available dimension. Each dimension can be separated while indexing by using a comma. You can use this method when doing slicing. Start and end indices can be separated using a full colon. The transpose of a matrix can be accessed using the attribute t
; every PyTorch tensor object has the attribute t
.
Concatenation is another important operation that you need in your toolbox. PyTorch made the function cat
for the same purpose. Two tensors of the same size on all the dimensions except one, if required, can be concatenated using cat
. For example, a tensor of size 3 x 2 x 4 can be concatenated with another tensor of size 3 x 5 x 4 on the first dimension to get a tensor of size 3 x 7 x 4. The stack
operation looks very similar to concatenation but it is an entirely different operation. If you want to add a new dimension to your tensor, stack
is the way to go. Similar to cat
, you can pass the axis where you want to add the new dimension. However, make sure all the dimensions of the two tensors are the same other than the attaching dimension.
split
and chunk
are similar operations for splitting your tensor. split
accepts the size you want each output tensor to be. For example, if you are splitting a tensor of size 3 x 2 with size 1 in the 0th dimension, you'll get three tensors each of size 1 x 2. However, if you give 2 as the size on the zeroth dimension, you'll get a tensor of size 2 x 2 and another of size 1 x 2.
The squeeze
function sometimes saves you hours of time. There are situations where you'll have tensors with one or more dimension size as 1. Sometimes, you don't need those extra dimensions in your tensor. That is where squeeze
is going to help you. squeeze
removes the dimension with value 1. For example, if you are dealing with sentences and you have a batch of 10 sentences with five words each, when you map that to a tensor object, you'll get a tensor of 10 x 5. Then you realize that you have to convert that to one-hot vectors for your neural network to process.
You add another dimension to your tensor with a one-hot encoded vector of size 100 (because you have 100 words in your vocabulary). Now you have a tensor object of size 10 x 5 x 100 and you are passing one word at a time from each batch and each sentence.
Now you have to split and slice your sentence and most probably, you will end up having tensors of size 10 x 1 x 100 (one word from each batch of 10 with a 100-dimension vector). You can process it with a 10 x 100-dimension tensor, which makes your life much easier. Go ahead with squeeze
to get a 10 x 100 tensor from a 10 x 1 x 100 tensor.
PyTorch has the anti-squeeze operation, called unsqueeze
, which adds another fake dimension to your tensor object. Don't confuse unsqueeze
with stack
, which also adds another dimension. unsqueeze
adds a fake dimension and it doesn't require another tensor to do so, but stack
is adding another tensor of the same shape to another dimension of your reference tensor.
If you are comfortable with all these basic operations, you can proceed to the second chapter and start the coding session right now. PyTorch comes with tons of other important operations, which you will definitely find useful as you start building the network. We will see most of them in the upcoming chapters, but if you want to learn that first, head to the PyTorch website and check out its tensor tutorial page, which describes all the operations that a tensor object can do.
One of the core philosophies of PyTorch, which came about with the evolution of PyTorch itself, is interoperability. The development team invested a lot of time into enabling interoperability between different frameworks, such as ONNX, DLPack, and so on. Examples of these will be shown in later chapters, but here we will discuss how the internals of PyTorch are designed to accommodate this requirement without compromising on speed.
A normal Python data structure is a single-layered memory object that can save data and metadata. But PyTorch data structures are designed in layers, which makes the framework not only interoperable but also memory-efficient. The computationally intensive portion of the PyTorch core has been migrated to the C/C++ backend through the ATen and Caffe2 libraries, instead of keeping this in Python itself, in favor of speed improvement.
Even though PyTorch has been created as a research framework, it has been converted to a research-oriented but production-ready framework. The trade-offs that came along with multi-use case requirements have been handled by introducing two execution types. We'll see more about this in Chapter 8, PyTorch to Production, where we discuss how to move PyTorch to production.
The custom data structure designed in the C/C++ backend has been divided into different layers. For simplicity, we'll be omitting CUDA data structures and focusing on simple CPU data structures. The main user-facing data structure in PyTorch is a THTensor
object, which holds the information about dimension, offset, stride, and so on. However, another main piece of information THTensor
stores is the pointer towards the THStorage
object, which is an internal layer of the tensor object kept for storage.
As you may have assumed, the THStorage layer is not a smart data structure and it doesn't really know the metadata of our tensor. The THStorage layer is responsible for keeping the pointer towards the raw data and the allocator. The allocator is another topic entirely, and there are different allocators for CPU, GPU, shared memory, and so on. The pointer from THStorage that points to the raw data is the key to interoperability. The raw data is where the actual data is stored but without any structure. This three-layered representation of each tensor object makes the implementation of PyTorch memory-efficient. Following are some examples.
Variable x
is created as a tensor of size 2 x 2 filled with 1s. Then we create another variable, xv
, which is another view of the same tensor, x
. We flatten the 2 x 2 tensor to a single dimension tensor of size 4. We also make a NumPy array by calling the .NumPy()
method and storing that in the variable xn
:
PyTorch provides several APIs to check internal information and storage()
is one among them. The storage()
method returns the storage object (THStorage
), which is the second layer in the PyTorch data structure depicted previously. The storage object of both x
and xv
is shown as follows. Even though the view (dimension) of both tensors is different, the storage shows the same dimension, which proves that THTensor
stores the information about dimensions but the storage layer is a dump layer that just points the user to the raw data object. To confirm this, we use another API available in the THStorage
object, which is data_ptr
. This points us to the raw data object. Equating data_ptr
of both x
and xv
proves that both are the same:
Next, we change the first value in the tensor, which is at the indices 0, 0 to 20. Variables x
and xv
have a different THTensor
layer, since the dimension has been changed but the actual raw data is the same for both of them, which makes it really easy and memory-efficient to create n number of views of the same tensor for different purposes.
Even the NumPy array, xn
, shares the same raw data object with other variables, and hence the change of value in one tensor reflects a change of the same value in all other tensors that point to the same raw data object. DLPack is an extension of this idea, which makes communication between different frameworks easy in the same program.