PyTorch Basics for Data Science
By: Nabh Mehta
Tags: Data Scientist, Python, Data Science, Deep Learning, Pytorch

PyTorch is an open source machine learning library based on the Torch library used primarily for applications in computer vision and natural language processing.

Developed and distributed by Facebook’s AI Research lab (FAIR), PyTorch is largely used by developers within the Python programming environment. However, it also has C++ interface.

PyTorch is seen by industry as a TensorFlow competitor since both have similar capabilities in terms of speed and performance. The Initial cost of learning PyTorch might seem to be high to some people but it does give you more flexibility if you want to know the mechanics of the codes. The developer community seem to be growing strong with major cloud native companies adopting PyTorch.

At the end it depends on the client what they want to have as a preference for their development needs.

What is an array in python?

An array is a data structure that stores values from same data type.

The difference between an array and a list:

a. List: Can contain values of different data types

b. Array: Can contain values of same data type only

Defining an array in python using numpy

What is a tensor?

It is a multi-dimensional matrix containing elements of a single data type.

It is a generic n-dimensional array to be used for numeric computation.

A tensor image showing it to be a n-D

The biggest difference between a numpy array and a PyTorch tensor is that the latter can use CPU or GPU for the computations.

How to create a tensor in Pytorch?

First we will create an array and try to cast it into a tensor using functions given by torch.

Import point to note here is, both the method will do create tensors but any change in the array will reflect in the tensor too. That means we are creating a copy or a direct link between an array and a tensor.

How to avoid using a direct link between an array and a PyTorch Tensor?

Creating a Tensor of type Float

We saw earlier that we were creating tensors which were of type integer. Now what we needed to have tensors of type Float. we can do it in 2 ways as follows:

How to block memory for tensor?

Numpy analogous functions for PyTorch

How to change data type length?

Tensors for random functions

Uniform distribution

Normal Distribution

Random Integers

Automatic shaping tensors

The below function creates tensors of random numbers with a shape of the tensor variable passed to it.

Setting up SEED

To have the reproducibility of our random numbers we would setup seed value for our tensors.

Seed value should be passed in the same cell as the random tensor variable, otherwise values will change in another cell if you call random function without seed there.

Tensor Operations

Get the slice of a column

View/Re-shape

From the usage point of view I did not see much of a difference between view and re-shape. However, the link here for the review.

If you assign a new value one variable in view, all the other variables which are a copy of that variable will also have the change.

Infer the dimension

When you want PyTorch to determine the dimensions of a tensor variable, we can use infer functionality which allows us to view/re-shape the tensor.

The total size after multiplication should be same as the original size otherwise it will give you error.

In case described earlier, 2 times 5 [NxM] was 10, the original size. Here since 3 times ‘fill-in-the-blanks’ does not give you original size value which is 10 so it gives an error.

You can use it to determine first N value of [NxM] matrix as well.

Arithmetic operations

Here we will see a simple addition operation. The function torch.tensor() will do the same operation as the regular ‘a + b’ operation we do.

Permanent re-assignment of arithmetic operation

There 2 ways in which we can do arithmetic operations. One way is to follow regular functions available in PyTorch to just go ahead and perform those. On the other hand, if you use ‘<arith>_’ operation, it will permanently assign the changes to the variable.

Matrix Multiplication

While doing Matrix multiplication keep in mind that the final shape of 2 matrices having shape [n*m] & [m*p] will be [n*p].

Condition for matrix multiplication is no of column in matrix 1 should match no. of rows in matrix 2.

We can do the operation in 2 ways as below:

Euclidean Norm

This function gives us a non-negative value which is square root of sum of squares of the given elements.

To find number of elements in a matrix:

References:

About Author

LinkedIn: https://www.linkedin.com/in/nabh-sanjay-mehta/