Implement TensorFlow log10 Function: A Step Guide – TensorFlow Tutorial

By | December 9, 2021

We have not found log10() function in tensorflow, however, we only find tf.log() function, which is used compute \(ln(x)\). In this tutorial, we will create a function named log10(x) to compuate \(log10(x)\).

We know:

create tensorflow log10 function

It means:

\(\log_{10}{x}=\frac{\log_e{x}}{\log_e{10}}\)

Then, we will create a function to compute \(\log_{10}{x}\) in tensorflow.

Look at this example:

import tensorflow as tf
import numpy as np

i = np.array([1,2, 20, 10, 100])
i = tf.convert_to_tensor(i, dtype = tf.float32)

def log10(x):
    x1 = tf.log(x)
    x2 = tf.log(10.0)
    return x1/ x2

w = log10(i)

init = tf.global_variables_initializer()
init_local = tf.local_variables_initializer()
with tf.Session() as sess:
    sess.run([init, init_local])
    np.set_printoptions(precision=4, suppress=True)
    a =sess.run(w)
    print(a)

In this code, we create a log10(x) to compute, run this code, you will get:

[0.    0.301 1.301 1.    2.   ]

Leave a Reply