pytorch

PyTorch – Max()

We will see how to return the maximum values from a tensor using max() 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.

Max()

Max() in PyTorch is used to return the maximum of elements present in the input tensor object.

Syntax:

torch.max(tensor,dim)

Where:

1. The tensor is the input tensor.

2. Dim is to reduce the dimension. Dim=0 specifies the column comparison which gets the maximum values along a column and dim=1 specifies the row comparison, which gets the maximum values along the row.

Return:

It also returns the indices of maximum values.

Example 1:

In this example, we will create a tensor with 2 dimensions that has 3 rows and 5 columns and apply the max() function on rows and columns.

#import torch module

import torch

 

#create a tensor with 2 dimensions (3 * 5)

#with random elements using randn() function

data = torch.randn(3,5)

 

#display

print(data)

 

print()

 

#get Maximum values along columns

print("Maximum values across columns:")

print(torch.max(data, dim=0))

 

print()

 

#get Maximum values along rows

print("Maximum values across rows:")

print(torch.max(data, dim=1))

Output:

tensor([[-0.4322, -1.9488, -1.9488, -0.1291, 2.0196],

[ 0.0421, -0.7765, -1.4377, 1.3680, -0.1703],

[ 1.2953, 0.2773, 0.5380, 0.1011, 0.0136]])

Maximum values across columns:

torch.return_types.max(

values=tensor([1.2953, 0.2773, 0.5380, 1.3680, 2.0196]),

indices=tensor([2, 2, 2, 1, 0]))

Maximum values across rows:

torch.return_types.max(

values=tensor([2.0196, 1.3680, 1.2953]),

indices=tensor([4, 3, 0]))

We can see that the maximum values are returned across the columns and rows along with their indices.

Example 2:

Create a Tensor with 5 * 5 matrix and return the maximum values across the rows and columns.

#import torch module

import torch

 

#create a tensor with 2 dimensions (5 * 5)

#with random elements using randn() function

data = torch.randn(5,5)

 

#display

print(data)

 

print()

 

#get Maximum values along columns

print("Maximum values across columns:")

print(torch.max(data, dim=0))

 

print()

 

#get Maximum values along rows

print("Maximum values across rows:")

print(torch.max(data, dim=1))

Output:

tensor([[ 1.5677, -0.8765, -1.7053, -0.4323, 1.1156],

[ 1.0781, -0.6891, 1.1491, -0.7799, 1.8014],

[-0.0449, 1.6926, -0.9708, -0.1957, 0.6765],

[-0.0336, 0.2317, -1.5785, -0.1141, -0.4226],

[ 0.2535, -0.2302, -0.1518, -0.6725, -0.6502]])

Maximum values across columns:

torch.return_types.max(

values=tensor([ 1.5677, 1.6926, 1.1491, -0.1141, 1.8014]),

indices=tensor([0, 2, 1, 3, 1]))

Maximum values across rows:

torch.return_types.max(

values=tensor([1.5677, 1.8014, 1.6926, 0.2317, 0.2535]),

indices=tensor([0, 4, 1, 1, 0]))

We can see that the maximum values across the rows and columns were returned along with their indices.

Without the Dim Parameter

If we didn’t specify the dim parameter, then it returns the maximum value from the entire tensor.

Example 1:

Create a 2D tensor with 5*5 matrix and return the maximum value.

#import torch module

import torch

 

#create a tensor with 2 dimensions (5 * 5)

#with random elements using randn() function

data = torch.randn(5,5)

 

#display

print(data)

 

print()

 

#get Maximum value

print("Maximum value :")

print(torch.max(data))

Output:

tensor([[ 1.5618, 0.5195, -0.2018, 0.7128, 0.0599],

[-2.4063, -0.0888, -0.3935, 0.2521, 1.3079],

[-1.1231, -0.3366, -1.5907, -1.6770, -0.2147],

[-0.7070, -1.5281, 0.2971, 1.8376, -1.6415],

[-0.8639, 0.5545, 1.2174, -1.3734, -0.7389]])

Maximum value :

tensor(1.8376)

Example 2:

Create a 1D tensor with 5 values and return the maximum value.

#import torch module

import torch

 

#create a tensor with 5 numeric values

data = torch.tensor([10.6,20.7,30.6,40.4,50.0])

 

#display

print(data)

 

print()

 

#get Maximum value

print("Maximum value :")

print(torch.max(data))

Output:

tensor([10.6000, 20.7000, 30.6000, 40.4000, 50.0000])

Maximum value :

tensor(50.)

Work with CPU

If you want to run a max() 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 2 dimensions that has 3 rows and 5 columns with a cpu() function and apply the max() function on rows and columns.

#import torch module

import torch

 

#create a tensor with 2 dimensions (3 * 5)

#with random elements using randn() function

data = torch.randn(3,5).cpu()

 

#display

print(data)

 

print()

 

#get Maximum values along columns

print("Maximum values across columns:")

print(torch.max(data, dim=0))

 

print()

 

#get Maximum values along rows

print("Maximum values across rows:")

print(torch.max(data, dim=1))

Output:

tensor([[-0.1843, 0.0419, -0.0474, 0.3713, -0.6861],

[-0.4083, -0.4918, -1.7254, 1.3483, -0.7855],

[-0.2685, 0.5834, 0.4953, -1.1605, -0.0414]])

Maximum values across columns:

torch.return_types.max(

values=tensor([-0.1843, 0.5834, 0.4953, 1.3483, -0.0414]),

indices=tensor([0, 2, 2, 1, 2]))

Maximum values across rows:

torch.return_types.max(

values=tensor([0.3713, 1.3483, 0.5834]),

indices=tensor([3, 3, 1]))

We can see that the maximum values are returned across the columns and rows along with their indices.

Example 2:

Create a Tensor with 5 * 5 matrix with the cpu() function and return the maximum values across the rows and columns.

#import torch module

import torch

 

#create a tensor with 2 dimensions (5 * 5)

#with random elements using randn() function

data = torch.randn(5,5).cpu()

 

#display

print(data)

 

print()

 

#get Maximum values along columns

print("Maximum values across columns:")

print(torch.max(data, dim=0))

 

print()

 

#get Maximum values along rows

print("Maximum values across rows:")

print(torch.max(data, dim=1))

Output:

tensor([[ 2.2844e+00, 1.0477e+00, -7.5003e-01, 6.2289e-01, 3.6587e-01],

[ 1.4600e+00, -9.5055e-01, 1.0254e+00, 9.8235e-01, 1.6930e+00],

[-1.6253e-01, 4.6555e-02, 1.1996e+00, -1.0124e+00, 9.4128e-01],

[ 1.6245e-01, 6.3275e-01, -2.4493e-01, -1.2998e+00, -4.8166e-01],

[-1.7680e+00, 1.2203e-01, 1.9420e-03, -6.2728e-01, 5.9291e-01]])

Maximum values across columns:

torch.return_types.max(

values=tensor([2.2844, 1.0477, 1.1996, 0.9824, 1.6930]),

indices=tensor([0, 0, 2, 1, 1]))

Maximum values across rows:

torch.return_types.max(

values=tensor([2.2844, 1.6930, 1.1996, 0.6327, 0.5929]),

indices=tensor([0, 4, 2, 1, 4]))

We can see that the maximum values across the rows and columns were returned along with their indices.

Conclusion

In this PyTorch lesson, we learned about the max() function and how to apply it on a tensor to return the maximum values across the columns and rows. It also returns the index positions along with the returned maximum values.

We also created a tensor with the cpu() function and returned the maximum values. If the dim is not specified in two or multi-dimensional tensor, it returns the maximum value from the entire tensor.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain