Tensorflow.js是Google開發的開源庫,用於在瀏覽器或節點環境中運行機器學習模型和深度學習神經網絡。它還可以幫助開發人員使用JavaScript語言開發ML模型,並可以直接在瀏覽器或Node.js中使用ML。
tf.layers.Layer.getWeights() 函數用於獲取張量的權重值。
用法:
getWeights( trainableOnly? )
參數:
- trainableOnly(boolean):如果為 true,該函數將僅返回可訓練的權重值。
返回值:它返回一個 tf.Tensor
範例1:
Javascript
// Creating a model
const model = tf.sequential();
// Adding layers
model.add(tf.layers.dense({units:2, inputShape:[5]}));
model.add(tf.layers.dense({units:3}));
model.compile({loss:'categoricalCrossentropy', optimizer:'sgd'});
// Printing the weights of the layers
model.layers[0].getWeights()[0].print()
model.layers[0].getWeights()[1].print()
輸出:
Tensor [[-0.4756567, 0.2925433 ], [0.3505997 , -0.5043278], [0.5344347 , 0.2662918 ], [-0.1357223, 0.2435055 ], [-0.6059403, 0.1990891 ]] Tensor [0, 0]
範例2:
Javascript
const tf = require("@tensorflow/tfjs")
// Creating a model
const model = tf.sequential();
// Adding layers
model.add(tf.layers.dense({units:1, inputShape:[10]}));
model.add(tf.layers.dense({units:3}));
// Setting new weights
model.layers[0].setWeights([tf.zeros([10, 1]), tf.ones([1])]);
model.compile({loss:'categoricalCrossentropy', optimizer:'sgd'});
// Printing the weights of the layers
model.layers[0].getWeights()[0].print()
model.layers[0].getWeights()[1].print()
輸出:
Tensor [[0], [0], [0], [0], [0], [0], [0], [0], [0], [0]] Tensor [1]
參考:https://js.tensorflow.org/api/latest/#tf.layers.Layer.getWeights
相關用法
- Lodash _.method()用法及代碼示例
- Node.js Http2ServerRequest.method用法及代碼示例
- Node.js http.IncomingMessage.method用法及代碼示例
- Javascript dataView.getInt16()用法及代碼示例
- Collect.js toArray()用法及代碼示例
- Javascript RegExp toString()用法及代碼示例
- Node.js URLSearchParams.has()用法及代碼示例
- Node.js hmac.update()用法及代碼示例
- HTML DOM isEqualNode()用法及代碼示例
- JavaScript Date toLocaleTimeString()用法及代碼示例
- Tensorflow.js tf.Tensor.buffer()用法及代碼示例
- Node.js crypto.createHash()用法及代碼示例
- Node.js process.send()用法及代碼示例
- Node.js writeStream.clearLine()用法及代碼示例
- HTML DOM History go()用法及代碼示例
- Node.js fs.link()用法及代碼示例
- Java ArrayList toArray()用法及代碼示例
- JavaScript Math random()用法及代碼示例
- JavaScript Math round()用法及代碼示例
- Javascript toFixed()用法及代碼示例
- Javascript toPrecision()用法及代碼示例
注:本文由純淨天空篩選整理自parasmadan15大神的英文原創作品 Tensorflow.js tf.layers getWeights() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。