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


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

深度學習中的模型是連接層的集合,可以訓練、評估並用於預測某些東西。要執行此操作,您需要實例化模型的輸入。在這篇文章中,我們將了解輸入工廠函數的工作原理。

tf.input() 函數在使用 tf.model() 函數創建模型時使用。

用法:

tf.input(Args) 

參數:Args 對象包含以下道具。

  • Shape:它表示預期的輸入將是 32 維向量的批次。
  • batchShape:它表示形狀元組,包括批量大小。
  • name:它代表層的名稱。
  • dtype:它用於表示輸入的類型。
  • sparse:布爾值表示創建的占位符是稀疏的。

返回值:它返回 tf.SymbolicTensor。



範例1:在此示例中,我們將使用默認參數形狀。

Javascript


// Importing the tensorflow.Js library
const tf = require("@tensorflow/tfjs")
// Define input
const inp = tf.input({ shape:[64] });
// Define op
const op = tf.layers.dense({ units:8, activation:'softmax' }).apply(inp);
// Create model and pass inp and op
const model = tf.model({ inputs:inp, outputs:op });
// Predict something
model.predict(tf.ones([2, 64])).print();

輸出:

Tensor
   [[0.0285837, 0.1409771, 0.1021329, 0.0912676, 0.2361873, 0.0262359, 
   0.2991393, 0.0754762],
    [0.0285837, 0.1409771, 0.1021329, 0.0912676, 0.2361873, 0.0262359, 
    0.2991393, 0.0754762]]

範例2:在此示例中,我們將使用所有參數 shape、name、type 和 sparse。

Javascript


// Importing the tensorflow.js library
const tf = require("@tensorflow/tfjs")
// Define input and pass all parameters
const inp = tf.input({ shape:[16] }, { name:'abc' },
    { dtype:'float32' }, { sparse:false });
// Define op
const op = tf.layers.dense({ units:2, activation:'softmax' }).apply(inp);
// Create model and pass inp and op
const model = tf.model({ inputs:inp, outputs:op });
// Predict something
model.summary();

輸出:

Layer (type)                 Output shape              Param #    
=================================================================
input8 (InputLayer)          [null,16]                 0          
_________________________________________________________________
dense_Dense8 (Dense)         [null,2]                  34        
=================================================================
Total params:34
Trainable params:34
Non-trainable params:0
_________________________________________________________________

參考文獻:https://js.tensorflow.org/api/latest/#input




相關用法


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