Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Tensorflow.js tf.LayersModel class .summary() Method

  • Last Updated : 21 Jul, 2021

The tf.LayersModel is a class used for training, inference, and evaluation of layers model in tensorflow.js. It contains methods for training, evaluation, prediction, and for saving of layers model purposes. So in this post, we are going to know about the model.summary() function.

The model.summary() function in tensorflow.js prints the summary for the model it includes the name of the model, numbers of weight parameters,  numbers of trainable parameters.

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!

Syntax:

model_name.summary (line length, position, print function)

Parameters: All the parameters are optional.



  • line length: It is a custom line length in a number of characters.
  • position: It is an array that showing widths for each column, values can be fractional or absolute.
  • print function: function which is printing the summary for model, default function is console.log().

Returns: Void.

Example 1:  In this example, we are going to create the sequential model with single dense layers and printing the summary for the model using model.summary() function.

Javascript




// Importing the tensorflow.Js library
import * as tf from "@tensorflow/tfjs"
 
// Creating model
var myModel = tf.sequential({
    layers:[tf.layers.dense({
        units: 10,
        inputShape: [15]
    })]
});
 
// Print the summary
myModel.summary();

Output:

_________________________________________________________________
Layer (type)                 Output shape              Param #    
=================================================================
dense_Dense8 (Dense)         [null,10]                 160        
=================================================================
Total params: 160
Trainable params: 160
Non-trainable params: 0
_________________________________________________________________

Example 2: In this example, we are going to create the model with 2 dense layers having activation function relu and softmax using tf.model method and making predictions also printing the summary for the model.

Javascript




// Importing the tensorflow.Js library
import * as tf from "@tensorflow/tfjs"
 
// Define input
var inp=tf.input({shape:[8]});
 
// Dense layer 1
var denseLayerOne=tf.layers.dense({units:7,activation:'relu'});
 
// Dense layer 1
var denseLayerTwo=tf.layers.dense({units:5, activation:'softmax'});
 
// Generate the output
var out=denseLayerTwo.apply(denseLayerOne.apply(inp));
 
// Model creation
var myModel=tf.model({inputs:inp,outputs:out});
 
// Make prediction
console.log("\nPrediction :")
myModel.predict(tf.ones([3,8])).print();
console.log("\nSummary :")
myModel.summary();

Output:

Prediction :
Tensor
   [[0.2074656, 0.1515629, 0.2641615, 0.2237201, 0.1530899],
    [0.2074656, 0.1515629, 0.2641615, 0.2237201, 0.1530899],
    [0.2074656, 0.1515629, 0.2641615, 0.2237201, 0.1530899]]

Summary :
_________________________________________________________________
Layer (type)                 Output shape              Param #    
=================================================================
input7 (InputLayer)          [null,8]                  0          
_________________________________________________________________
dense_Dense19 (Dense)        [null,7]                  63        
_________________________________________________________________
dense_Dense20 (Dense)        [null,5]                  40        
=================================================================
Total params: 103
Trainable params: 103
Non-trainable params: 0
_________________________________________________________________

Reference: https://js.tensorflow.org/api/latest/#tf.LayersModel.summary




My Personal Notes arrow_drop_up
Recommended Articles
Page :