Tutorial Example

Implement Normal Distribution Using tf.distributions.Normal() in TensorFlow

Normal Distribution (Gaussian Distribution) is widely used in deep learning model. In this tutorial, we will introduce how to compute it in tensorflow.

Normal Distribution can be defined as:

Here is the tutorial:

An Introduction to Gaussian Distribution or Normal Distribution

In tensorflow, we can use tf.distributions.Normal() to compute it.

Syntax

tf.distributions.Normal() is defined as:

__init__(
    loc,
    scale,
    validate_args=False,
    allow_nan_stats=True,
    name='Normal'
)

Parameters are:

loc: the mean of distribution, which is \(\mu\).

scale: the standard deviation of distribution, which is \(\sigma\).

How to use tf.distributions.Normal()?

We will use some examples to show you how to do.

As to normal distribution\(f(x) = N(x)\)

Example 1:

If we have got the \(x\) value, how to compute its probability?

import tensorflow as tf
import numpy as np
x = tf.convert_to_tensor(np.array([0.0, 1.0]), dtype = tf.float32)

In this code, we have got \(x = 0.0\) or \(x = 1.0\), how to get the probability \(P(x \leqslant 0.0)\) and \(P(x \leqslant 1.0)\)?

First, we can build a standard normal distribution.

#mean = 0, standard deviation = 1.0
std_norm = tf.distributions.Normal(0.0, 1.0)

Then we can use std_norm.cdf() to compute.

att2 = std_norm.cdf(x) #compute probility of x<=0 and x<=1.0

Run this code, you will get the value:

[0.5    0.8413]

It means:

\(P(x \leqslant 0.0) = 0.5\)

\(P(x \leqslant 1.0) = 0.8413\)

Example 2:

If we have got \(x\) value, how to get the value of \(f(x)\)?

We can use std_norm.prob() to compute.

Here is the example code:

att = std_norm.prob(x)

Run this code, we will get the value:

[0.3989 0.242 ]

It means, in standard normal distribution

\(f(0.0) = 0.3989\)

\(f(1.0) = 0.242\)

Example 3:

If we have got the probability value \(P(x)\), how to get the \(x\) value?

For example, if \(P(x) = 0.8413\), how about \(x\)?

We can use std_norm.quantile() to compute.

x_v = std_norm.quantile(att2)

Run this code, we will get the value:

[0. 1.]

It means \(x = [0., 1]\).

Here is the full example code:

import tensorflow as tf
import numpy as np

x = tf.convert_to_tensor(np.array([0.0, 1.0]), dtype = tf.float32)
#mean = 0, standard deviation = 1.0
std_norm = tf.distributions.Normal(0.0, 1.0)
att = std_norm.prob(x)
att2 = std_norm.cdf(x) #compute probility of x<0 and x<1.0

x_v = std_norm.quantile(att2)

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)
    print(sess.run(x))
    print(sess.run(att))
    print(sess.run(att2))
    print(sess.run(x_v))