In this example, we define a 1-D tensor and we compute the logarithms to the base 2 values of its elements.
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.
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])
In this example, we try to compute the logarithm to the base 2 of negative, zero, and infinity values. See how the output is.