Open In App

Tensorflow.js tf.truncatedNormal() Function

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

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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads