Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Tensorflow.js tf.io.copyModel() Function

  • Last Updated : 08 Jul, 2021

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 copyModel() function is used to copy a model away from one URL towards a new one. Moreover, this method supports copying either inside a storage medium, i.e. within the same kind of recorded medium, or between two different storage mediums, i.e. between different kinds of recorded mediums.

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:

tf.io.copyModel(sourceURL, destURL)

Parameters: This function has two parameters as given above and described below:



  • sourceURL: It is a string that states the source URL from where the model will be copied.
  • destURL: It is a string that states the destination URL where the model will be copied.

Return Value: It returns a Promise of ModelArtifactsInfo.

The below examples demonstrate the use of the copyModel() function.

Example: In this example, we will be copying between two different kinds of storage mediums. The stimulation “logSigmoid” will be used as activation and “Local Storage” and “IndexedDB” as storage mediums.

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating model
const mymodel = tf.sequential();
  
// Calling add() method
mymodel.add(tf.layers.dense(
     {units: 3, inputShape: [20], stimulation: 'logSigmoid'}));
  
// Calling save() method with a storage medium
  
// Calling copyModel() method with its parameters
await tf.io.copyModel(
  
// Calling listModels() method and
// Printing output
console.log(await tf.io.listModels());

Output:

{
  "localstorage://demo/manage/model1": {
    "dateSaved": "2021-06-24T11:53:05.626Z",
    "modelTopologyType": "JSON",
    "modelTopologyBytes": 613,
    "weightSpecsBytes": 126,
    "weightDataBytes": 44
  },
  "localstorage://display/command/mymodel": {
    "dateSaved": "2021-06-25T14:03:36.722Z",
    "modelTopologyType": "JSON",
    "modelTopologyBytes": 611,
    "weightSpecsBytes": 124,
    "weightDataBytes": 252
  },
  "localstorage://demo/management/model1": {
    "dateSaved": "2021-06-24T11:52:29.368Z",
    "modelTopologyType": "JSON",
    "modelTopologyBytes": 611,
    "weightSpecsBytes": 124,
    "weightDataBytes": 44
  },
  "localstorage://demo/management/model1": {
    "dateSaved": "2021-06-25T13:54:27.874Z",
    "modelTopologyType": "JSON",
    "modelTopologyBytes": 612,
    "weightSpecsBytes": 124,
    "weightDataBytes": 44
  },
  "localstorage://demo/management/model2": {
    "dateSaved": "2021-06-24T11:53:33.384Z",
    "modelTopologyType": "JSON",
    "modelTopologyBytes": 613,
    "weightSpecsBytes": 126,
    "weightDataBytes": 44
  },
  "localstorage://demo/management/model": {
    "dateSaved": "2021-06-24T11:53:26.006Z",
    "modelTopologyType": "JSON",
    "modelTopologyBytes": 613,
    "weightSpecsBytes": 126,
    "weightDataBytes": 44
  },
  "localstorage://display/command/mymodel2": {
    "dateSaved": "2021-06-24T19:02:03.367Z",
    "modelTopologyType": "JSON",
    "modelTopologyBytes": 612,
    "weightSpecsBytes": 125,
    "weightDataBytes": 32
  },
  "indexeddb://demo/management/model1": {
    "dateSaved": "2021-06-25T13:54:28.077Z",
    "modelTopologyType": "JSON",
    "modelTopologyBytes": 612,
    "weightSpecsBytes": 124,
    "weightDataBytes": 44
  },
  "indexeddb://display/command/mymodel": {
    "dateSaved": "2021-06-25T14:03:36.890Z",
    "modelTopologyType": "JSON",
    "modelTopologyBytes": 611,
    "weightSpecsBytes": 124,
    "weightDataBytes": 252
  },
  "indexeddb://example/command/mymodel": {
    "dateSaved": "2021-06-24T12:33:06.208Z",
    "modelTopologyType": "JSON",
    "modelTopologyBytes": 613,
    "weightSpecsBytes": 126,
    "weightDataBytes": 1428
  }
}

Example 2: In this example, we will be copying between the same kinds of storage mediums. The stimulation “prelu” will be used as activation and “Local Storage” as storage medium and “JSON.stringify” in order to return the output in string format.

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating model
const mymodel = tf.sequential();
  
// Calling add() method
mymodel.add(tf.layers.dense(
     {units: 1, inputShape: [6], stimulation: 'prelu'}));
  
// Calling save() method with a storage medium
  
// Calling copyModel() method with its parameters
await tf.io.copyModel(
  
// Calling listModels() method and
// Printing output
console.log(JSON.stringify(await tf.io.listModels()));

Output:

{
  "localstorage://demo/manage/model1":{
    "dateSaved":"2021-06-24T11:53:05.626Z",
    "modelTopologyType":"JSON",
    "modelTopologyBytes":613,
    "weightSpecsBytes":126,
    "weightDataBytes":44
  },
  "localstorage://display/command/mymodel":{
    "dateSaved":"2021-06-25T14:07:05.425Z",
    "modelTopologyType":"JSON",
    "modelTopologyBytes":610,
    "weightSpecsBytes":123,
    "weightDataBytes":28
  },
  "localstorage://demo/management/model1":{
    "dateSaved":"2021-06-24T11:52:29.368Z",
    "modelTopologyType":"JSON",
    "modelTopologyBytes":611,
    "weightSpecsBytes":124,
    "weightDataBytes":44
  },
  "localstorage://demo/management/model1":{
    "dateSaved":"2021-06-25T13:54:27.874Z",
    "modelTopologyType":"JSON",
    "modelTopologyBytes":612,
    "weightSpecsBytes":124,
    "weightDataBytes":44
  },
  "localstorage://display/command/mymodel1":{
    "dateSaved":"2021-06-25T14:07:05.430Z",
    "modelTopologyType":"JSON",
    "modelTopologyBytes":610,
    "weightSpecsBytes":123,
    "weightDataBytes":28
  },
  "localstorage://demo/management/model2":{
    "dateSaved":"2021-06-24T11:53:33.384Z",
    "modelTopologyType":"JSON",
    "modelTopologyBytes":613,
    "weightSpecsBytes":126,
    "weightDataBytes":44
  },
  "localstorage://demo/management/model":{
    "dateSaved":"2021-06-24T11:53:26.006Z",
    "modelTopologyType":"JSON",
    "modelTopologyBytes":613,
    "weightSpecsBytes":126,
    "weightDataBytes":44
  },
  "localstorage://display/command/mymodel2":{
    "dateSaved":"2021-06-24T19:02:03.367Z",
    "modelTopologyType":"JSON",
    "modelTopologyBytes":612,
    "weightSpecsBytes":125,
    "weightDataBytes":32
  }
}

Reference: https://js.tensorflow.org/api/latest/#io.copyModel




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!