Tensors Introduction

Tensors are the key components of Pytorch. We can say PyTorch is wholly based on the Tensors. In mathematics, a rectangular array of number is called metrics. In NumPy library, these metrics called ndaaray. In PyTorch, it is known as Tensor. A tensor is an n-dimensional data container. For example, In PyTorch, 1d-tensor is a vector, 2d-tensor is a metrics, 3d- tensor is a cube, and 4d-tensor is a cube vector.

Torch provides tensor computation with strong GPU acceleration. It is essential that we get familiar with the tensor data structure to work with PyTorch. It will serve as a fundamental prerequisite before neural network implementation.

In Deep learning, Tensor is the key part, and we can see so many discussion around Tensor. Even it appears in the name of Google’s main machine learning library, i.e., TensorFlow.

In this tutorial, we will discuss what tensors are and how to perform operations and to manipulate them in python with numpy.

“In the general case, an array of numbers arranged on a regular grid with a variable number of axes is known as a tensor.”Below is the diagram that describes the Tensor’s dimensions in a very efficient way.

Tensors Introduction

Now let’s get a little bit knowledge about the notation of Tensors

The tensor notation is similar to the metrics notation. A capital letter represents the tensor, and the lower letter with subscript integer represents scalar values within the tensor.

Tensors Introduction

In the field of Physics and Engineering, as a tool, tensor and tensor algebra widely used. We can say it is a set of techniques in machine learning in the operation and training of deep learning models can be described regarding tensors.

How to create Tensor?

There are three ways by which we can create the Tensor. Each one has a different way to createTensor and use a different method. Tensors are created as

  1. Create Tensor from an array
  2. Create Tensor with all ones and random number
  3. Create Tensor from numpy array

Let see how Tensors are created

Create Tensor from an array

In this, you have first to define the array and then pass that array in your Tensor method of the torch as an argument.

For example


  1. Import torch  
  2. arr = [[3, 4], [8, 5]]   
  3. pytensor = torch.Tensor(arr)  
  4. print(pytensor)  

Output:tensor ([[3., 4.],[8., 5.]])

Tensors Introduction

Create Tensor with the random number and all one

To create a random number tensor, we have to use method rand() and to create a tensor with all ones you have to use method ones () of the torch. To generate random number one more method of the torch will be used with rand, i.e., manual_seed with 0 parameters.

For example


  1. import torch  
  2. ones_t = torch.ones((2, 2))   
  3. torch.manual_seed(0)  //to have same values for random generation  
  4. rand_t = torch.rand((2, 2))  
  5. print(ones_t)  
  6. print(rand_t)  

Output:

Tensors Introduction

Create a Tensor from numpy array

To create a tensor from the numpy array, we have to create a numpy array. Once your numpy array is created, we have to pass it in from_numpy() as an argument. This method converts the numpy array to tensor.

For example


  1. import torch  
  2. import numpy as np1  
  3. numpy_arr = np1.ones((2, 2))  
  4. pytensor = torch.from_numpy(numpy_arr)  
  5. np1_arr_from_tensor = pytensor.numpy()  
  6. print(np1_arr_from_tensor) 

Output:[[1. 1.][1. 1.]]


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *