當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Tensorflow.js tf.io.copyModel()用法及代碼示例

Tensorflow.js是由Google開發的開源庫,用於在瀏覽器或節點環境中運行機器學習模型以及深度學習神經網絡。

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());

輸出:

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




相關用法


注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Tensorflow.js tf.io.copyModel() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。