Open In App

Tensorflow.js tf.GraphModel class .save() Method

Introduction: Tensorflow.js is an open-source library that is developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment.

The .save() function is used to save the structure and/or the weights of the stated GraphModel



Note:

Syntax:



save(handlerOrURL, config?)

 

Parameters:  

  1. trainableOnly: It states if only the trainable weights of the stated model is saved, overlooking the non-trainable weights. It is of type Boolean and defaults to false.
  2. includeOptimizer: It states if the stated optimizer will be stored or not. It is of type Boolean and defaults to false.

Return Value: It returns promise of io.SaveResult.

Example 1:




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining model url
const model_Url =
  
// Calling the loadGraphModel() method
const mymodel = await tf.loadGraphModel(model_Url);
  
// Calling save() method
const output = await mymodel.save('downloads://mymodel');
  
// Printing output
console.log(output)

Output:

{
  "modelArtifactsInfo": {
    "dateSaved": "2021-08-19T12:00:15.603Z",
    "modelTopologyType": "JSON",
    "modelTopologyBytes": 90375,
    "weightSpecsBytes": 15791,
    "weightDataBytes": 13984940
  }
}

Example 2:




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Calling the loadGraphModel() method
const mymodel = await tf.loadGraphModel(
  
// Calling save() method with all its
// parameters
const output = await mymodel.save('downloads://mymodel', true, true);
  
// Printing output
console.log(JSON.stringify(output))

Output:

{"modelArtifactsInfo":{"dateSaved":"2021-08-19T12:05:35.906Z",
"modelTopologyType":"JSON","modelTopologyBytes":90375,
"weightSpecsBytes":15791,"weightDataBytes":13984940}}

Reference: https://js.tensorflow.org/api/latest/#tf.GraphModel.save


Article Tags :