Función Tensorflow.js tf.io.copyModel()

Tensorflow.js es una biblioteca de código abierto desarrollada por Google para ejecutar modelos de aprendizaje automático, así como redes neuronales de aprendizaje profundo en el entorno del navegador o del Node.

La función copyModel() se usa para copiar un modelo de una URL a una nueva. Además, este método admite la copia dentro de un medio de almacenamiento, es decir, dentro del mismo tipo de medio grabado, o entre dos medios de almacenamiento diferentes, es decir, entre diferentes tipos de medios grabados.

Sintaxis:

tf.io.copyModel(sourceURL, destURL)

Parámetros: Esta función tiene dos parámetros, como se indicó anteriormente y se describe a continuación:

  • sourceURL: Es una string que indica la URL de origen desde donde se copiará el modelo.
  • destURL: Es una string que indica la URL de destino donde se copiará el modelo.

Valor devuelto: Devuelve una Promesa de ModelArtifactsInfo .

Los siguientes ejemplos demuestran el uso de la función copyModel() .

Ejemplo: En este ejemplo, copiaremos entre dos tipos diferentes de medios de almacenamiento. Se utilizará la estimulación “logSigmoid” como activación y “Local Storage” e “IndexedDB” como medios de almacenamiento.

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

Producción:

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

Ejemplo 2: En este ejemplo, copiaremos entre los mismos tipos de medios de almacenamiento. La estimulación “prelu” se utilizará como activación y “Local Storage” como medio de almacenamiento y “JSON.stringify” para devolver la salida en formato de string.

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

Producción:

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

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

Publicación traducida automáticamente

Artículo escrito por nidhi1352singh y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *