Open In App

Python PyTorch log2() method

Last Updated : 04 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

PyTorch log2() method computes the logarithm to the base 2 of the elements of an input tensor.  It computes the logarithm values element-wise.  It takes a tensor as an input and returns a new tensor with computed logarithm values. The elements of the input tensor must be between zero and the positive infinity as the domain of the function log2(x) is (0,∞). We will use the following syntax for this method-

Syntax: torch.log2(input, out=None)

Parameters:

  • input: the input tensor.
  • out: the output tensor.

Return: It returns a new tensor with the logarithm to the base 2 values of the elements of the input tensor.

Let’s understand the torch.log2() method with the help of some Python 3 program examples.

Example 1:

In this example, we define a 1-D tensor and we compute the logarithms to the base 2 values of its elements.

Python3




# Python3 program to demonstrate torch.log2() method
# importing torch
import torch
 
# defining a torch tensor
t = torch.tensor([2., 4., 7., 3.])
print('Tensor:', t)
 
# computing the logarithm base 2 of t
result = torch.log2(t)
print('Logarithm of Tensor:', result)


Output:

Tensor: tensor([2., 4., 7., 3.])

Logarithm of Tensor: tensor([1.0000, 2.0000, 2.8074, 1.5850])

Explanation: The logarithm is computed element-wise. 

Example 2:

In the example below we compute the logarithms to the base 2 of a 2D tensor. 

Python3




# Python3 program to demonstrate torch.log2() method
# for 2D tensors
# importing torch
import torch
 
# defining a 2D torch tensor with numbers sampled
# from the discrete uniform distribution
t = torch.empty((3, 4), dtype=torch.float32).random_(1, 50)
print('Tensor:\n', t)
 
# computing the logarithm base 2 of t
result = torch.log2(t)
print('Logarithm of Tensor:\n', result)


Output:

Tensor:
 tensor([[19., 32.,  2., 29.],
        [30., 10., 16., 18.],
        [49., 10.,  7., 25.]])
Logarithm of Tensor:
 tensor([[4.2479, 5.0000, 1.0000, 4.8580],
        [4.9069, 3.3219, 4.0000, 4.1699],
        [5.6147, 3.3219, 2.8074, 4.6439]])

Explanation: Please note that the input tensor in the above code is generated with numbers sampled from the discrete uniform distribution, so when you execute the code may get different numbers generated. 

Example 3:

In the example below we compute the logarithm to the base 2 of the elements of the input tensor and visualize the results with the help of a Matplotlib plot.

Python3




# Python3 program to demonstrate torch.log2() method
%matplotlib qt
 
# importing required libraries
import torch
import numpy as np
 
# import matplotlib.pyplot as plt
# defining a torch tensor
x = np.arange(1,50, 1)
t = torch.tensor(x)
print('Tensor:', t)
 
# computing the logarithm base 2 of t
result = torch.log2(t)
print('Logarithm of Tensor:', result)
 
# tensor to numpy array
y = result.numpy()
 
# plot the result using matplotlib
plt.plot(x, y, color = 'red', marker = "o")
plt.xlabel("x")
plt.ylabel("log2(x)")
plt.show()


Output:

Tensor: tensor([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18,

        19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,

        37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49], dtype=torch.int32)

Logarithm of Tensor: tensor([0.0000, 1.0000, 1.5850, 2.0000, 2.3219, 2.5850, 2.8074, 3.0000, 3.1699,

        3.3219, 3.4594, 3.5850, 3.7004, 3.8074, 3.9069, 4.0000, 4.0875, 4.1699,

        4.2479, 4.3219, 4.3923, 4.4594, 4.5236, 4.5850, 4.6439, 4.7004, 4.7549,

        4.8074, 4.8580, 4.9069, 4.9542, 5.0000, 5.0444, 5.0875, 5.1293, 5.1699,

        5.2095, 5.2479, 5.2854, 5.3219, 5.3576, 5.3923, 5.4263, 5.4594, 5.4919,

        5.5236, 5.5546, 5.5850, 5.6147])

Example 4:

In this example, we try to compute the logarithm to the base 2 of negative, zero, and infinity values. See how the output is.

Python3




# Python3 program to demonstrate torch.log2() method
# importing libraries
import torch
import numpy as np
 
# defining a torch tensor
t = torch.tensor([-3., -5., 0, 0.5, 7., 3., np.inf])
print('Original Tensor:\n', t)
 
# computing the logarithm base 2 of t
result = torch.log2(t)
print('Logarithm of Tensor:\n', result)


Output:

Original Tensor:

 tensor([-3.0000, -5.0000,  0.0000,  0.5000,  7.0000,  3.0000,     inf])

Logarithm of Tensor:

 tensor([    nan,     nan,    -inf, -1.0000,  2.8074,  1.5850,     inf])

Explanation: See the logarithm of the negative number is nan (Not a Number). The logarithm of 0 is -inf. And the logarithm of infinity is infinity (inf).



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads