DEEP LEARNING

5 PyTorch Functions

Tanuj Shrivastava
3 min readAug 27, 2020
https://analyticsindiamag.com/wp-content/uploads/2020/02/Pytorch.png

PyTorch is a deep learning library as popular as Tensorflow , helps us to bulid deep learning models . It consist of many inbuilt functions and some of them will be discussed here .

First we will import torch :

Function 1 : torch.cat

Concatenates the given sequence of seq tensors in the given dimension. All tensors must either have the same shape (except in the concatenating dimension) or be empty.

torch.cat(tensors, dim=0, out=None)

In this example we are concatenating two (3,3) dimentional metrics. Here torch.zeros will fill all the values with zeros and torch.randn will fill in normally distributed values where the mean is 0 and standard deciatio in 1

In this example we are concatenating three (4,3) dimentional metrics. Here torch.ones will fill all the values with 1.

In this example both the matrics are of different dimentions so it is not possible to concate them .

Function 2 : torch.argmax

Returns the indices of the maximum value of all elements in the input tensor.

torch.argmax(input, dim, keepdim=False)

In this example there is a tensor of size (2,2) and we have captured the index which consist of maximum value .

In this example we have change the default value of dim parameter from 0 to 1 .

Here t1 is a single (2,2) matrics so the value of dim must be 0 or 1 but we have specified 2 so we got a dimention error .

Function 3 : torch.argsort

Returns the indices that sort a tensor along a given dimension in ascending order by value.

torch.argsort(input, dim=-1, descending=False)

Here we are sorting the (5,5) tensor indices .

Here we have set the descending parameter to True in order to sort in descending order .

Here we have passed descending = 1 but we need to specify any boolena value .

Function 4 — torch.is_nonzero

Returns True if the input is a single element tensor which is not equal to zero after type conversions. i.e. not equal to torch.tensor([0.]) or torch.tensor([0]) or torch.tensor([False]).

If we pass zero then it will return a False .

If we pass any non zero value it will return True

Throws a RuntimeError if torch.numel() != 1 (even in case of sparse tensors).

Function 5 — torch.linspace

Returns a one-dimensional tensor of steps equally spaced points between start and end.The output tensor is 1-D of size steps.

torch.linspace(start, end, steps=100, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)

Here we have created a linearly spaced tensor rannging from 1 to 5 which consist of default 100 steps .

Here we have specified the parameters which consist of stating value , end value and the number of steps to take .

We need to pass at least 2 parameters for linspace to execute .

Conclusion

In this article we have discussed 5 random pytorch functions which allows us to perform certain task with tensors and gain some more understanding about how tensor performs . We took the refrence from the official documentation of pytorch : https://pytorch.org/docs/stable/tensors.html , and for more indepth defination of every functuon we took the help from : https://www.geeksforgeeks.org/ .

Thank You

--

--

Tanuj Shrivastava
Tanuj Shrivastava

Written by Tanuj Shrivastava

Data science , Machine Learning and Deep Learning enthusiast .