当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Tensorflow.js tf.io.moveModel()用法及代码示例

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

.moveModel() 函数用于将模型从一个 URL 移到一个新的 URL。此外,该方法有利于在存储介质内部即同种记录介质内或两种存储介质之间即不同类型记录介质之间的移动。

用法:

tf.io.moveModel(sourceURL, destURL)

Parameters: 

  • sourceURL:它是指定的移动源 URL。它是字符串类型。
  • destURL:它是指定的移动目的地 URL。它是字符串类型。

返回值:它返回 ModelArtifactsInfo 的 Promise。



范例1:

  • 在两种不同类型的存储介质之间移动。
  • 使用“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 moveModel() method with its parameters
await tf.io.moveModel(
     'localstorage://display/command/mymodel',
     'indexeddb://display/command/mymodel1');
  
// 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://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://demo/managemen/model1":{
    "dateSaved":"2021-06-24T11:52:29.368Z",
    "modelTopologyType":"JSON",
    "modelTopologyBytes":611,
    "weightSpecsBytes":124,
    "weightDataBytes":44
  },
  "indexeddb://demo/management/model1":{
    "dateSaved":"2021-06-24T13:02:20.265Z",
    "modelTopologyType":"JSON",
    "modelTopologyBytes":614,
    "weightSpecsBytes":126,
    "weightDataBytes":44
  },
  "indexeddb://display/command/mymodel":{
    "dateSaved":"2021-06-24T18:50:50.602Z",
    "modelTopologyType":"JSON",
    "modelTopologyBytes":613,
    "weightSpecsBytes":126,
    "weightDataBytes":252
  },
  "indexeddb://display/command/mymodel1":{
    "dateSaved":"2021-06-24T18:55:00.803Z",
    "modelTopologyType":"JSON",
    "modelTopologyBytes":613,
    "weightSpecsBytes":126,
    "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:[7], stimulation:'prelu'}));
  
// Calling save() method with a storage medium
await mymodel.save('localstorage://display/command/mymodel1');
  
// Calling moveModel() method with its parameters
await tf.io.moveModel(
     'localstorage://display/command/mymodel1',
     'localstorage://display/command/mymodel2');
  
// 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://demo/managemen/model1":{
        "dateSaved":"2021-06-24T11:52:29.368Z",
        "modelTopologyType":"JSON", 
        "modelTopologyBytes":611, 
        "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-24T13:02:20.265Z",
        "modelTopologyType":"JSON", 
        "modelTopologyBytes":614, 
        "weightSpecsBytes":126, 
        "weightDataBytes":44
    },
    "indexeddb://display/command/mymodel":{
        "dateSaved":"2021-06-24T18:50:50.602Z",
        "modelTopologyType":"JSON", 
        "modelTopologyBytes":613, 
        "weightSpecsBytes":126, 
        "weightDataBytes":252
    },
    "indexeddb://display/command/mymodel1":{
        "dateSaved":"2021-06-24T18:59:17.435Z",
        "modelTopologyType":"JSON", 
        "modelTopologyBytes":612, 
        "weightSpecsBytes":125, 
        "weightDataBytes":32
    },
    "indexeddb://example/command/mymodel":{
        "dateSaved":"2021-06-24T12:33:06.208Z",
        "modelTopologyType":"JSON", 
        "modelTopologyBytes":613, 
        "weightSpecsBytes":126, 
        "weightDataBytes":1428
    }
}

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




相关用法


注:本文由纯净天空筛选整理自nidhi1352singh大神的英文原创作品 Tensorflow.js tf.io.moveModel() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。