🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEtensorflow

tensorflow Documentation

LOADING ENGINE...

tf.math.log()

AI & DATA SCIENCE // tf-math-log

tf.math.log() computes the natural logarithm of each element in a tensor, element-wise.

Syntax

tf.math.log(x)

Deep Dive Course

tf.math.log() is the inverse of tf.math.exp(), returning the exponent needed to raise e to in order to get the input value. Like NumPy's np.log(), it's only defined for positive real inputs — log(0) produces -inf, and log of a negative number produces nan, since the natural logarithm has no real result for non-positive numbers. It appears constantly in loss functions like cross-entropy, which fundamentally rely on the log of a predicted probability.

1Understanding tf.math.log()

tf.math.log() is the inverse of tf.math.exp(), returning the exponent needed to raise e to in order to get the input value. Like NumPy's np.log(), it's only defined for positive real inputs — log(0) produces -inf, and log of a negative number produces nan, since the natural logarithm has no real result for non-positive numbers. It appears constantly in loss functions like cross-entropy, which fundamentally rely on the log of a predicted probability.

💡

Predicted probabilities from a model can sometimes be exactly 0 or very close to it due to floating-point rounding, and taking tf.math.log() of that produces -inf or a very large negative number — many loss functions add a tiny epsilon value internally specifically to avoid this, which is worth knowing if you're implementing a custom loss involving log() yourself.

editor.html
import tensorflow as tf

x = tf.constant([1.0, 2.718281828, 7.389056])
print(tf.math.log(x))
localhost:3000

2Practical Example

Here is a real-world application of tf.math.log() showing how it is used in production TensorFlow code.

editor.html
import tensorflow as tf

predicted_prob = tf.constant([0.9, 0.5, 0.1])
loss = -tf.math.log(predicted_prob)
print(loss)
localhost:3000

3Best Practices

Follow these guidelines when working with tf.math.log():

1. Add a small epsilon value before taking the log of a predicted probability in a custom loss function, to avoid -inf from an exact-zero prediction

2. Prefer TensorFlow's built-in loss functions, like categorical crossentropy, over a hand-written log()-based formula, since they already handle this numerical edge case internally

3. Use tf.math.log() together with tf.math.exp() for converting between a value and its log-space representation when numerical stability for very small or very large values matters

⚠️

Tip: Predicted probabilities from a model can sometimes be exactly 0 or very close to it due to floating-point rounding, and taking tf.math.log() of that produces -inf or a very large negative number — many loss functions add a tiny epsilon value internally specifically to avoid this, which is worth knowing if you're implementing a custom loss involving log() yourself.

editor.html
import tensorflow as tf

x = tf.constant([1.0, 2.718281828, 7.389056])
print(tf.math.log(x))
localhost:3000

Examples

Example 01Basic Usage
import tensorflow as tf

x = tf.constant([1.0, 2.718281828, 7.389056])
print(tf.math.log(x))
Example 02Advanced Example
import tensorflow as tf

predicted_prob = tf.constant([0.9, 0.5, 0.1])
loss = -tf.math.log(predicted_prob)
print(loss)

Best Practices

  • Add a small epsilon value before taking the log of a predicted probability in a custom loss function, to avoid -inf from an exact-zero prediction
  • Prefer TensorFlow's built-in loss functions, like categorical crossentropy, over a hand-written log()-based formula, since they already handle this numerical edge case internally
  • Use tf.math.log() together with tf.math.exp() for converting between a value and its log-space representation when numerical stability for very small or very large values matters

Interview Question

Why does cross-entropy loss involve taking the negative log of a predicted probability, rather than using the raw probability value directly?

Hint: Think about how the negative log function's shape penalizes confident-but-wrong predictions compared to a linear penalty.

The negative log of a probability grows extremely large as the probability approaches 0, while it approaches 0 itself as the probability approaches 1 — this means a confident, correct prediction, a probability near 1 for the true class, produces a tiny loss, while a confident, wrong prediction, a probability near 0 for the true class, produces a very large loss, growing without bound. A simpler penalty based on the raw probability directly, like 1 minus the probability, would penalize a wildly overconfident wrong prediction only mildly more than a mildly uncertain one, whereas the negative log's shape specifically punishes confident wrongness much more severely, which is exactly the training signal that pushes a model to become genuinely well-calibrated rather than just superficially close.

Exercises

MediumPractice using tf.math.log() in a real scenario.
View Solution
import tensorflow as tf

x = tf.constant([1.0, 2.718281828, 7.389056])
print(tf.math.log(x))

Frequently Asked Questions

Why does cross-entropy loss involve taking the negative log of a predicted probability, rather than using the raw probability value directly?

The negative log of a probability grows extremely large as the probability approaches 0, while it approaches 0 itself as the probability approaches 1 — this means a confident, correct prediction, a probability near 1 for the true class, produces a tiny loss, while a confident, wrong prediction, a probability near 0 for the true class, produces a very large loss, growing without bound. A simpler penalty based on the raw probability directly, like 1 minus the probability, would penalize a wildly overconfident wrong prediction only mildly more than a mildly uncertain one, whereas the negative log's shape specifically punishes confident wrongness much more severely, which is exactly the training signal that pushes a model to become genuinely well-calibrated rather than just superficially close.

Related Functions

tf-math-explosses-categoricalcrossentropynp-log