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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。