5 Powerful Interesting Pytorch functions For Data Science Beginners

Image by Deepcode Inc.

The knowledge of Pytorch is one of the most important skills on the path to become a Data Scientist most especially when working on Neural Networks and Deep Learning projects.

What is Pytorch?

PyTorch is a Python library with a wide variety of functions and operations, mostly used for deep learning. One of the most basic yet important parts of PyTorch is the ability to create Tensors. A tensor is a number, vector, matrix, or any n-dimensional array.

Someone once ask me ‘why not use numpy arrays instead?’

For Deep Learning, we would need to compute the derivative of elements of the data. PyTorch provides the ability to compute derivatives automatically, which NumPy does not. This is called ‘Auto Grad.’ PyTorch also provides built in support for fast execution using GPU. This is essential when it comes to training models.

All Deep Learning projects using PyTorch start with creating a tensor. Let’s see a few MUST HAVE functions which are the backbone of any Deep Learning project.

  • torch.from_numpy()
  • torch.split()
  • torch.transpose()
  • torch.unbind()
  • torch.randint()

Before we begin, let’s install and import PyTorch

Let’s also import our torch library

Now let’s start from the first function

Function 1 — Torch.from_numpy() :

this function can help us to change a numpy array to a tensor without changing the data type. see example below

The numpy array x has been converted to tensor y without changing the data type

let’s look at another example;

The numpy array x1 has been converted to tensor y1 without changing the data type

There are some cases where the above function will not work, a situation where it breaks, check the example below

We can’t convert np.ndarray of type numpy.str_. The only supported types are: float64, float32, float16, complex64, complex128, int64, int32, int16, int8, uint8, and bool.

when working on deep learning project, it is very important to change your data from numpy to tensor and this can be achieve by using the function above.

Function 2 — torch.split():

tensor can be splits into chunks. Each chunk is a view of the original tensor.

If split_size_or_sections is an integer type, then tensor will be split into equally sized chunks (if possible). Last chunk will be smaller if the tensor size along the given dimension dim is not divisible by split_size.

If split_size_or_sections is a list, then tensor will be split into len(split_size_or_sections) chunks with sizes in dim according to split_size_or_sections.

The explanation above looks like jargons right?, check the example below

the tensor has been split into two group using the torch split function above, this fuction can be very useful when training and testing a model.

let’s look at another example for more clarification

The tensor has been splitted into 2 part using the split function

let’s look at the situation where this function can’t hold

as you can see from the above that it brings an error when we make an attempt to split y5, this happened because the splitting size exceed the tensor dimension

Function 3 — torch.transpose:

when the need to interchange the column and row of a matrix arises then this function is very useful

the matrix as been transpose easily with the function

the matrix has been transpose easily using the function

we can also illustrate when the function can’t hold, see below

y8 can not be transpose because it is not a matrix

Function 4 — torch.unbind

This function removes a dimension from a tensor and returns a tuple of slices of the tensor without the removed dimension.

looking carefully at the example above, y9 has been sliced

the tensor y10 has been unbind as shown above

let’s look at another example where its can’t hold

Dimension out of range (expected to be in range of [-2, 1], but got 2)

this function is very useful when dealing with large dataframe from which some particular data can be extrated

Function 5 — torch.randint

In a situation where we need to generate some random integers, this fuction is very useful

torch.randint : Returns a tensor filled with random integers generated uniformly between low (inclusive) and high (exclusive).

The parameters are define as follows

low (int, optional) — Lowest integer to be drawn from the distribution. Default: 0.

high (int) — One above the highest integer to be drawn from the distribution.

size (tuple) — a tuple defining the shape of the output tensor.

the above function has been been used to generate a tensor with random integer

The function can also be use to generate a matrix containing random integer as shown above

This gave an error because the dimension of the tensor was not define

This function will be very useful when random integer is needed

Conclusion

The above 5 functions of pytorch are very important when working on deep learning project, PyTorch provides many such functions which make job of a data science enthusiast easier. This was a beginner friendly introduction to PyTorch. There is much more. From here it would be a good idea to explore the documentation and create your own tensors. As we get a tighter grip on the basics, we can move forward to Neural Networks and Deep Learning.

You can also check the link below for further reading

https://pytorch.org/docs/stable/torch.html