Python

# PyTorch – Square()

We will return the square of all the elements in tensor using the square() method in this PyTorch tutorial.

PyTorch is an open-source framework available with a Python programming language. Tensor is a multidimensional array that is used to store the data. To use a tensor, we have to import the torch module. To create a tensor, the method used is tensor().

Syntax:

torch.tensor(data)

Where the data is a multi-dimensional array.

## Torch.square()

Square() in PyTorch returns the square of every element in PyTorch tensor. It takes one parameter.

Syntax:

torch.square(tensor_object)

Parameter:

tensor_object is a tensor

## Example 1:

In this example, we will create a tensor with one dimension that has 5 elements and return the square of these 5 elements in a tensor.

#import torch module

import torch

#create a tensor

data1 = torch.tensor([12,34,56,1,10])

#display

print("Actual Tensor: ")

print(data1)

print("Square: ")

print(torch.square(data1))

Output:

Actual Tensor:

tensor([12, 34, 56, 1, 10])

Square:

tensor([ 144, 1156, 3136, 1, 100])

Working:

1. 12 * 12 = 144
2. 34 * 34 = 1156
3. 56 * 56 = 3136
4. 1 * 1 = 1
5. 10 * 10 = 100

## Example 2:

In this example, we will create a tensor with two dimensions that has 5 elements in each row and return the squares of the elements.

#import torch module

import torch

#create a 2D tensor

data1=torch.tensor([[45,67,21,23,2],[-2,3,4,5,6]])

#display

print("Actual Tensor: ")

print(data1)

print("Square")

print(torch.square(data1))

Output:

Actual Tensor:

tensor([[45, 67, 21, 23, 2],

[-2, 3, 4, 5, 6]])

Square

tensor([[2025, 4489, 441, 529, 4],

[ 4, 9, 16, 25, 36]])

Working:

1. 45 * 45 = 2025, -2*-2 = 4
2. 67 * 67 = 4489,3*3=9
3. 21 * 21 = 441,4*4=16
4. 23 * 23 = 529,5*5=25
5. 2 * 2 = 4,6*6=36

### Work with CPU

If you want to run a square() function on the CPU, we have to create a tensor with a cpu() function. This will run on a CPU machine.

When we create a tensor, this time, we can use the cpu() function.

Syntax:

torch.tensor(data).cpu()

## Example 1:

In this example, we will create a tensor with one dimension that has 5 elements on the cpu function and return the square of these 5 elements in a tensor.

#import torch module

import torch

#create a tensor

data1 = torch.tensor([12,34,56,1,10]).cpu()

#display

print("Actual Tensor: ")

print(data1)

print("Square: ")

print(torch.square(data1))

Output:

Actual Tensor:

tensor([12, 34, 56, 1, 10])

Square:

tensor([ 144, 1156, 3136, 1, 100])

Working:

1. 12 * 12 = 144
2. 34 * 34 = 1156
3. 56 * 56 = 3136
4. 1 * 1 = 1
5. 10 * 10 = 100

## Example 2:

In this example, we will create a tensor with two dimensions that has 5 elements on a cpu function in each row and return the squares of the elements.

#import torch module

import torch

#create a 2D tensor

data1=torch.tensor([[45,67,21,23,2],[-2,3,4,5,6]]).cpu()

#display

print("Actual Tensor: ")

print(data1)

print("Square")

print(torch.square(data1))

Output:

Actual Tensor:

tensor([[45, 67, 21, 23, 2],

[-2, 3, 4, 5, 6]])

Square

tensor([[2025, 4489, 441, 529, 4],

[ 4, 9, 16, 25, 36]])

Working:

1. 45 * 45 = 2025, -2*-2 = 4
2. 67 * 67 = 4489,3*3=9
3. 21 * 21 = 441,4*4=16
4. 23 * 23 = 529,5*5=25
5. 2 * 2 = 4,6*6=36

## Conclusion

In this PyTorch lesson, we discussed about the square() function. It returns the square of every element in the PyTorch tensor. We discussed the two examples with the different dimensional tensors to perform the square() function.