PyTorch is an open-source framework available with a Python programming language.

A tensor is a multidimensional array that is used to store the data. So for using a Tensor, we have to import the torch module.

It is possible to check whether the given object is a tensor or not.

torch.is_tensor() is used to check whether the given object is tensor or not.

If the object is a tensor, it will return True otherwise, False.”

**Syntax:**

**Parameter:**

object refers to the collection of data.

**Example 1**

Here, we will create a tensor with 5 elements and check if it is a tensor or not.

import torch

#create a 1D tensor with 5 elements

data1 = torch.tensor([23,45,67,0,0])

#check whether data1 is tensor or not

print(torch.is_tensor(data1))

Output:

We can see that the given object is a tensor. So it returned True.

**Example 2**

Let’s create a list with 5 elements and check if it is tensor or not.

import torch

#create a list with 5 elements

data1 = [23,45,67,0,0]

#check whether data1 is tensor or not

print(torch.is_tensor(data1))

Output:

It returned False.

Now, we will see how to return the metadata of a tensor.

Metadata explains the tensor structure and elements present in the vector.

**torch.size()**

torch.size() returns the total number of elements present in a tensor.

**Syntax:**

Where tensor_object is the tensor.

It takes no parameters.

**Example 1**

Let’s create a 1D tensor and return size.

import torch

#create a 1D tensor with 5 elements

data1 = torch.tensor([23,45,67,0,0])

#display

print(«Tensor: «,data1)

#return tensor size

print(«Size: «,data1.size())

Output:

Size: torch.Size([5])

We can see that 5 is returned since there are 5 elements in the above tensor.

**Example 2**

Let’s create a 2D tensor and return size.

import torch

#create a 2D tensor with 5 elements in each row

data1 = torch.tensor([[23,45,67,0,0],[23,45,67,0,0]])

#display

print(«Tensor: «,data1)

#return tensor size

print(«Size: «,data1.size())

Output:

[23, 45, 67, 0, 0]])

Size: torch.Size([2, 5])

We can see that 2,5 is returned and represents 2 rows and 5 columns.

**torch.shape**

torch.shape() returns the shape of a tensor.

**Syntax:**

Where tensor_object is the tensor.

It takes no parameters.

**Example 1**

import torch

#create a 1D tensor with 5 elements

data1 = torch.tensor([23,45,67,0,0])

#display

print(«Tensor: «,data1)

#return tensor Shape

print(«Shape: «,data1.shape)

Output:

Shape: torch.Size([5])

We can see that 5 is returned since there are 5 elements in the above tensor.

**Example 2**

import torch

#create a 2D tensor with 5 elements in each row

data1 = torch.tensor([[23,45,67,0,0],[23,45,67,0,0]])

#display

print(«Tensor: «,data1)

#return tensor Shape

print(«Shape: «,data1.shape)

Output:

[23, 45, 67, 0, 0]])

Shape: torch.Size([2, 5])

We can see that 2,5 is returned and represents 2 rows and 5 columns.

**torch.numel()**

torch.numel() returns the total number of elements present in a tensor.

**Syntax:**

Where tensor_object is the tensor.

It takes no parameters.

**Example 1**

import torch

#create a 1D tensor with 5 elements

data1 = torch.tensor([23,45,67,0,0])

#display

print(«Tensor: «,data1)

#return total number of elements in a tensor

print(«Total elements: «,data1.numel())

Output:

Total elements: 5

We can see that 5 is returned since there are 5 elements in the above tensor.

**Example 2**

import torch

#create a 2D tensor with 5 elements in each row

data1 = torch.tensor([[23,45,67,0,0],[23,45,67,0,0]])

#display

print(«Tensor: «,data1)

#return total number of elements in a tensor

print(«Total elements: «,data1.numel())

Output:

[23, 45, 67, 0, 0]])

Total elements: 10

We can see that 10 is returned since there are a total of 10 elements present in the tensor.

**Conclusion**

In this PyTorch lesson, we saw how to check if the given object is tensor or not using the is_tensor() function. To return the metadata, we used size() and shape methods to return the size and shape of the given tensor.