PyTorch for Deep Learning: A Beginner-Friendly Guide

An overview of some Basic PyTorch functions.

Dele Akanbi
4 min readJul 21, 2020

The world is changing rapidly and technology is driving these changes. One of the fastest and most evolving domains in the field of technology is the deep learning sub-field of artificial intelligence.

Deep learning is a machine learning technology that lets a computer do some tasks that humans do naturally. Deep learning is providing the impetus to efforts to achieve some of these tasks. Tasks such as understanding of human language, speech recognition, and image classification, object detection are experiencing tremendous advancement within the field of artificial intelligence.

PyTorch is one of the major tools in the domain of deep learning. The advent of PyTorch brought in new choices for building machine models. This has attracted researchers, enthusiast and developers across the world and its popularity is increasing each day,.

PyTorch is a premier open-source deep learning framework. It was developed by Facebook’s artificial intelligence research group. PyTorch enables you to build amazing solutions by providing APIs that allow you to easily develop a suite of deep learning models for important tasks.

Let’s get started with basic overview of PyTorch by exploring some functions in PyTorch.

  • torch.arange()
  • torch.size()
  • torch.randn()
  • torch.reshape()
  • torch.t()
  • torch.from_numpy()
  • torch.argmax()
  • torch.argmin()
  • torch.view()
  • torch.permute()
  • torch.eye()
  • torch.cat()
  • torch.tensor.clone()
  • torch.zeros()
  • torch.unique()

Requirements

PyTorch is build on Python and you will need to install python on your system. You can refer to official Python documentation on how to install python on your system. In addition, you will need a Python IDE such as Jupyter notebook for this tutorial. Jupyter notebook is distributed as part of Anaconda Software studio.

Before we continue, we will import the torch library.

Tensor

A tensor is a multi-dimensional array (n-array) with a single data type. A tensor can take integer, floating-point or Boolean data type as element. A tensor may also consist of a single number, in such instance, it is referred to as a tensor of order zero, or simply a scalar. According to the popular deep learning book called “Deep Learning” (Goodfellow et al.) -

“In the general case, an array of numbers arranged on a regular grid with a variable number of axes is known as a tensor.”

A PyTorch Tensor uses tensor which is basically the same as a numpy array. This is a generic n-dimensional array used for arbitrary numeric computation. However, a PyTorch Tensor can run on either CPU or GPU which is the major difference from numpy array. Most computation is PyTorch use floating-point type rather than an integer type.

To know about tensor in PyTorch, we will look as some examples using the torch.tensor() function.

Running the above will yield the following.

x has type {torch.int64}

y has type {torch.float32}

h has type {torch.bool}

Below is an example.

The arange function creates a tensor where we give start to be ) and end to be 10 and step 2.

This returns a tensor k with elements (0, 2, 4, 6, 8).

This can also be done with floats too.

This also returns values for float.

Note: We cannot pass list when creating a tensor using arange function. The function accepts start , end and step only. This function can be used to create a tensor when there is a need for specific tensor values.

From the above PyTorch returned that a is a 2 by 2 tensor and b is a 2 by 3 tensor. This function is useful when we want to know/determine the size of a tensor.

The above is a tensor with five random elements.

This produces a tensor with size 3 x 4

Note: This function is useful when initializing neural network with random number. However, the size must be specified in the argument

The above is a 1 by 6 tensor

With the “reshape ” function, the shape has changed to a 2 x 3 dimension tensor.

The 2 x 3 dimension tensor x has been transposed by the function torch.t() to a 3 x 2 tensor z. This function is useful for tensor multiplication or for finding dot product of two tensors.

However, to use this we will need to import numpy library. See below.

Let us create a numpy ndarray

Let us convert the x_array to a tensor

torch.argmax() returns index 2 as the index with the maximum value. The corresponding element 1.3944

torch.argmin() returns index 9 as the index with the minimum value. The corresponding element -3.4935

In the example above the tensor with dimension 1 by 8 has its dimension changed to 2 by 4 tensor but the elements remain the same.

Conclusion

In this post, we have introduced some interesting PyTorch functions to kick start our journey into the exciting world of PyTorch. There are lots of other useful functions that are not covered in this short blog. More information can be found on the Official PyTorch documentation.

References

Provide links to your references and other interesting articles about tensors

Final Note

Big thank you to the team at Aakash N S and all the JovainML team

--

--