# Gentle Introduction to TensorFlow Probability: Distribution Objects

This article belongs to the series “Probabilistic Deep Learning”. This weekly series covers probabilistic approaches to deep learning. The main goal is to extend deep learning models to quantify uncertainty, i.e. know what they do not know.

We develop our models using TensorFlow and TensorFlow Probability (TFP). TFP is a Python library built on top of TensorFlow. We are going to start with the basic objects that we can find in TensorFlow Probability (TFP) and understand how can we manipulate them. We will increase complexity incrementally over the following weeks and combine our probabilistic models with deep learning on modern hardware (e.g. GPU).

As usual, the code is available in my GitHub.

**Univariate Distributions**

Distribution objects capture the essential operations on probability distributions. Let’s start with the simplest form — univariate distributions. As the name suggests, these are distributions with only one random variable. A Gaussian distribution is a continuous probability distribution fully defined by its mean and standard deviation. Its standard form is a special case where μ = 0 and σ = 1.

Notice the properties `batch_shape`

and `event_shape`

. The `event_shape`

property captures the dimensionality of the random variable. Since we defined a univariate distribution the `event_shape`

is empty. The `batch_shape`

represents the number of different independent distribution objects that we are storing in our object. In this case, we are storing only one distribution hence it is empty.

What can we do with this distribution object? We can, for instance, sample from it.

We can also evaluate the Probability Density Function (PDF), in the case of continuous random variables, on a given input.

When we start implementing Machine Learning and Deep Learning algorithms we will often find ourselves facing a common problem: multiplying probabilities is not fun. The overall product starts to approach zero quickly and you will run out of precision to store such tiny numbers.

A common way to solve this problem is to replace probabilities with log probabilities. First, by applying the log transformation we are changing the domain from [0,1] to (-∞, 0]. This is relevant since instead of having to store very small numbers (we would run out of precision quickly), we can store big negative numbers in the log scale. Second, the logarithm function is a monotonically increasing function, which ensures that we can compare two transformed log probabilities the same way we do with the original ones. Finally, the log transformation converts all multiplication operations into addition. Thus, the probabilities are no longer multiplied, instead, we sum the log probabilities together.

In TFP working with log probabilities is trivial, we just need to call a different method.

Notice that the above is simply the natural logarithm of the value returned by the`prob`

method.

Finally, we can plot a histogram, which approximates the density of the distribution.

Now, let’s explore how we can in a single object store a batch of distributions. Let’s create two univariate normal distributions, one with μ=0 and σ=1 and another with μ=1 and σ=1.

Note that these two distributions need to be of the same type (in this case they are both Gaussian) and they are considered independent, i.e. this is not the way to create multivariate distributions. In the above output, you can see that the `batch_shape`

is now 2 as expected.

We can sample from both distributions easily.

The shape of our samples is different. It is now (3,2), as we are drawing 3 samples from each normal distribution. We can plot both distributions just to be sure that they are not correlated in any way.

In the same way, we can get the values for the PDFs of both distributions.

We get a returned tensor of shape (2,) since we are getting the PDF values for both distributions in the two different points that we supplied.

As an exercise to understand better how the shapes behave, let’s increase the rank of our `batch_shape`

.

We now have 6 different Gaussian distributions stored in the same object. Can you tell what is the mean and standard deviation of `normal_batch_D[0,2,1] `

without running the code?

As usual, we can sample from it.

We get a tensor with shape (10, 1, 3, 2), which just means that we have 10 samples (the first dimension) for each of the 6 Gaussian distributions.

## Multivariate Distributions

Now, it is time to explore the`event_shape`

property of our distribution objects. There are multiple ways to create a multivariate distribution, let’s start with the simplest one. We can define a 2-dimensional Gaussian distribution and not include any correlation between the two dimensions, which means that the off-diagonal terms of the covariance matrix are 0.

Finally, we see an`event_shape`

populated, in this case with a value of 2. As we stated this is the dimensionality of our random variable defined above.

Time to sample from it and see the differences compared to multiple batched distributions.

The shape is the same as in the example where we sampled 3 times from two batched distributions. Since we have defined a multivariate distribution with an off-diagonal covariance matrix (the dimensions are independent) we are getting similar results — compare figure 4 with figure 3.

Time to start bringing both concepts together and defining a batch of multivariate distributions.

We can see now that both `batch_shape`

and `event_shape`

are populated, with values 3 and 2 respectively. It means that we have created 3 2-dimensional Gaussian distributions.

When sampling from the above object we get an output with the following dimensions: `(number_samples, batch_shape, event_shape)`

.

Let’s explore computing the log probability of this object. What is the shape of the output that you are expecting to see?

Since we have 3 distributions (even though they are 2-dimensional), we get an output of `(number_samples, batch_shape)`

. Why?

We can plot samples from the 3 distributions to compare how impactful it is to have different diagonal values in the covariance matrix.

Notice that no correlation arises in this plot for any of the distributions. Even so, the third distribution is extremely elongated in the y-axis direction. This is because the second value in the diagonal is large compared to the first one — the standard deviation of the second dimension is much larger than the first, hence the spread we see in the y direction.

This article covered the first steps to understanding the core objects in TFP — distribution objects. We start by defining univariate distributions and manipulating the `batch_shape`

property of the object as well as testing out different methods: `sample`

, `prob`

and `log_prob`

. Next, we moved to increase the dimensionality of our random variables and introduced multivariate distributions. In this case, we explored ways to work with the`batch_shape`

and`event_shape`

properties of an object, allowing us to store multiple multivariate distributions in a single object.

Next week, we will explore how can we train the parameters of these distribution objects. See you then!

This content was originally published here.