- +91 7397328021 support@deepneuron.In

Home / Blog / Machine Learning / Tensors

**Tensors**

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.

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.

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

- Create Tensor from an array
- Create Tensor with all ones and random number
- 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**

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

**Output:**

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

**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**

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

**Output:**

tensor([[1., 1.],[1., 1.]]) tensor([[0.4962, 0.7682],[0.0885, 0.1320]])

**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**

import torch import numpy as np1 numpy_arr = np1.ones((2, 2)) pytensor = torch.from_numpy(numpy_arr) np1_arr_from_tensor = pytensor.numpy() print(np1_arr_from_tensor)

**Output:**

[[1. 1.][1. 1.]]

**You may also like...**

**You may also like...**