- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Creating a Tensor in Pytorch

## What is Pytorch?

Pytorch is a Python - based framework in machine learning primarily used for scientific computations and to build deep learning models.

A Tensor is a popular concept in Machine learning and Data science. Tensors are a generalized term for matrices and vectors. In simple words, Tensors can be anything - a n-dimensional array, a vector, or a matrix. In this article, we will be looking at the multiple methods used to create Tensors in Pytorch.

## Method of Creating Tensors

Tensors can be created in multiple methods. Some of the important methods are listed below.

Creating a tensor using ones

Creating a tensor using zeros

Creating a tensor using arange

Creating a tensor using rand()

Creating a tensor using full()

Creating a tensor using linspace

Creating a tensor using randint()

Creating a tensor using the eye() method

Creating a tensor using the tensor method

creating a tensor using complex method

As an initial step, we must import the torch first before getting to the different methods of creating Tensors.

### Syntax

#importing torch import torch

## Approach 1: Creating a tensor using ones()

Using the ones() function, the tensor is created such that all its elements are set to 1.

Let us write a code in Python programming to create a tensor using ones() function.

### Example

#creating a tensor using ones() tensor_using_ones = torch.ones(4,4,5) #prints the created tensor print(tensor_using_ones)

### Output

We can see from the below output that the newly created tensor has all entries set to “1” in the shape (4,4,5).

tensor([[[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]], [[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]], [[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]], [[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]]])

## Approach 2: Creating a tensor using zeros()

We use the zeros() function to create a tensor which has all its elements set to 0. Using this, the values can be passed either as a list, a tuple or empty values. Tensors can be created in any of these ways.

Using the zeros() function, the tensor is created such that all its elements are set to 0.

### Example

#creating a tensor using zeros() tensor_using_zeros = torch.zeros(4,4,5) #prints the created tensor print(tensor_using_zeros)

### Output

We can see from the below output that the newly created tensor has all entries set to “0” in the shape (4,4,5).

tensor([[[0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.]], [[0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.]], [[0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.]], [[0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.]]])

## Approach 3: Creating a tensor using arange() – Example 1

The arange() function is used to create a 1-dimensional (1D) tensor.

### Syntax

The arange() function has the following syntax.

torch.arange(start,end,step-size)

where,

start - is inclusive

end - is exclusive of the specified shape

We write a code in Python to create a 1-D tensor with the shape specifications (5,30,2).

### Example

#creating a tensor using arange tensor_using_arange = torch.arange(5, 30,2) #prints the created tensor print(tensor_using_arange)

### Output

Here, a 1-dimensional tensor is created with the initial element as 5, ending element as 29 (i.e 30-1) with a common difference (step) of ”2”.

tensor([ 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29])

## Approach 4: Creating a tensor using arange() - Example 2

Let us look at another example of creating a tensor in Pytorch where the start value is negative i.e -5 and a step of “1”.

We write a code in python to create a 1-D tensor with the shape specifications (-5,25,1).

### Example

#creating a tensor using arange tensor_using_arange1 = torch.arange(-5, 25,1) #prints the created tensor print(tensor_using_arange1)

### Output

Here, a 1-dimensional tensor is created with the initial element as -5, ending element as 24 (i.e 25-1) with a common difference (step) of ”1”.

tensor([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24])

## Approach 5: Creating a tensor using rand()

Another commonly used method to create a Tensor in Python is the rand() method. A Tensor containing random numbers will be created using the rand() method for the specified shape.

### Syntax

The syntax of the rand() method is shown below −

torch.rand(shape)

### Example

Now to understand the creation of a Tensor using the rand() method, let's write a code in Python programming.

#creating a tensor using rand() tensor_using_rand = torch.rand(5, 5) #prints the created tensor print(tensor_using_rand)

### Output

We can see from the below output, a Tensor of shape 5X5 is created using the rand() method. The elements inside the tensor are random numbers of the Uniform distribution.

tensor( [[0.2000, 0.6068, 0.3968, 0.6269, 0.8587], [0.1754, 0.5050, 0.8569, 0.5139, 0.9026], [0.2634, 0.6543, 0.3015, 0.6873, 0.2214], [0.9257, 0.6045, 0.4763, 0.5107, 0.7925], [0.7465, 0.0279, 0.4937, 0.0989, 0.8806]])

## Approach 6: Creating a tensor using full()

The full() method in Tensor is used whenever the elements in a Tensor are preferred to be the same number.

### Syntax

The syntax for the full() method is as follows.

torch.full(size, fill_value, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)

where,

fill_value - is the value (same number) which we want to be in the Tensor.

Suppose, if we want to create a Tensor having the same number (4) as all the elements. We can write the code in Python programming as shown below. The fill_value is specified as 4. That is the value that will be the common element in the Tensor.

### Example

#creating a tensor using full method tensor1 = torch.full((5,5,4) #prints the created tensor print(tensor1)

### Output

For the above code, we give a Tensor with 5X5 matrix with 4 as all elements.

tensor([[4, 4, 4, 4, 4], [4, 4, 4, 4, 4], [4, 4, 4, 4, 4], [4, 4, 4, 4, 4], [4, 4, 4, 4, 4]])

## Approach 7: Creating a tensor using linspace

### Syntax

Linspace() method has the following syntax.

linspace (start, stop, num, endpoint=True, retstep=False, dtype=None, axis=0)

where,

low - starting element(inclusive)

high- ending element(inclusive)

num- number of elements to be generated in the Tensor

### Example

We use the Linspace () method to create a 1- dimensional (1D) tensor. The shape we specify for example, in this case is (2,20,4). In our example, 2 is the starting element, 20 is the ending element and 4 is the number of the elements to be present in the 1-D Tensor.

#creating a tensor using linspace tensor_using_linspace = torch.linspace(2, 20, 4) #prints the created tensor print(tensor_using_linspace)

### Output

We can see that the above code yields the output below where a 1-D Tensor is created with 4 elements.

tensor([ 2., 8., 14., 20.])

## Approach 8: Creating a tensor using randint()

The randint() method creates a Tensor with random integers generated according to the specified shape.

### Syntax

torch.randint(low,high,shape)

where,

low - starting element(inclusive)

high- ending element(exclusive)

shape - shape of the Tensor

### Example

We write a Python code using the randint() methods as shown below.

#creating a tensor using randint tensor_using_randint = torch.randint(10,100, (2,2)) #prints the created tensor print(tensor_using_randint)

### Output

In the below output, we see a Tensor with random elements generated between the shape (10,100,(5,5)). Here, we get a Tensor with random elements between 10 and 99 in the shape of a 5X5 matrix.

tensor([[71, 32, 56, 34, 21], [37, 71, 98, 63, 40], [41, 45, 27, 41, 50], [51, 42, 43, 71, 14], [87, 52, 77, 88, 85]])

## Approach 9: Creating a tensor using eye()

The eye() method is useful when we want to create a 2D Tensor where the elements in the diagonal are 1 and zeros as the rest of the elements.

### Syntax

torch.eye (shape)

Now, we write a Python code using the eye() method.

### Example -1

#creating a tensor using eye tensor1_using_eye = torch.eye(4, 4) #prints the created tensor print(tensor1_using_eye)

### Output

We have now created a 2D 4X4 Tensor using the eye() method.

tensor([[1., 0., 0., 0.], [0., 1., 0., 0.], [0., 0., 1., 0.], [0., 0., 0., 1.]])

Lets see another example of creating a tensor using the eye function.

### Example-2

#creating a tensor using eye tensor2_using_eye = torch.eye(3) #prints the created tensor print(tensor2_using_eye)

### Output

We have now created a 2D 3X3 Tensor using the eye() method as shown in the below output.

tensor([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]])

## Approach 10: Creating a tensor using Complex() method

The complex () method is the last method we are going to look at. It is used to create a Complex Tensor where the created Tensor will have both imaginary part and real part.

### Syntax

The complex() method has the following syntax.

torch.complex(real part, imaginary part)

### Example

The code for the complex() method is written below.

real_tensor = torch.rand(4,5) print(real_tensor) imaginary_tensor = torch.rand(4, 5) print(imaginary_tensor) tensor_using_complex = torch.complex(real_tensor, imaginary_tensor) #prints created tensor print(tensor_using_complex )

### Output

Thus, we have created a complex Tensor having the imaginary part and real part of shape 4X5.

Tensor([[0.8284, 0.7833, 0.4952, 0.7634, 0.9098], [0.6373, 0.3428, 0.0352, 0.6095, 0.8066], [0.7292, 0.0703, 0.3846, 0.6653, 0.1924], [0.2920, 0.2676, 0.4669, 0.2841, 0.5974]]) tensor([[0.5236, 0.9037, 0.7622, 0.7044, 0.3362], [0.8543, 0.6088, 0.4882, 0.0563, 0.9318], [0.7075, 0.1176, 0.6272, 0.3318, 0.3751], [0.6317, 0.1482, 0.3772, 0.6941, 0.9126]]) tensor([[0.8284+0.5236j, 0.7833+0.9037j, 0.4952+0.7622j, 0.7634+0.7044j, 0.9098+0.3362j], [0.6373+0.8543j, 0.3428+0.6088j, 0.0352+0.4882j, 0.6095+0.0563j, 0.8066+0.9318j], [0.7292+0.7075j, 0.0703+0.1176j, 0.3846+0.6272j, 0.6653+0.3318j, 0.1924+0.3751j], [0.2920+0.6317j, 0.2676+0.1482j, 0.4669+0.3772j, 0.2841+0.6941j, 0.5974+0.9126j]])

## Conclusion

From this article, we have learnt multiple ways to create tensors in Pytorch. We recommend you to take a look at our articles as well.