Open In App

Tensorflow.js tf.initializers.constant() Method

Last Updated : 17 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The initializers in Tensorflow.js are used to initialize the starting values of kernel, weights, and biases. 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:

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



Previous Article
Next Article

Similar Reads

Tensorflow.js tf.initializers.ones() Function
Tensorflow.js is a very well-known machine learning library that used to develop a machine learning model using JavaScript. The main purpose to use this library is to run and deploy a machine learning model directly from the browser or in Node.js. Tensorflow.js is an open-source hardware-accelerated JavaScript library. In this article, we're going
2 min read
Tensorflow.js tf.initializers.glorotUniform() 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 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.glorotUniform() function extracts sam
2 min read
Tensorflow.js tf.initializers.glorotNormal() 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 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.glorotNormal() function extract sampl
2 min read
Tensorflow.js tf.initializers.heUniform() 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 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.heUniform() function draws samples fr
2 min read
Tensorflow.js tf.initializers.leCunNormal() 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 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.leCunNormal() function extracts sampl
2 min read
Tensorflow.js tf.initializers.leCunUniform() 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 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.leCunUniform() function takes samples
2 min read
Tensorflow.js tf.initializers.identity() 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. The Initializer class is the base class of all initializers in Tensorflow.js. The initializers are used to initialize the Tensors with the specific values. It returns the tensor object
2 min read
Tensorflow.js tf.initializers.truncatedNormal() 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 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.truncatedNormal() function produces r
2 min read
Tensorflow.js tf.initializers.varianceScaling() 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 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.varianceScaling() function is capable
2 min read
Tensorflow.js tf.initializers.orthogonal() 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 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.orthogonal() function produces a rand
2 min read