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


Tensorflow.js tf.GraphModel.execute()用法及代碼示例

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

.execute() 方法用於為指定的輸入張量實現有利於給定模型的蘊涵。

用法:

execute(inputs, outputs?)

Parameters: 

  • inputs:它是指定的張量或張量數組或有利於模型的輸入張量圖,通過輸入節點指定處理。它的類型是 (tf.Tensor|tf.Tensor[]|{[name:string]:tf.Tensor})。
  • outputs:它是來自所述張量流模型的所述輸出節點名稱。如果未規定輸出,則必須應用規定模型的默認輸出。此外,我們可以通過將它們附加到輸出數組來分析指定模型的節點之間。它是 string 或 string[] 類型。

返回值:它返回 tf.Tensor 或 tf.Tensor[]。



範例1:在此示例中,我們從 URL 加載 MobileNetV2。

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining tensor input elements
const model_Url =
'https://storage.googleapis.com/tfjs-models/savedmodel/mobilenet_v2_1.0_224/model.json';
  
// Calling the loadGraphModel() method
const mymodel = await tf.loadGraphModel(model_Url);
  
// Defining inputs
const inputs = tf.zeros([1, 224, 224, 3]);
  
// Calling execute() method and 
// Printing output
mymodel.execute(inputs).print();

輸出:

Tensor
     [[-0.1800361, -0.4059965, 0.8190175, 
     ..., 
     -0.8953396, -1.0841646, 1.2912753],]

範例2:在本例中,我們從 TF Hub URL 加載 MobileNetV2。

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining tensor input elements
const model_Url =
'https://tfhub.dev/google/imagenet/mobilenet_v2_140_224/classification/2';
  
// Calling the loadGraphModel() method
const mymodel = await tf.loadGraphModel(
        model_Url, {fromTFHub:true});
  
// Defining inputs
const inputs = tf.zeros([1, 224, 224, 3]);
  
// Defining outputs
const outputs = "module_apply_default/MobilenetV2/Logits/output";
  
// Calling execute() method and 
// Printing output
mymodel.execute(inputs, outputs).print();

輸出:

Tensor
     [[-1.1690605, 0.0195426, 1.1962479, 
     ..., 
     -0.4825858, -0.0055641, 1.1937635],]

參考: https://js.tensorflow.org/api/latest/#tf.GraphModel.execute




相關用法


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