Open In App

Tensorflow.js tf.Sequential Class

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.

Tensorflow.js tf.Sequential class is a model of the collection of layers in stack form. These layers are connected to the respective neighbor layer. We use tf.Sequential() function to create tf.Sequential class instance. The tf.Sequential class has many methods which are used to apply in instances. 

Syntax: 

Sequential_instance.method(args);

Parameters: This method accepts the following parameter:

  • args: It depends on the method. Different methods accept different parameters.

Return Value: Different method returns different return value tf.Tensor object, etc.

Example 1: In this example, we will see add() method which is used to add a layer at top of the layer. It takes layer as a parameter and returns void. 

Javascript




import * as tf from "@tensorflow/tfjs"
 
// Importing the tensorflow.js library
const tf = require("@tensorflow/tfjs")
 
async function run() {
    // Creating Instance
    const gfg_Instance = tf.sequential();
 
    // Adding first Layer
    const Layer1 = tf.layers.dense({ units: 6, inputShape: [2] });
    gfg_Instance.add(Layer1);
 
    // Adding second layer
    const layer2 = tf.layers.dense({ units: 3, activation: 'sigmoid' })
    gfg_Instance.add(layer2);
 
    // Adding third layer
    const layer3 = tf.layers.dense({ units: 2, activation: 'sigmoid' })
    gfg_Instance.add(layer3);
 
    // Predicting data with layer model.
    const random = tf.randomNormal([4, 2]);
    gfg_Instance.predict(random).print();
}
 
// Function call
run();


 
 

Output: 

 

Tensor
    [[0.5581576, 0.3110509],
     [0.546664 , 0.3369413],
     [0.5634928, 0.2920811],
     [0.5309308, 0.3545613]]

Example 2: In this example, we will see the summary() method which is used to print a summary of layer instance. It takes line length which is the custom length of the summary, and positions which is the width of the summary column, and at last print function which is used to customize the output of the summary. It returns void. 

 

Javascript




// Importing the tensorflow.js library
const tf = require("@tensorflow/tfjs")
 
async function run() {
    // Creating Instance
    const gfg_Instance = tf.sequential();
 
    // Adding first Layer
    const Layer1 = tf.layers.dense({ units: 6, inputShape: [2] });
    gfg_Instance.add(Layer1);
     
    // Adding second layer
    const layer2 = tf.layers.dense({ units: 3, activation: 'sigmoid' })
    gfg_Instance.add(layer2);
     
    // Adding third layer
    const layer3 = tf.layers.dense({ units: 2, activation: 'sigmoid' })
    gfg_Instance.add(layer3);
     
    // Printing summary of layer model
    gfg_Instance.summary();
}
 
// Function call
run();


 
 

Output:

 

_________________________________________________________________
Layer (type)                 Output shape              Param #   
=================================================================
dense_Dense34 (Dense)        [null,6]                  18        
_________________________________________________________________
dense_Dense35 (Dense)        [null,3]                  21        
_________________________________________________________________
dense_Dense36 (Dense)        [null,2]                  8         
=================================================================
Total params: 47
Trainable params: 47
Non-trainable params: 0
_________________________________________________________________

Reference: https://js.tensorflow.org/api/latest/#class:Sequential

 



Last Updated : 12 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads