Open In App
Related Articles

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

Improve Article
Save Article
Like Article

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


  • An IOHandler is an object which possesses a save method concerning the accurate signature specified.
  • The save method controls the accumulation or else transmission of sequential data i.e. artifacts which describes the model’s topology as well as weights upon or by means of a particular medium, like local storagefile, file downloads, IndexedDB in the web browser as well as HTTP requests to a server.
  • TensorFlow.js enables IOHandler  implementations in favor of a number of repeatedly utilized saving mediums, like and
  • Moreover, this method also permits us to apply specific kinds of IOHandlers such as URL-like string techniques, like ‘localstorage://’ and ‘indexeddb://’.


save(handlerOrURL, config?)



  • handlerOrURL: The stated instance of IOHandler or else a URL resembling, design based string techniques in favor of IOHandler. It is of type io.IOHandler|string.
  • config: The stated options in order to save the stated model. It is optional and is of type object. It has two arguments under it, as given below:
  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'downloads://mymodel');
// Printing 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'downloads://mymodel', true, true);
// Printing output




Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 22 Aug, 2021
Like Article
Save Article
Similar Reads
Related Tutorials