Tensorflow.js是由Google開發的開源庫,用於在瀏覽器或節點環境中運行機器學習模型以及深度學習神經網絡。
.getLayer() 函數用於獲取基於其名稱(必須是唯一的)或索引的圖層。其中,索引依賴於自下而上的水平圖遍曆順序。此外,如果同時給出名稱和索引,則索引將優先。
用法:
getLayer(name?, index?)
參數:
- name:它是層的規定名稱。它是可選的並且是字符串類型。
- index:它是層的規定索引。它是可選的,類型為 number。
返回值:它返回 tf.layers.Layer。
範例1:
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Defining model
const model = tf.sequential();
// Adding a layer
model.add(tf.layers.dense({units:4, inputShape:[1]}));
// Calling getLayer() method
const layer_0 = model.getLayer(null, 0);
// Printing weights of the layer_0
// using getWeights() method
layer_0.getWeights()[0].print();
輸出:
Tensor [[-0.0678914, 0.6647689, -0.3708572, -0.1764591],]
範例2:
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Defining model
const model = tf.sequential();
// Adding layers
model.add(tf.layers.dense({units:4, inputShape:[1]}));
model.add(tf.layers.dense({units:2, inputShape:[3], activation:'relu6'}));
model.add(tf.layers.dense({units:3, inputShape:[5], activation:'sigmoid'}));
// Calling getLayer() method
const layer_0 = model.getLayer(NaN, 0);
const layer_1 = model.getLayer('denselayer', 1);
const layer_2 = model.getLayer(undefined, 2);
// Printing number of numbers in the weights
// of the layer_0, layer_1, and layer_2
// using countParams() method
console.log(layer_0.countParams());
console.log(layer_1.countParams());
console.log(layer_2.countParams());
輸出:
8 10 9
參考:https://js.tensorflow.org/api/latest/#tf.LayersModel.getLayer
相關用法
- Tensorflow.js tf.Tensor.buffer()用法及代碼示例
- Java String repeat()用法及代碼示例
- Tensorflow.js tf.LayersModel.evaluate()用法及代碼示例
- Tensorflow.js tf.data.Dataset.batch()用法及代碼示例
- Tensorflow.js tf.Sequential.add()用法及代碼示例
- p5.js Element class()用法及代碼示例
注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Tensorflow.js tf.LayersModel class .getLayer() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。