Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Tensorflow.js tf.initializers.constant() Method

  • Last Updated : 06 Aug, 2021

The initializers in Tensorflow.js are used to initialize the starting values of kernel, weights, and baises. The tf.initializers.constant() is initializer function inherited from Initializer base class. This function is used to generate the values initialized to some constant. In this post, we are going to know about tf.initializers.constant() function in Tensorflow.js.

Syntax:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

tf.initializers.constant(args)

Parameters: The args object contains the following props.

  • values: The value for each element in the variable.

Return Value: It returns the tf.initializers.Initializer.

Example 1: In this example, we are going to see standalone use of tf.initializers.constant() function. 



Javascript




// Importing the tensorflow.js library
const tf = require("@tensorflow/tfjs")
 
// Use  tf.initializers.constant() function
var initializer = tf.initializers.constant({ value: 7, })
 
// Print the value of constant
console.log(initializer);

Output:

Constant { value: 7 }

Example 2: In this example, we are going to use the constant() function in model creation to initialize the kernel.

Javascript




// Importing the tensorflow.js library
const tf = require("@tensorflow/tfjs")
 
// Using tf.initializers.constant() function
var initializer = tf.initializers.constant({ value: 7, })
 
// Create model
const model = tf.sequential();
 
// Add layer and initialize the kernel
model.add(tf.layers.dense({
    units: 3,
    activation: 'softmax',
    kernelInitializer: initializer,
    inputShape: [2]
}));
 
// Print the summary
model.summary();

Output:

Layer (type)                 Output shape              Param #   
=================================================================
dense_Dense1 (Dense)         [null,3]                  9
=================================================================
Total params: 9
Trainable params: 9
Non-trainable params: 0
_________________________________________________________________

Reference: https://js.tensorflow.org/api/latest/#initializers.constant




My Personal Notes arrow_drop_up
Recommended Articles
Page :