Data Science
10 Methods To Create Tensors In PyTorch
Index Of Contents
· Introduction
· Topics
· tensor()
· zeros()
· ones()
· full()
· arange()
· linspace()
· rand()
· randint()
· eye()
· complex()
· Conclusion
Introduction
PyTorch is an open-source Python-based library. It provides high flexibility and speed while building, training, and deploying deep learning models.
At its core, PyTorch involves operations involving tensors. A tensor is a number, vector, matrix, or any n-dimensional array.
In this article, we will see different ways of creating tensors using PyTorch tensor methods (functions).
tensor()
It returns a tensor when data
is passed to it. data
can be a scalar, tuple, a list, or a NumPy array.
In the above example, a NumPy array created using np.arange()
was passed to tensor()
method, resulting in a 1-D tensor.
We can create a multi-dimensional tensor by passing a tuple of tuples, a list of lists, or a multi-dimensional NumPy array.
When an empty tuple or list is passed into tensor()
, it creates an empty tensor.
zeros()
It returns a tensor where all elements are zeros, of specified size
(shape). The size
can be given as a tuple or a list or neither.
We could have passed 3, 2
inside a tuple or a list as well. It is self-explainable that passing negative numbers or float would result in run time error.
Passing an empty tuple or an empty list gives a tensor of size (dimension) 0, having 0 as its only element, whose data type is float.
ones()
Similar to zeros()
, ones()
returns a tensor where all elements are 1, of specified size
(shape). The size
can be given as a tuple or a list or neither.
Like zeros()
, passing an empty tuple or list gives a tensor of 0 dimensions, having 1 as the sole element, whose data type is float.
full()
What if you want all the elements of a tensor to be equal to some value but not only 0 and 1? Maybe 2.9?
full()
returns a tensor of shape given by the size
argument, with all its elements equal to the fill_value
.
Here, we have created a tensor of shape 3, 2
and fill_value
as 3. Here again, passing an empty tuple or list creates a scalar-tensor of zero dimension.
While using full
, it is necessary to give size
as a tuple or a list.
arange()
It returns a 1-D tensor, with elements from start
(inclusive) to end
(exclusive) with a common difference step
. The default value for start
is 0 while that for step
is 1.
The elements of the tensor can be said to be in Arithmetic Progression, with step
as the common difference.
Here, we created a tensor which starts from 2 and goes till 20 with a step
(common difference) of 2.
All the three parameters, start
, end
and step
can be positive, negative, or float.
While choosing start
, end
and step
, we need to ensure that start
and end
are consistent with the step
sign.
Since step
is set as -2, there is no way -42 can reach -22 (exclusive). Hence, it gives an error.
linspace()
It returns a 1-D dimensional tensor, with elements from start
(inclusive) to end
(inclusive). However, unlike arange()
, here, steps
isn't the common difference but the no. of elements to be in the tensor.
PyTorch automatically decides the common difference based on the steps
given.
Not providing a value for steps
is deprecated. For backward compatibility, not providing a value for steps creates a tensor with 100 elements. According to the official documentation, in a future PyTorch release, failing to provide a value for steps will throw a runtime error.
Unlike arange()
, linspace
can have start
greater than end
since the common difference is automatically calculated.
Since steps
here is not common difference, but no. of elements, it can only be a non-negative integer.
rand()
It returns a tensor filled with random numbers from a uniform distribution on the interval 0 (inclusive) to 1 (exclusive). The shape is given by the size
argument. The size
argument can be given as a tuple or list or neither.
Passing an empty tuple or list creates a scalar tensor of zero dimension.
randint()
It returns a tensor filled with random integers generated uniformly between low
(inclusive) and high
(exclusive). The shape is given by the size
argument. The default value for low
is 0.
When only one int
argument is passed, low
gets the value 0, by default and high
gets the passed value.
The size
argument only takes a tuple or a list. An empty tuple or list creates a tensor with zero dimension.
eye()
It returns a 2-D tensor with ones on the diagonal and zeros elsewhere. No. of rows is given by n
and columns is given by m
.
The default value for m
is the value of n
. When only n
is passed, it creates a tensor in the form of an identity matrix. An identity matrix has its diagonal elements as 1 and all others as 0.
complex()
It returns a complex tensor with its real part equal to real
and its imaginary part equal to imag
. Both real
and imag
are tensors.
The data type of both real
and imag
tensors should be either float
or double
.
Also, the size
of both tensors, real
and imag
should be the same, since the corresponding elements of the two matrices form a complex number.
Conclusion
We covered ten different ways to create tensors using PyTorch methods. You can go through the official documentation to know more about other PyTorch methods.
You can click here to go to the Jupyter notebook where you can play around with these methods.
If you want to learn more about PyTorch, check out this amazing course by Jovian.ai
Stay safe!