Tensorflow.js是由Google開發的開源庫,用於在瀏覽器或節點環境中運行機器學習模型以及深度學習神經網絡。
.build() 函數用於創建所述層的權重。這種方法應該應用於持有權重的每一層。此外,它在調用 apply() 方法以構建權重時調用。
用法:
build(inputShape)
參數:
- inputShape:它是未觸及的規定形狀或形狀陣列。
返回值:它返回void。
範例1:
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Creating a model
const model = tf.sequential();
// Adding a layer
model.add(tf.layers.dense({units:1, inputShape:[3]}));
// Defining input
const input = tf.input({shape:[6, 2, 6]});
// Calling build method with its
// parameter
model.layers[0].build([input.Shape]);
// Printing output
console.log(JSON.stringify(input.shape));
model.layers[0].getWeights()[0].print();
輸出:
[null,6,2,6] Tensor [[-0.3726568], [0.7343086 ], [-0.2459907]]
這裏使用 getWeights() 方法打印權重。
範例2:
Javascript
// Importing the tensorflow.js library
//import * as tf from "@tensorflow/tfjs"
// Creating a model
const model = tf.sequential();
// Adding layers
model.add(tf.layers.dense({units:1, inputShape:[2]}));
model.add(tf.layers.dense({units:2}));
// Defining inputs
const input1 = tf.input({shape:[1, 2]});
const input2 = tf.input({shape:[1.7, 2.7, 6.5]});
// Calling build method with its
// parameter
model.layers[0].build([input1.Shape]);
model.layers[1].build([input2.Shape]);
// Printing outputs
console.log(JSON.stringify(input1.shape));
console.log(JSON.stringify(input2.shape));
model.layers[0].getWeights()[0].print();
model.layers[1].getWeights()[0].print();
輸出:
[null,1,2] [null,1.7,2.7,6.5] Tensor [[0.6224715], [1.2144204]] Tensor [[0.8342852, 0.4770206],]
參考: https://js.tensorflow.org/api/latest/#tf.layers.Layer.build
注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Tensorflow.js tf.layers build() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。