Open In App

Tensorflow.js tf.initializers.randomUniform() Function

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 also helps the developers to develop ML models in JavaScript language and can use ML directly in the browser or in Node.js.

The tf.initializers.randomUniform() function generates random values that is initialized to a uniform distribution. The values are distributed uniformly between the configured min-value and max-value.

Syntax:

tf.initializers.randomUniform(arguments)

Parameters:

  • arguments: It is an object that contains 3 key-values listed below:
    1. mean: It is the mean of the random values to be generated.
    2. stddev: It is the standard deviation of the random values to be generated.
    3. seed: It is the random number generator seed.

Returns value: It returns tf.initializers.Initializer

Example 1:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
 
// Initializing the .initializers.randomUniform() function
let geek = tf.initializers.randomUniform(5)
 
// Printing gain value
console.log(geek);
 
// Printing individual gain value.
console.log('\nIndividual values:\n');
console.log(geek.DEFAULT_MINVAL);
console.log(geek.DEFAULT_MAXVAL);
console.log(geek.minval);
console.log(geek.maxval);


Output:

{
  "DEFAULT_MINVAL": -0.05,
  "DEFAULT_MAXVAL": 0.05,
  "minval": -0.05,
  "maxval": 0.05
}

Individual values:

-0.05
0.05
-0.05
0.05

Example 2:

Javascript




// Importing the tensorflow.Js library
import * as tf from "@tensorflow/tfjs
 
 
// Defining the input value
const inputValue = tf.input({shape:[4]});
 
// Initializing tf.initializers.randomUniform() function.
const funcValue = tf.initializers.randomUniform(8)
 
// Creating dense layer 1
const dense_layer_1 = tf.layers.dense({
    units: 5,
    activation: 'relu',
    kernelInitialize: funcValue
});
 
// Creating dense layer 2
const dense_layer_2 = tf.layers.dense({
    units: 7,
    activation: 'softmax'
});
 
// Output
const outputValue = dense_layer_2.apply(
    dense_layer_1.apply(inputValue)
);
 
// Creation the model.
const model = tf.model({
    inputs: inputValue,
    outputs: outputValue
});
 
// Predicting the output.
model.predict(tf.ones([2, 4])).print();


Output:

Tensor
    [[0.1145501, 0.133405, 0.0640167, 0.2349582, 
     0.1064994, 0.0799759, 0.2665946],
     [0.1145501, 0.133405, 0.0640167, 0.2349582, 
      0.1064994, 0.0799759, 0.2665946]]

Reference: https://js.tensorflow.org/api/3.6.0/#initializers.randomUniform



Last Updated : 31 Aug, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads