Introduction PyTorch Tensors — Tutorial 01

Aadhil imam | Data Scientist
4 min readAug 18, 2021

--

Hi , Today I am going to write about PyTorch , what is Tensors and what are the tensor arithmetic operations we can do. PyTorch is the fastest growing Deep Learning framework developed by Facebook’s AI Research lab in September 2016. It is free and open-source software released under the Modified BSD license.

What is Tensor ?

A PyTorch Tensor is basically the same as a numpy array . A scalar (a single number) has zero dimensions, a vector has one dimension, a matrix has two dimensions and a Tensor has three or more dimensions.

A tensor is a generalization of vectors and matrices and is easily understood as a multidimensional array.

PyTorch tensors are the data structures we’ll be using when programming neural networks in PyTorch. The Tensor does not know anything about deep learning or computational graphs or gradients, and is just a generic n-dimensional array to be used for arbitrary numeric computation. The biggest difference between a numpy array and a PyTorch Tensor is that a PyTorch Tensor can run on either CPU or GPU. To run operations on the GPU, just cast the Tensor to a CUDA datatype.

Creating a Tensor

We can initialize Tensors in various ways. First of lets create a single dimensional numpy array and covert to PyTorch Tensors. As I mentioned above Tensor is basically the same as a numpy array.

Output :

As above output we can see now the numpy array has been covert into One dimensional Tensor like wise we can make a simple tensor using an array.

Now lets create tensor using torch.tensor class. This called as factory function it build object for us.

Output :

From above code we made a two dimensional tensor as object. Now lets go into deeper to PyTorch Tensor.

Tensor Attributes.

Every Tensor attributes describe their shape, datatype, and the device on which they are stored.

Output :

For above shows the tensor attributes for the two dimensional tensor. In shape attribute describe the shape of the tensor because the tensor is a multi dimensional array , in dtype attribute specify the data type that contain in the tensor and device attribute specify the either CPU or GPU where the tensor data is allowed.

Now lets see how we can change the data type of a tensor and what are type of data we can use in a tensor.

Output :

as above output when we create a tensor , the function return tensor data type as int64 as default data type like wise PyTorch allows to change the data type of tensor to us.

we can see above code I used torch.dtype is an object that represents the data type of a Tensor. PyTorch has eight different data types from below table you can see what are the data types we can use for the tensors and each type of data type has CPU and GPU versions.

Tensor Data types

There are ways to build tensors using PyTorch built in functions that present in PyTorch.

PyTorch allows you to perform arithmetic operations on tensors. Now lets see how to do arithmetic operations of PyTorch tensors. These operations can be performed by using PyTorch arithmetic operation functions or python operands.

Addition

The addition of two tensors can be performed by using the + operand and can also be performed using the torch.add() function.

Subtraction

The subtraction of two tensors can be performed by using the - operand and can also be performed using the torch.sub() function.

Multiplication

The multiplication of two tensors can be performed by using the * operand and can also be performed using the torch.mul() function.

Division

The division of two tensors can be performed by using the / operand and can also be performed using the torch.div() function.

Note– Notice how division by 0 yields a value of inf – Infinity.

Exponent

The elements of a tensor can be raised to the power of their respective elements in the other tensor using the ** operator and can also be performed using the torch.pow() function.

Remainder

The remainder operation can be performed using the % operand and can also be performed by using the torch.remainder() function. This will calculate the remainder of elements in the first tensor after division by respective elements in the second tensor.

Conclusion

In this article I explained how we can create a PyTorch Tensor and what are arithmetic operation we can do using a Tensor. Thank you

--

--

Responses (1)