Select Page

Are you afraid that AI might take your job? Make sure you are the one who is building it.

STAY RELEVANT IN THE RISING AI INDUSTRY! 🖖

Elon Musk made it clear that he doesn’t like Facebook, however, Tesla still uses one piece of technology that comes from Zukenberg’s laboratory. We can bet that it made its way into SpaceX as well and that it was an important part of the recent launch too. This technology has a big advocate in the AI world and that is Yan LeCunn, father of modern Convolutional Neural Networks. That is correct, we are talking about PyTorch. This deep learning framework is extremely popular among the research community and we can witness that in the past couple of years it gained popularity in the industry as well. In this article, we explore the basic principles of PyTorch and how we can use can be used for simple tasks. Generally speaking PyTorch as a tool has two big goals. The first one is to be NumPy for GPUs. This doesn’t mean that NumPy is a bad tool, it just means that it doesn’t utilize the power of GPUs. The second goal of PyTorch is to be a deep learning framework that provides speed and flexibility. That is why it is so popular in the research community because it provides a platform in which users can quickly perform experiments. Apart from that, building models in PyTorch is very easy.

## Installation

Installing PyTorch is quite easy and you can create a command that you need to run for the installation on this page. All you need to do is enter your environment details and run the command. Here is how I’ve set it up for my machine: To verify that you have installed it correctly, run Jupyter Notebook and try to import torch:

In general, a lot of concepts in machine learning and deep learning can be abstracted using multi-dimensional matrices – tensors. Mathematically speaking, tensors are described as geometric objects that describe linear relationships between other geometric objects. For the simplification of all concepts, tensors can be observed as multi-dimensional arrays of data. When we observe them like n-dimensional arrays we can apply matrix operations easily and effectively. That is what PyTorch is actually doing. In this framework, a tensor is a primitive unit used to model scalars, vectors and matrices located in the central class of the package torch.Tensor. We can do various operations with tensors, but first, let’s see how we can create and initialize them.

### Creating and Initializing Tensors

Here is how we can define scalar, vector, and matrix:

In the code snippet above, we created scalar, vector, matrix and 3D array. Note that for vector we use integers and for other floats. Also, we printed out their data types and shapes. Here is the output: As you can see it is quite easy to create all the types of data that we need quickly. There are various tensor initialization functions. For example, you can create uninitialized tensor, or tensor initialized with zeroes, ones or with some random value:

As we mentioned one of the benefits of using PyTorch is that you can perform NumPy operations in parallel on GPU. This means that we can create tensors from NumPy arrays, and vice-versa create NumPy arrays from PyTorch tensors. The cool thing is that the tensor and NumPy array share underlying memory locations, so the changing one will change the other:

### Tensor Operations

Torch tensors can easily be manipulated and there are various options to perform mathematical operations on them. For example, we can do something like this: There are also the different syntax of these operations that you can use. For example, if you want to add two tensors there are several options that will give the same results:

Apart from this, you can reshape tensors using view function:

Finally, it is important to mention that you can index tensors just like you would index NumPy arrays:

There are many other functions that you can do with PyTorch tensors, but for start, these will get you going.

Another core concept of Pytorch is the notion of gradients. They are located in torch.autograd package. In essence, this package performs automatic differentiation and it is define-by-run. This means that backpropagation is defined by how your code is run. In order to utilize this functionality, all you have to do is set PyTorch tensor attribute `.requires_grad` as `True`. This will signal PyTorch to record all operations performed on that tensor. Once the computation or some interaction is finished, you can call function `.backward()` and have all the gradients computed automatically. The gradient for each tensor is stored into `.grad` attribute of the class. Here is an example. First, let’s create 3×3 tensor: Then let’s add a scalar to it:

Note that grad attribute is now filled and that performed operations are recorded. This means that we can perform some more operations and calculate gradients and print them out:

## Linear Regression

Ok, this should be enough PyTorch background to help us implement Linear Regression. Linear Regression is one of the foundational algorithms of machine learning. It attempts to model the relationship between two variables by fitting a linear equation to observed data. In our example, we have multiple variables, which means that we will implement Multiple Linear Regression. Note that we are not following strict definitions here, and we are using some techniques that are more used in deep learning than in standard machine learning. This is because PyTorch is a primarily deep learning framework and in the next several articles we will use it to develop neural networks. Anyhow, Linear Regression can be described by the formula: Where is the input vector (presented as x1, x2, …, xn) and y is the output target. The goal of Linear regression is to predict correct weights vector w and bias b that will for new values for input x give correct values for output y. Linear regression learns these values during the training process where and x values are known (supervised learning). Here is the dataset that we use for this example: So, we have five regions with three input values: the average temperature in Fahrenheit, average rainfall in mm, and average humidity in %. As the output, we have two values, crop yields for raspberry and blueberry. The goal is to create the linear regression model that will from the given data be able to learn to predict correct crop yields. Technically our linear regression formula looks something like this: Ok, so let’s implement Linear Regression from scratch. First, we need to define input and output data and create PyTorch tensors from them:

Apart from that, we need to initialize weights and biases:

In this case, our model is simple, it needs to multiply input values with weights and add bias to it. We can define model as a function:

Operator @ in PyTorch represents matrix multiplication and t() function on a tensor, returns the transposed value of it. Another thing we need to do is to define the loss function. This function will tell us how well our model performs. There are multiple options that we can pick for this function, but we will use Mean Squared Error function. It does exactly what the name suggests, here is the formula: In essence, this function calculates differences between predicted values and real values, squares them and calculated the average. Here is the implementation:

Method `torch.sum` returns the sum of all the elements in a tensor, and the `.numel` method returns the number of elements in a tensor. Finally, we need to decide how to utilize this error in order to modify weights and biases. We do that using optimization technique Gradient Descent. In essence, what we are trying to do is calculate the best values for weights and biases that will minimize the loss. We do so by training our model in several epochs. During every epoch, we will calculate predictions, loss and gradients. Based on the gradients we will adjust the weights by subtracting a small quantity proportional to the gradient. Here is what training for 200 epochs looks like:

Note that everytime we call zero_() method in the end to reset the gradients. The output looks like this:

Note how the loss, ie. prediction error is getting smaller and smaller. In the end, we can compare predictions after 200 epochs and real values:

Considering that we are performing validation on the training set and that we still have some differences we can say that these results could be better. Anyhow, we managed to get close and in the process learn how to use PyTorch. Now, we can proceed and build more powerful tools like neural networks. ## Conclusion

In this article, we had a chance to check out the basics of PyTorch. We also saw how we can implement Linear Regression with it. In the next article, we will go one level higher and use it to implement simple neural networks.

Are you afraid that AI might take your job? Make sure you are the one who is building it.

STAY RELEVANT IN THE RISING AI INDUSTRY! 🖖 #### Nikola M. Zivkovic

CAIO at Rubik's Code

Nikola M. Zivkovic a CAIO at Rubik’s Code and the author of book “Deep Learning for Programmers“. He is loves knowledge sharing, and he is experienced speaker. You can find him speaking at meetups, conferences and as a guest lecturer at the University of Novi Sad.

Rubik’s Code is a boutique data science and software service company with more than 10 years of experience in Machine Learning, Artificial Intelligence & Software development. Check out the services we provide.