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


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

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

Initializer 類是 Tensorflow.js 中所有初始化器的基類。初始化器用於使用特定值初始化張量。它返回初始化器指定的張量對象。因此,在本文中,我們將了解身份初始值設定項是如何工作的。這是使用單位矩陣初始化新張量對象的初始化程序。它僅用於二維矩陣。

用法:

tf.initializers.identity(Gain)

參數:

  • Gain: 它是適用於單位矩陣的乘法因子。

返回值:它返回 tf.initializers.Initializer



範例1:在本例中,我們將檢查 identity() 函數的獨立使用。

Javascript


// Importing the tensorflow.Js lbrary
import * as tf from "@tensorflow/tfjs"
  
// Generates the identity matrix
const value=tf.initializers.identity(1.0)
  
// Print gain
console.log(value)

輸出:

{
 "gain":1
}

範例2:在此示例中,我們將使用 identity() 和 dense() 函數使用具有密集層的單位矩陣。

Javascript


// Importing the tensorflow.Js lbrary
import * as tf from "@tensorflow/tfjs"
  
// Define the input
const inp = tf.input({shape:[4]});
  
// Create identity matrix with gain 1
const value=tf.initializers.identity(1.0)
  
  
// Dense layer 1
const denseLayer1 = tf.layers.dense({
    units:6, 
    activation:'relu',
    kernelInitialize:value
});
  
// Dense layer 2
const denseLayer2 = tf.layers.dense({
    units:8, 
    activation:'softmax'
});
  
const out = denseLayer2.apply(denseLayer1.apply(inp));
  
//  Model creation
const model = tf.model({inputs:inp,outputs:out});
  
// Make prediction
console.log("Lets Make Some Prediction:")
model.predict(tf.ones([2, 4])).print();

輸出:

Lets Make Some Prediction:
Tensor
   [[0.1651815, 0.1695402, 0.0670628, 0.0771763, 
     0.1045933, 0.1027268, 0.1647871, 0.148932],
    [0.1651815, 0.1695402, 0.0670628, 0.0771763, 
     0.1045933, 0.1027268, 0.1647871, 0.148932]]

參考:https://js.tensorflow.org/api/3.6.0/#initializers.identity




相關用法


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