Open In App

Tensorflow.js tf.randomNormal() Function

Last Updated : 20 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Tensorflow.js is an open-source library developed by Google for running machine learning models and deep learning neural networks in the browser or node environment. It helps developers to develop ML models in JavaScript, and use ML directly in the browser or in Node.js.

The tf.randomNormal() function is used to create a tf.Tensor with values sampled from a normal distribution.

Syntax:

tf.randomNormal (shape, mean, stdDev, dtype, seed)

Parameters:

  • shape: An array of integers defining the shape of the output tensor.
  • mean: It is an optional argument. The mean of the normal distribution.
  • stdDev: It is also an optional argument. The standard deviation of the normal distribution.
  • dtype: The data type of the output. The values of datatype possible are ‘float32’ or ‘int32’. It is also an optional argument.
  • seed: It is an optional argument. The seed for the random number generator.

Return Value: It returns tf.Tensor.

Example 1:

Javascript




// Creating the tensor with values 
// sampled from a normal distribution
const x = tf.randomNormal([5]);
  
// Printing the tensor
x.print();


Output:

Tensor
   [1.5322036, 2.2685387, -0.4921667, 1.1309422, 1.470457]

Example 2:

Javascript




// Creating the tensor with values 
// sampled from a normal distribution
const x = tf.randomNormal([2, 2]);
  
// Printing the tensor
x.print();


Output:

Tensor
   [[1.9162624 , -0.9760998],
    [-0.2262698, -2.1717837]]

Example 3

Javascript




// Creating the tensor with values 
// sampled from a normal distribution
const x=tf.randomNormal([5], 5, 1, 'int32', 2);
  
// Printing the tensor
x.print();


Output:

​Tensor
   [5, 7, 6, 5, 6]

Reference: https://js.tensorflow.org/api/latest/#randomNormal



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads