Machine Learning — PyTorch

Original article was published on Deep Learning on Medium

Machine Learning — PyTorch

Explore Some PyTorch functions for tensor operations

PyTorch : It is an open source machine learning library based on the Torch library (which is based on Lua Programming Language), developed by Facebook’s AI Research lab.

It can also use the power of graphics processing unit and so it is one of the preferred library for deep learning. TensorFlow is another great library for tensor operations in deep learning.

PyTorch is easy to learn and integrates easily with other python packages makes it a simple choice for researchers to use.

Tensor : A tensor is a generalization of scalar, vectors and matrices or we can say these are the special cases of a tensor and can easily understood as a multidimensional array. All values in a tensor hold identical data type with a known (or partially known) shape.

Syntax to create tensor with PyTorch :

This is how you can create n-dimensional tensors with PyTorch.

Here are some of the PyTorch functions, we are going to explore:

view, flip, ne, topk, bincount

1. torch.Tensor.view(*shape)

It changes the shape of a tensor. In argument it requires the shape for output tensor. It returns a new tensor with the same data as the self tensor but of a different shape.

Example 1: Created a 1-D tensor having different 8 values, then in the view function passed 2, 2, 2 which means changing the shape of the tensor having 2 element in each axis.

Example 2: Created a 1-D tensor having values 1–8, then in the view function passed 2, 4 which means changing the shape of the tensor having 2 element in first axis and 4 element in the second axis.

Example 3: In this example, the input tensor is having 9 elements, then in the view function we passed 2, 2, 2 as argument. The error is because it’s not possible to change the shape such that 2 element in each axis.

2. torch.flip(input, dims)

It reverse the order of a n-D tensor along given axis in dims. It requires two argument, the first one is the input tensor and second is the list of axis you want to flip and returns the resultant tensor.

Example 1: In this example, input is a 2-D tensor which is 1st argument in flip function. The second argument is a list containing 1 which means that 2nd axis is to be flipped, the output is a resultant tensor.

Example 2: In this example, input is a 3-D tensor which is 1st argument in flip function. The second argument is a list containing 0, 1 which means that 1st and 2nd axis is to be flipped, the output is a resultant tensor.

Example 3: In this example, input is a 1-D tensor which is 1st argument in flip function. The second argument is a list containing 1 which means that 2nd axis is to be flipped, but as we can clearly see that the input tensor is having only one axis so the operation trying to perform is impossible and that’s why it is giving error.

3. torch.ne(input, output)

This function is used to check element-wise that the two tensors in argument(input and output) are unequal. input != output . Both tensors must be of the same length. It return a tensor of same length as the input containing True and False, True for unequal and False for equal.

Example 1: In this example, we have taken two tensors [1, 3, 4, 9, 5] and [1, 3, 5, 9, 0], then pass them to the function, the result is a tensor of True and False → [False, False, True, False, True].

Example 2: In this example, we have taken two 2-D tensors, then pass them to the function, the result is a tensor of True and False of same length,True where the elements are unequal and False where elements are equal.

Example 3: This example gives an error, because the number of elements in both the tensors are unequal. First tensor is having more elements than second tensor.

4. torch.topk(input, k, dim=None, largest=True, sorted=True, out=None)

This function returns the k largest elements of the given input tensor along a given dimension. If dim is not given, the last dimension of the input is chosen. If largest is False then the k smallest elements are returned.

Example 1: In this example, we have taken a 1-D tensor then pass it to topk function, we have given k=3 which means we want top 3, largest= True which means we need 3 largest numbers and also we kept sorted=True which means the output should be sorted in descending order. We got indices of top 3 sorted largest number as well.

Example 2: In this example we have taken the 2-D tensor. Taken k=2, largest=False and sorted=True, and not taken dim, so it will automatically take the last dim. And so it sort it in ascending order from each inner list and gives the top 2 as output along with their indices.

Example 3: In this example, we have kept the same tensor from previous example and taken largest=True, k=4 with dim=0. Now in dim 0 there are only 3 lists present, and we asked for 4 largest element so it is giving error. If we have taken k=1, then it would have return [30, 24, 5, 25, 15] as output because this list is having the largest element out of all.

5. torch.bincount(input, weights=None, minlength=0)

This function is used to count the frequency of each value in an array of non-negative integers. It looks for largest integer in input array or minlength and then produce an output array having frequency of each number in input from 0 to the largest.

Example 1: In this example the input tensor have the largest element 9, so the output is an array having frequency of all element in input from 0 to 9. for example- 4 comes twice so in the output array at index 4, the value is 2.

Example 2: In this example, suppose we know that the input array is having values between 0 to 11. So we can decide the length of output array which should be having 12 element for 0–11. So we kept another argument minlength=12 in the function, which will generate the array of minimum 12 element in output. It output the frequency as 0 for the elements which are not there in input.

Example 3: This function can only work on 1-D non negative integers. In our case the input is having negative and float values. So it gives the error.