Python | Tensorflow log () method

The tensorflow.math module provides support for many basic mathematical operations. The tf.log () [alias tf.math.log ] function provides support for the natural logarithmic function in Tensorflow. Expected to be input as complex numbers in the form or floating point. Input type — tensor, and if the input contains more than one element, the element-wise logarithm is calculated, ,

Syntax : tf.log (x, name = None) or tf.math.log (x, name = None)

Parameters :
x : A Tensor of type bfloat16, half, float32, float64, complex64 or complex128.
name (optional): The name for the operation.

Return type : A Tensor with the same size and type as that of x.

Code # 1:

# Import Tensorflow library

import tensorflow as tf

 
# Constant vector of size 5

a = tf.constant ([ - 0.5 , - 0.1 , 0 , 0.1 , 0.5 ], dtype = tf.float32)

 
# Using the log function and
# saving the result to & # 39; b & # 39;

b = tf.log (a, name = `log` )

  
# Initiating a Tensorfl session ow
with tf.Session () as sess:

print ( `Input type:` , a)

print ( `Input:` , sess.run (a))

print ( `Return type:` , b)

print ( `Output:` , sess.run (b))

Exit:

 Input type: Tensor ("Const: 0", shape = (5,), dtype = float32) Input: [-0.5 -0.1 0. 0.1 0.5] Return type: Tensor (" log: 0 ", shape = (5,), dtype = float32) Output: [nan nan -inf -2.3025851 -0.6931472] 

means that natural logarithm does not exist for negative values ​​and means that it is approaching negative infinity when the input approaches zero.

Code # 2: Rendering

# Import Tensorflow library

import tensorflow as tf

 
# Importing the NumPy library

import numpy as np

 
# Import ma function tplotlib.pylot

import matplotlib.pyplot as plt

 
# Vector size 20 with values ​​0 to 1 and 1 to 10

a = np.append (np.linspace ( 0 , 1 , 10 ), np.linspace ( 1 , 10 , 10 ))

 
# Applying a logarithmic function and
# save the result to & # 39; b & # 39;

b = tf.log (a, name = `log` )

  
# Initiate a Tensorflow session
with tf.Session () as sess:

print ( `Input:` , a)

print ( `Output:` , sess.run (b))

plt.plot (a, sess.run (b), color = ` red` , marker = "o"

  plt.title ( " tensorflow.abs "

plt.xlabel ( "X"

plt.ylabel ( "Y"

  plt.grid ()

 

  plt.show ()

Output:

 Input: [0. 0.11111111 0.22222222 0.33333333 0.44444444 0.55555556 0.66666667 0.77777778 0.88888889 1. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.] Output: [-inf -2.19722458 -1.5040774 -1.09861229 -0.81093022 -0.58778666 -0.40546511 -0.25131443 -0.11778304 0. 0. 0.69314718 1.09861229 1.38629436 1.609437 91 1.79175947 1.94591015 2.07944154 2.19722458 2.30258509]