Data Science

Five Interesting PyTorch Tensor Functions

An Introduction to PyTorch

BHUPENDRA SINGH@IIT Indore

--

PyTorch is an open-source machine learning library based upon Torch library, used for a lot of applications such as Computer vision, Natural Language Processing, and other many applications related to AI It was primarily developed by Facebook’s AI Research lab

A number of pieces of Deep Learning software are built on top of PyTorch including Tesla Autopilot, Uber’s Pyro, and a lot of others

Pytorch provides two high-level features :

Tensor computing with great efficiency using GPUs (Graphics Processing Units)
Deep neural networks built on a tape-based automatic differentiation system

Now there is a list of Functions: -

1 . torch.abs

2 . torch.add

3 . torch.arange

4 . torch.eye

5 . torch.take

Now we are going to go through every function step wise step

Function 1: torch.abs

torch.abs function computes the absolute value of each element in the input and input should be a tensor

I think we can understand the function by seeing the code cell ok. Let’s look at it

Here we can see how the output is changing from t1 tensor to new_t named tensor. I mean After taking absolute of all values we get new_t. All values of the t1 tensor are converted to absolute values.

The absolute of positive number is the number itself and the absolute of negative number is the negative of that number.

Let’s take look at when this function is going to show an error

Function torch.abs is going to work fine always except a case when you do not pass the correct tensor

Use this function when you need a tensor having all values positive but the tensor that you have may have some negative numbers

that are all about tensor.abs function now go ahead and read next function

Function 2: torch.add

We can use this function to increment or decrement all values of a tensor by a fixed number

Here is a description of how can you pass parameters to your add function

torch.add(input, other, *, out=None)

  • Adds the scalar other to each element of the input and returns a new resulting tensor.
  • input -> tensor
  • other -> the number to be added to each element of input
  • out -> is an optional parameter, it is output tensor

torch.add(input,other,alpha=1,out=None)

  • Each element of the tensor other is multiplied by the scalar alpha and added to each element of the tensor input. The resulting tensor is returned.
  • The shapes of input and others must be broadcastable.
  • input (Tensor) –> the first input tensor
  • other (Tensor) –> the second input tensor
  • alpha -> scaler value to multiply with other

Now let’s explore this function using code example

Value 10 is added to each element of tensor t1 and get new tensor t2

Now take another example to show how we can pass parameter in a different way to this function

Here alpha=10 multiply with each element of other tensor added to input_t and then get output

let’s take a look at when this function may cause an error

If the input is of type FloatTensor or DoubleTensor, the other must be a real number or tensor of real numbers, otherwise, it should be an integer.

that was all about torch.add function now you can practice this function in your jupyter notebook, so lets the go-ahead

Function 3: torch.arange

We use this torch.arange function When we need to create a tensor of values starting from a certain point to end at and point with constant steps

torch.arange(start=0, end, step=1, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor

this going to create a tensor of size floor values of [(end-start)/step] and having values in the interval [start, end)

start (Number) — the starting value for the set of points. Default: 0.
end (Number) — the ending value for the set of points
step (Number) — the gap between each pair of adjacent points. Default: 1.

let’s understand by code cell

By default, step is 1 so torch.arange function creates a tensor of values from 2 to 33

let’s explore another example where taking a step as 2

So here we get a tensor of values from 2 to 34 by step of 2

By default starting point is 0 and the step is 1

Now let’s take a look at when this function is going to throw an error

This error is suggesting that the upper bound(endpoint) should be greater than the lower bound(starting point)

now you can explore more in your notebook by doing experiments, as much as an experiment you do, the more you will learn

Now, this is time to go to the next function.

Function 4: torch.eye

The function torch.eyereturns a 2-D tensor with ones on the diagonal and zeros elsewhere. In calculus, we call this tensor identity matrix, but no problem if you do not know about the matrix that’s fine.

torch.eye(n, m=None, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)

  • n (int) — the number of rows
  • m (int, optional) — the number of columns with the default being n
  • out (Tensor, optional) — the output tensor.
  • dtype (torch.dtype, optional) — the desired data type of returned tensor. Default: if None, uses a global default (see torch.set_default_tensor_type()).
  • layout (torch.layout, optional) — the desired layout of returned Tensor. Default: torch.strided.
  • device (torch.device, optional) — the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.
  • requires_grad (bool, optional) — If autograd should record operations on the returned tensor. Default: False.

If you are not understanding the above lines, no problem, we are going to understand torch.eye by looking at code cells

look at tensor t1 created by tensor.eye function. This tensor has the same number of columns and rows and diagonal entries as 1 and rests as 0

let’s look at another example

If the no of columns and rows are not equal then the function ignores the additional columns/rows and returns the output as shown above.

Now let’s look where this function is going to throw an error

n value should be positive I mean greater than zero

Now it is your turn to explore more about this function by doing an experiment with your code in jupyter notebook

Now let’s explore another function which is the last of this blog

Function 5: torch.take

We are using this function when we need to get elements at more than 1 indices at one time

The function torch.take takes the input tensor and returns the elements based on the index values passed.

torch.take(input, index)

  • input (Tensor) — the input tensor.
  • indices (LongTensor) — the indices into tensor

Note — The input tensor would be converted on 1D tensor before assiging the index values

let’s understand how to use this function using code

\

In the above code, we get the element at the 2nd index of tensor t1

let’s take another example

In the above code, we pass an input tensor and another tensor of indices and get tensor of elements of given indices

Now we are going to see when this function is going to throw an error

The tensor of indices should have values in range of size of input otherwise function is going to throw an error.

So that is all about tensor.take function, now you can explore more by doing the experiment in your jupyter notebook.

Summary:

We have explained 5 interesting and very useful functions from the PyTorch library related to tensor.

Here is a quick summary of all functions.

1: torch.abs
torch.abs function computes the absolute value of each element in the input and input should be a tensor.

2: torch.add
We can use this function to increment or decrement all values of a tensor by a fixed number.

3: torch.arange
this going to create a tensor of size floor values of [(end-start)/step] and having values in the interval [start, end).

We use this torch.arange function When we need to create a tensor of values starting from a certain point to end at and point with constant steps.

4: torch.eye
The function torch.eye returns a 2-D tensor with ones on the diagonal and zeros elsewhere.

5: torch.take
The function torch.take takes the input tensor and returns the elements based on the index values passed.
We are using this function when we need to get elements at more than 1 indices at one time.

Future Work

Here are some suggestions on what to do next.

Now you should to PyTorch documentation and explore the more useful function and try them out in your Jupyter notebook

References

1. PyTorch documentation

2. Jovian.ai Tutorials

3. Particular video on PyTorch

If you love this and found something interesting you can give one clap for me and you can share it with your friends

Thanks for reading and Best of Luck.

--

--