Python

Compute the Logarithm of Elements of a Tensor in PyTorch

“In this PyTorch tutorial, we will see how to perform logarithmic functions on a given tensor.

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.

To create a tensor, the method used is tensor()”

Syntax:

torch.tensor(data)

Where data is a multi-dimensional array.

log()

log() in PyTorch is used to return the natural logarithm of all the elements present in the tensor object. It takes only one parameter.

Syntax:

torch.log(tensor_object)

Parameter:

The tensor_object is the input tensor

Example 1

In this example, we will create a tensor with 3 dimensions that have 3 rows and 5 columns and apply log() on it.

#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 logarithmic values

print("logarithmic values:")

print(torch.log(data))

Output:

tensor([[-1.0134, -0.0345, 0.0841, 0.7704, 0.3895],

[ 0.5293, -0.9141, 0.4486, -1.1050, -0.1396],

[-2.7476, -1.6378, -0.3021, 0.0936, 1.9816]])

logarithmic values:

tensor([[ nan, nan, -2.4762, -0.2608, -0.9429],

[-0.6361, nan, -0.8017, nan, nan],

[ nan, nan, nan, -2.3682, 0.6839]])

We can see that the natural log values for all the elements in a tensor were returned.

Example 2

Create Tensor with 5 * 5 matrix and return natural log values.

#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 logarithmic values

print("logarithmic values:")

print(torch.log(data))

Output:

tensor([[-0.2143, 0.4640, -0.7694, 0.2063, 0.1471],

[-0.9600, 0.3431, 0.0933, -0.7847, -0.6198],

[ 1.9533, 0.7456, -0.8035, -0.2091, -2.1858],

[-0.3841, 0.4142, -1.6795, -1.3310, 1.5622],

[ 0.3093, 0.6724, 0.5488, -1.3811, 1.6062]])

logarithmic values:

tensor([[ nan, -0.7679, nan, -1.5782, -1.9169],

[ nan, -1.0698, -2.3719, nan, nan],

[ 0.6695, -0.2936, nan, nan, nan],

[ nan, -0.8815, nan, nan, 0.4461],

[-1.1735, -0.3969, -0.6001, nan, 0.4739]])

We can see that the natural log values for all the elements in a tensor were returned.

log10()

log10() in PyTorch is used to return the logarithm to the base 10 of all the elements present in the tensor object. It takes only one parameter.

Syntax:

torch.log10(tensor_object)

Parameter:

The tensor_object is the input tensor

Example 1

In this example, we will create a tensor with 3 dimensions that have 3 rows and 5 columns and apply log10() on it.

#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 logarithmic values to the base 10

print("logarithmic values to the base 10:")

print(torch.log10(data))

Output:

tensor([[ 0.1137, 1.8604, 0.1547, 0.1092, 0.0385],

[-1.2716, 1.8937, -0.4259, 0.4512, 0.5377],

[-1.3074, 2.2634, 1.0972, -0.3502, 0.4971]])

logarithmic values to the base 10:

tensor([[-0.9441, 0.2696, -0.8105, -0.9617, -1.4140],

[ nan, 0.2773, nan, -0.3456, -0.2695],

[ nan, 0.3548, 0.0403, nan, -0.3035]])

We can see that the log values to the base 10 for all the elements in a tensor were returned.

Example 2

Create Tensor with 5 * 5 matrix and return log values to the base 10.

#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 logarithmic values to the base 10

print("logarithmic values to the base 10:")

print(torch.log10(data))

Output:

tensor([[-0.2903, -0.1354, -0.7794, -0.5695, -0.7214],

[ 0.5197, 0.5463, 1.4539, 0.0285, -0.7019],

[-0.0714, -1.2804, 0.0606, 1.1813, 0.9769],

[ 0.2130, 1.1354, 0.2970, -0.2755, -0.0466],

[ 2.8192, -0.9078, 0.5023, 1.1128, 0.3141]])

logarithmic values to the base 10:

tensor([[ nan, nan, nan, nan, nan],

[-0.2842, -0.2626, 0.1625, -1.5455, nan],

[ nan, nan, -1.2177, 0.0724, -0.0101],

[-0.6717, 0.0551, -0.5273, nan, nan],

[ 0.4501, nan, -0.2990, 0.0464, -0.5029]])

We can see that the log values to the base 10 for all the elements in a tensor were returned.

log2()

log2() in PyTorch is used to return the logarithm to the base 2 of all the elements present in the tensor object. It takes only one parameter.

Syntax:

torch.log2(tensor_object)

Parameter:

The tensor_object is the input tensor

Example 1

