PyTorch Tensor Methods – How to Create Tensors in Python

[ad_1]


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).

Topics we’ll cover

  • tensor
  • zeros
  • ones
  • full
  • arange
  • linspace
  • rand
  • randint
  • eye
  • complex

The tensor() method

This method 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 that was created using np.arange() was passed to the 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.

The zeros() method

This method 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 a float would result in a 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.

The ones() method

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 dimension, having 1 as the sole element, whose data type is float.

The full() method

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 a 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 with the 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.

The arange() method

This method 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 common difference.

Here, we created a tensor which starts from 2 and goes until 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.

The linspace() method

This method 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 number 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 backwards 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 a start greater than end since the common difference is automatically calculated.

Since steps here is not a common difference, but the number of elements, it can only be a non-negative integer.

The rand() method

This method 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.

The randint() method

This method 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.

The eye() method

This method returns a 2-D tensor with ones on the diagonal and zeros elsewhere. The number 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.

The complex() method

This method 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 the 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’ve 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 on freeCodeCamp’s YouTube channel.

Stay safe!

Read More …

[ad_2]


Write a comment