9951 explained code solutions for 126 technologies


python-tensorflowHow can I generate a random normal distribution using Python and TensorFlow?


Generating a random normal distribution using Python and TensorFlow is quite easy. To do so, we can use the tf.random.normal() function. This function takes two arguments: the shape of the output tensor and the mean and standard deviation of the normal distribution.

For example, the following code block generates a random normal distribution with a mean of 0 and a standard deviation of 1:

import tensorflow as tf

normal_dist = tf.random.normal(shape=[2,3], mean=0, stddev=1)

print(normal_dist)

The output of the above code is:

tf.Tensor(
[[-1.0422163  -1.1837213   0.93375015]
 [ 0.09916094 -0.7462523  -1.2795391 ]], shape=(2, 3), dtype=float32)

The code can be broken down into the following parts:

  1. Import the TensorFlow library: import tensorflow as tf
  2. Generate the random normal distribution: normal_dist = tf.random.normal(shape=[2,3], mean=0, stddev=1)
  3. Print the generated distribution: print(normal_dist)

For more information on the tf.random.normal() function, please refer to the following link:

https://www.tensorflow.org/api_docs/python/tf/random/normal

Edit this code on GitHub