In this example, we will create a tensor with 3 dimensions that have 3 rows and 5 columns and apply log2() on it.

#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 logarithmic values to the base 2

print("logarithmic values to the base 2:")

print(torch.log2(data))

Output:

tensor([[-0.0242, 0.6124, -1.2847, -0.2737, 1.2455],

[-0.5786, -0.1747, 0.6064, -0.5265, 0.3504],

[-0.3898, 0.5609, -0.0565, 0.5324, 0.0105]])

logarithmic values to the base 2:

tensor([[ nan, -0.7075, nan, nan, 0.3168],

[ nan, nan, -0.7216, nan, -1.5128],

[ nan, -0.8342, nan, -0.9095, -6.5752]])

We can see that the log values to the base 2 for all the elements in a tensor were returned.

Example 2

Create Tensor with 5 * 5 matrix and return log values to the base 2.

#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 logarithmic values to the base 2

print("logarithmic values to the base 2:")

print(torch.log2(data))

Output:

tensor([[ 3.0918, 0.2328, 0.6354, -0.6991, 2.1373],

[-1.2590, -1.5860, -0.1142, -0.1805, -1.9556],

[ 1.2391, 1.0197, 0.1663, 0.9892, -1.4073],

[ 0.0174, 0.8185, 0.3453, -0.7556, 1.0040],

[-1.0775, 0.4131, -0.7916, -0.9372, 0.1482]])

logarithmic values to the base 2:

tensor([[ 1.6285e+00, -2.1029e+00, -6.5418e-01, nan, 1.0958e+00],

[ nan, nan, nan, nan, nan],

[ 3.0926e-01, 2.8108e-02, -2.5882e+00, -1.5649e-02, nan],

[-5.8447e+00, -2.8896e-01, -1.5339e+00, nan, 5.7767e-03],

[ nan, -1.2754e+00, nan, nan, -2.7546e+00]])

We can see that the log values to the base 2 for all the elements in a tensor were returned.

Work With CPU

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

When we are creating a tensor, at this time, we can use the cpu() function.

Syntax:

torch.tensor(data).cpu()

Example

Create Tensor with 5 * 5 matrix on the cpu and return natural log values, log values with base2, and log values with base 10.

#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 natural log values

print("natural log values: ")

print(torch.log(data))

print()

#get logarithmic values to the base 2

print("logarithmic values to the base 2:")

print(torch.log2(data))

print()

#get logarithmic values to the base 10

print("logarithmic values to the base 10:")

print(torch.log10(data))

Output:

tensor([[-0.2807, 0.0260, 0.3326, -0.1958, 2.7080],

[ 1.3534, -0.2371, 0.0085, 0.1877, 1.4870],

[ 1.2967, 0.4262, -0.6323, 0.4446, 3.0513],

[ 0.4478, -0.0436, -0.4577, 1.3098, 0.7293],

[-0.4575, -1.4020, -0.9323, -0.4406, 0.5844]])

natural log values:

tensor([[ nan, -3.6494, -1.1009, nan, 0.9962],

[ 0.3026, nan, -4.7711, -1.6731, 0.3968],

[ 0.2598, -0.8529, nan, -0.8107, 1.1156],

[-0.8034, nan, nan, 0.2699, -0.3157],

[ nan, nan, nan, nan, -0.5371]])

logarithmic values to the base 2:

tensor([[ nan, -5.2650, -1.5882, nan, 1.4372],

[ 0.4366, nan, -6.8833, -2.4138, 0.5724],

[ 0.3748, -1.2304, nan, -1.1696, 1.6094],

[-1.1591, nan, nan, 0.3893, -0.4554],

[ nan, nan, nan, nan, -0.7749]])

logarithmic values to the base 10:

tensor([[ nan, -1.5849, -0.4781, nan, 0.4327],

[ 0.1314, nan, -2.0721, -0.7266, 0.1723],

[ 0.1128, -0.3704, nan, -0.3521, 0.4845],

[-0.3489, nan, nan, 0.1172, -0.1371],

[ nan, nan, nan, nan, -0.2333]])

We can see that the log values to the base 2, log values to the base 10, and natural log values for all the elements in a tensor were returned.

Conclusion

In this PyTorch lesson, we saw three types of logarithmic functions used to return logarithmic values for all the elements in a tensor. torch.log() is a simple logarithmic function used to return the natural logarithm of all the elements present in the tensor object.log10() is used to return the logarithm to the base 10 of all the elements present in the tensor object, and log2() is used to return the logarithm to the base 2 of all the elements present in the tensor object. We also discussed how to apply these functions while we were working with cpu.

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