Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptTensorflow.js tf.truncatedNormal() Function

Tensorflow.js tf.truncatedNormal() Function

Tensorflow.js is an open-source library that is developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment.

The .truncatedNormal() function is used to find tf.Tensor along with the values that are evaluated from a truncated normal distribution. Moreover, the values that are produced here as an output follows a normal distribution with the support of a stated mean and standard deviation, excluding those values whose size is greater than 2 standard deviations against the mean are discarded and chosen again.

Syntax :  

tf.truncatedNormal(shape, mean?, stdDev?, dtype?, seed?)

Parameters:

  • shape: It is an array that contains integer describing the shape of the output tensor and is of type number[].
  • mean: It is the stated mean of the normal distribution and is of type number.
  • stdDev: It is the stated standard deviation of the normal distribution and is of type number.
  • dtype: It is the stated data type of the output tensor returned and can be of type float32 or int32.
  • seed: It is the stated seed that helps in random number generator and is of type number.

Return Value: It returns the tf.Tensor object.

Example 1:  

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Calling truncatedNormal() method and
// Printing output
tf.truncatedNormal([3, 4]).print();


Output:

Tensor
    [[-0.0277713, -0.4777073, -0.3911407, 1.85613   ],
     [-0.0667888, -0.0867875, 0.8295102 , -0.5933844],
     [0.5160138 , 0.7871808 , 0.6818511 , 1.2177598 ]]

Example 2:

Javascript




// Importing the tensorflow.js library 
import * as tf from "@tensorflow/tfjs"
  
// Defining shape
var sh = [3, 2];
var mean = 4;
var st_dev = 5;
var dtyp = 'int32';
  
// Calling truncatedNormal() method
var res = tf.truncatedNormal(sh, mean, st_dev, dtyp);
  
// Printing output
res.print();


Output:

Tensor
    [[-1, -5],
     [4 , 4 ],
     [11, 2 ]]

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

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!

RELATED ARTICLES

Most Popular

Recent Comments