📜  Tensorflow.js tf.io.copyModel()函数

📅  最后修改于: 2022-05-13 01:56:55.129000             🧑  作者: Mango

Tensorflow.js tf.io.copyModel()函数

Tensorflow.js 是由谷歌开发的开源库,用于在浏览器或节点环境中运行机器学习模型以及深度学习神经网络。

copyModel()函数用于将模型从一个 URL 复制到一个新 URL。此外,该方法支持在存储介质内部,即在相同种类的记录介质内,或在两个不同的存储介质之间,即在不同种类的记录介质之间进行复制。

句法:

tf.io.copyModel(sourceURL, destURL)

参数:此函数有两个参数,如上所示,如下所述:

  • sourceURL:它是一个字符串,表示模型将从中复制的源 URL。
  • destURL:它是一个字符串,表示模型将被复制到的目标 URL。

返回值:它返回一个ModelArtifactsInfo的 Promise。

下面的示例演示了copyModel()函数的使用。

示例:在此示例中,我们将在两种不同的存储介质之间进行复制。刺激“logSigmoid”将用作激活,“Local Storage”和“IndexedDB”作为存储介质。

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
await mymodel.save('localstorage://display/command/mymodel');
  
// Calling copyModel() method with its parameters
await tf.io.copyModel(
     'localstorage://display/command/mymodel',
     'indexeddb://display/command/mymodel');
  
// Calling listModels() method and
// Printing output
console.log(await tf.io.listModels());


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
await mymodel.save('localstorage://display/command/mymodel');
  
// Calling copyModel() method with its parameters
await tf.io.copyModel(
     'localstorage://display/command/mymodel',
     'localstorage://display/command/mymodel1');
  
// Calling listModels() method and
// Printing output
console.log(JSON.stringify(await tf.io.listModels()));


输出:

{
  "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
  }
}

示例 2:在此示例中,我们将在相同种类的存储介质之间进行复制。刺激“prelu”将用作激活,“Local Storage”用作存储介质,“JSON.stringify”以字符串格式返回输出。

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
await mymodel.save('localstorage://display/command/mymodel');
  
// Calling copyModel() method with its parameters
await tf.io.copyModel(
     'localstorage://display/command/mymodel',
     'localstorage://display/command/mymodel1');
  
// Calling listModels() method and
// Printing output
console.log(JSON.stringify(await tf.io.listModels()));

输出:

{
  "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
  }
}

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