Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptTensorflow.js tf.randomNormal() Function

Tensorflow.js tf.randomNormal() Function

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments