TensorFlow
probability density function
PDF calculation
machine learning
data science

how to calculate PDF in tensorflow

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

If you want a probability density function in modern TensorFlow code, the usual tool is TensorFlow Probability, not raw TensorFlow alone. You create a distribution object such as tfd.Normal and evaluate its density with prob, or more commonly its log density with log_prob.

Use TensorFlow Probability

TensorFlow itself focuses on tensors, automatic differentiation, and model execution. Probability distributions live in TensorFlow Probability, usually imported as tfp.

python
1import tensorflow as tf
2import tensorflow_probability as tfp
3
4tfd = tfp.distributions

Once you have a distribution object, you can evaluate the density at one value or many values at once.

Example: Normal Distribution PDF

python
1import tensorflow as tf
2import tensorflow_probability as tfp
3
4tfd = tfp.distributions
5
6dist = tfd.Normal(loc=0.0, scale=1.0)
7x = tf.constant([-1.0, 0.0, 1.0], dtype=tf.float32)
8
9pdf = dist.prob(x)
10log_pdf = dist.log_prob(x)
11
12print("pdf:", pdf.numpy())
13print("log_pdf:", log_pdf.numpy())

prob returns the density for each input value. log_prob returns the natural logarithm of that density, which is often preferred in machine learning because it is numerically more stable when probabilities get very small.

Batch Shapes and Vectorization

A major advantage of TensorFlow Probability is that prob works on tensors, not only scalars. If your distribution parameters are also tensors, TensorFlow broadcasts shapes where appropriate.

python
1import tensorflow as tf
2import tensorflow_probability as tfp
3
4tfd = tfp.distributions
5
6means = tf.constant([0.0, 5.0], dtype=tf.float32)
7scales = tf.constant([1.0, 2.0], dtype=tf.float32)
8dist = tfd.Normal(loc=means, scale=scales)
9
10x = tf.constant([0.0, 5.0], dtype=tf.float32)
11print(dist.prob(x).numpy())

This kind of vectorized evaluation is one reason TensorFlow Probability is much nicer than hand-writing PDF formulas for every model.

Discrete Distributions Use prob Too

The method name is still prob for both continuous and discrete distributions. Conceptually:

  • continuous distribution: prob is a density
  • discrete distribution: prob is a probability mass

Example with a Bernoulli distribution:

python
1import tensorflow_probability as tfp
2
3tfd = tfp.distributions
4
5dist = tfd.Bernoulli(probs=0.8)
6print(dist.prob([0, 1]).numpy())

So the method name stays the same even though the underlying mathematical meaning changes slightly.

Why log_prob Is Often Better

In real probabilistic models, products of many densities can underflow quickly. Summing log densities is more stable and often easier to optimize.

python
1import tensorflow as tf
2import tensorflow_probability as tfp
3
4tfd = tfp.distributions
5
6dist = tfd.Normal(loc=0.0, scale=1.0)
7samples = tf.constant([0.2, -0.1, 0.4], dtype=tf.float32)
8
9total_log_likelihood = tf.reduce_sum(dist.log_prob(samples))
10print(total_log_likelihood.numpy())

If you are building losses for maximum likelihood or Bayesian inference, log_prob is usually the quantity you actually want.

Custom Formula Versus Distribution Object

You can always implement a PDF directly with TensorFlow math operations. For a standard normal, that could look like this:

python
1import tensorflow as tf
2
3def standard_normal_pdf(x):
4    two_pi = tf.constant(2.0 * 3.141592653589793, dtype=tf.float32)
5    return tf.exp(-0.5 * tf.square(x)) / tf.sqrt(two_pi)
6
7x = tf.constant([0.0, 1.0, 2.0], dtype=tf.float32)
8print(standard_normal_pdf(x).numpy())

That is educational, but for production code, distribution objects are usually better because they provide tested implementations, shape handling, sampling methods, cumulative functions, and log-space computations.

Installation

You need TensorFlow Probability installed alongside TensorFlow:

bash
pip install tensorflow tensorflow-probability

Version compatibility matters, so it is worth checking that your TensorFlow and TensorFlow Probability versions are intended to work together in the environment you are using.

Common Pitfalls

The most common mistake is trying to find a standalone pdf function in core TensorFlow. In modern code, the usual entry point is distribution.prob(...) from TensorFlow Probability.

Another issue is confusing density with probability. For continuous distributions, a PDF value at one exact point is not itself a probability. Developers also often use prob where log_prob would be more numerically stable in model training.

Summary

  • Use TensorFlow Probability to evaluate PDFs in modern TensorFlow workflows.
  • Create a distribution such as tfd.Normal and call prob or log_prob.
  • 'log_prob is usually better for training and likelihood calculations.'
  • TensorFlow Probability handles vectorized tensor inputs naturally.
  • You can implement formulas by hand, but distribution objects are usually safer and more practical.

Course illustration
Course illustration

All Rights Reserved.