A Beginners Guide For Creating PyTorch Tensors

Saurabh Palhade
GDSC DYPCOE
4 min readMay 27, 2020

--

In this article, we will explore some basic PyTorch tensor functions, To begin with let us first understand what is PyTorch and what are Tensors.

PyTorch is a python based library built to provide flexibility as a deep learning development platform, also used for applications such as computer vision and natural language processing developed by facebook’s AI Research lab (FAIR), and A PyTorch Tensor is conceptually identical to a NumPy array, A tensor is a number, vector, matrix or an n-dimensional array, and PyTorch provides many functions for operating on these Tensors.

Now that you know what a tensor is, Here’s the list of five interesting functions we will go through :

  • torch.tensor()
  • torch.randn()
  • torch.linspace()
  • torch.reshape()
  • torch.sum()

let us see how to create a PyTorch tensor. To create a tensor the most basic function is: torch.tensor()

torch.tensor()

We start by importing the PyTorch library

Now we are all set to play with tensors, Let’s create a tensor with a single number:

4. is a shorthand for 4.0. It is used to indicate to Python (and PyTorch) that you want to create a floating-point number.

Let’s try creating slightly more complex tensors:

The datatype of tensor if not specified will be of data,but we can specify the datatype using dtype parameter i.e dtype=torch.float64 .Tensors can have any number of dimensions, and different lengths along each dimension. It can be constructed from a Python list,Initial values for the tensor can be a list, tuple, NumPy NDarray, scalar, and other types.here we have created tensors t2 ,t3 and t4 of one,two and three dimensions respectively.

As you have seen that using torch.tensor() we have created a tensor using specific values,but to create a tensor filled with random numbers we can use a function torch.randn().

torch.randn()

so, torch.randn() Returns a tensor filled with random numbers from a normal distribution with mean 0 and variance 1. here in example 1, we have created a x tensor of size 4 (1D) with the values between 0 and 1.We can create a tensor of any size by passing the size as an argument to the function, as here in example 2 you can see we created a y tensor of size 3 by 3 (2D). This function is very useful when we want to initialize a tensor of big size with random values.

Now let’s see another important tensor function:

torch.linspace()

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

This returns a one-dimensional tensor of steps equally spaced points between start and end.

In this function we have to specify the starting,ending and step parameters as we will be getting a tensor of equally spaced values between starting and ending parameters with its size equal to the step parameter. Here in example 1 we have created a tensor of size 10(steps),with equally spaced values from 1 to 10.

Here in example 2 notice that how the values of tensor got rounded to int as we defined the data type,the default tensor type will be float.

Our next tensor function is:

torch.reshape()

This function basically reshapes the tensor into another shape.

In this function we pass the tensor which we want to reshape as the first parameter and second parameter is the size in which we want to reshape the tensor. In example 1 we converted the 1D tensor of size 10 into a 2D tensor of size 2 by 5 using torch.reshape() function.

Here In Example 2 we reshaped the 2D tensor of size 4 by 4 into a another 2D tensor of size 8 by 2.

Take a look at example 3, This is one of the most common mistakes, always remember while giving the reshape size you must check that the original tensor and the reshaped tensor should have same no of elements, as in this example the input x tensor is of size 4 by 4 and will have 16 elements, and our reshape size is 2 by 2 which will be having only four elements, so we cannot convert a 4 by 4 tensor into 2 by 2, hence we got a traceback here.

Now let’s see another tensor function:

torch.sum()

This function returns the sum of all elements in the input tensor.

We pass the tensor to the torch.sum() function as a parameter,In example 1 we got a sum of all the elements in the tensor and in In example 2 we got the sum of the elements of the tensor according to the dimension specified,row-wise and column-wise.

Don’t try to add two tensors using this sum function otherwise you will end up getting a traceback as shown in example 3,we can directly add two tensors in PyTorch with arithmetic operator i.e x+y. This function can be used when we need to get the sum of all the elements of tensor,and also when we need the column-wise and row-wise sum of elements.

So this brings us to the end of this article,we have seen some basic PyTorch tensor functions which we can use while performing tensor operations,there are many other useful functions as well,if you are reading this i hope this article helped you to gain some knowledge about PyTorch tensor operations and encouraged you to learn more about tensors,to check out more functions please visit official documentation of PyTorch.

Thank you so much for taking the time to read this! I hope you enjoyed reading it .you can connect with me on LinkedIn and Twitter

Reference links:

Documentation for PyTorch

--

--