Introduction to the PyTorch library and some basic operations on tensors

Alifia Ghantiwala
3 min readMay 24, 2020

As the title suggests, this blog is an attempt to better understand the PyTorch library and it’s functionalities. What better way to test your understanding than to put it out to the world for honest feedback.

As part of the first lecture of the course : Deep Learning with PyTorch: Zero to GANs, our instructor Aakash and his team at Jovian.ml and FreeCodeCamp gave us a hands-on tutorial on what tensors are and how easy it is to explore the PyTorch library.

Below I will list down five basic operations along with their explanations which the PyTorch library allows us to do using tensors as input :-

1] torch.numel(input) → int

This PyTorch function returns an integer value which represents the number of values present in an (valid) input Tensor.

The input is a tensor of dimension (2,2) and has four elements\

Below is an example of an invalid tensor.A tensor is valid if all it’s elements have the same data type and are of the same size.

torch.tensor([[1, 2], [3, 4, 5]])

In this case all tensor functions will result in an error saying that the input tensor is not valid. As you can see the first element has size of two while the second has a size of three and violates the rule that tensors should have it’s internal elements of the same size.

2]torch.abs(input, out=None) → Tensor

The function performs an element wise absolute logic on the input tensor.

In the example above we assign random numbers (through torch.randn() function) to the tensor a. We can see that it includes both positive as well as negative numbers. On performing torch.abs on the input tensor “a” we get the following:-

3]torch.trunc(input, out=None) → Tensor

Use this function to truncate your floating point tensor to an integer tensor. It returns a new tensor with the truncated integer values of the elements of input. This is further explained with the following example:-

On using the function on our input tensor “a” we are returned with a tensor which has truncated numbers representing only the integer part of the number.

4]torch.where(condition, x, y) → Tensor

This function acts as the if-else logic which we use in programming. The input parameters are condition, a tensor ‘x’ and a tensor ‘y’. If the condition holds true for the particular element in input tensor we pick a value from x and if the condition is false for that particular element we pick the value from y. This is further illustrated with the following example.

5] torch.mean(input) → Tensor

Returns the mean value of all elements in the input tensor. This could be further understood by the following example:-

The only catch with this function which might not actually be a catch but a fundamental requirement is that the input tensor cannot be an integer it must be a float point tensor. If an integer tensor is passed as input it results in the following error:-

In conclusion, I would say that I referred to the official documentation for torch.Tensor: https://pytorch.org/docs/stable/tensors.html and you could do the same to learn more about some amazing functions which the library has to offer. Thank you for reading through, any feedback would be highly appreciated, and do check out the course it seems promising to me.

Until next time.

--

--