Tensorflow.js是Google開發的開源庫,用於在瀏覽器或節點環境中運行機器學習模型和深度學習神經網絡。
.compile() 函數為訓練和評估過程配置和製作模型。通過調用 .compile() 函數,我們為模型準備了優化器、損失和指標。 .compile() 函數將參數對象作為參數。
注意:如果在未編譯的模型上調用 .fit() 或 .evaluate() 函數,則程序將拋出錯誤。
用法:
tf.model.compile({optimizer, loss}, metrics=[])
參數:
- optimizer:它是一個強製參數。它接受 tf.train.Optimizer 的對象或優化器的字符串名稱。以下是優化器的字符串名稱 - “sgd”、“adam”、“adamax”、“adadelta”、“adagrad”、“rmsprop”、“momentum”。
- loss:它是一個強製參數。它接受損失類型的字符串值或字符串數組。如果我們的模型有多個輸出,我們可以通過傳遞一組損失對每個輸出使用不同的損失。模型將最小化的損失值將是所有單個損失的總和。以下是損失的字符串名稱——“meanSquaredError”、“meanAbsoluteError” 等。
- metrics:它是一個可選參數。它接受模型在訓練和測試階段評估的指標列表。通常,我們使用metrics=[‘accuracy’]。要為 multi-output 模型的不同輸出指定不同的度量,我們還可以傳遞字典。
返回值:由於它為訓練準備模型,因此它不會返回任何內容。 (即返回類型為空)
範例1:在這個例子中,我們將創建一個簡單的模型,我們將傳遞優化器和損失參數的值。這裏我們使用優化器作為 “adam” 和損失作為 “meanSquaredError”。
Javascript
// Importing the tensorflow.js library
const tf = require("@tensorflow/tfjs");
// define the model
const model = tf.sequential({
layers:[tf.layers.dense({ units:1, inputShape:[10] })],
});
// compile the model
// using "adam" optimizer and "meanSquaredError" loss
model.compile({ optimizer:"adam", loss:"meanSquaredError" });
// evaluate the model which was compiled above
// computation is done in batches of size 4
const result = model.evaluate(tf.ones([8, 10]), tf.ones([8, 1]), {
batchSize:4,
});
// print the result
result.print();
輸出:
Tensor 2.6806342601776123
範例2:在這個例子中,我們將創建一個簡單的模型,我們將傳遞優化器、損失和指標參數的值。這裏我們使用優化器作為 “sgd”,損失作為 “meanAbsoluteError” 和 “accuracy” 作為指標。
Javascript
// Importing the tensorflow.js library
const tf = require("@tensorflow/tfjs");
// define the model
const model = tf.sequential({
layers:[tf.layers.dense({ units:1, inputShape:[10] })],
});
// compile the model
// using "adam" optimizer, "meanSquaredError" loss and "accuracy" metrics
model.compile(
{ optimizer:"adam", loss:"meanSquaredError" },
(metrics = ["accuracy"])
);
// evaluate the model which was compiled above
// computation is done in batches of size 4
const result = model.evaluate(tf.ones([8, 10]), tf.ones([8, 1]), {
batchSize:4,
});
// print the result
result.print();
輸出:
Tensor 1.4847172498703003
範例3:在這個例子中,我們將創建一個簡單的模型,我們將傳遞優化器、損失和指標參數的值。這裏我們使用優化器作為 “sgd”,損失作為 “meanAbsoluteError” 和 “precision” 作為指標。
Javascript
// Importing the tensorflow.js library
const tf = require("@tensorflow/tfjs");
// define the model
const model = tf.sequential({
layers:[tf.layers.dense({ units:1, inputShape:[10] })],
});
// compile the model
// using "adam" optimizer, "meanSquaredError" loss and "accuracy" metrics
model.compile(
{ optimizer:"sgd", loss:"meanAbsoluteError" },
(metrics = ["precision"])
);
// evaluate the model which was compiled above
// computation is done in batches of size 4
const result = model.evaluate(tf.ones([8, 10]), tf.ones([8, 1]), {
batchSize:4,
});
// print the result
result.print();
輸出:
Tensor 1.507279634475708
參考:https://js.tensorflow.org/api/latest/#tf.LayersModel.compile
相關用法
- Javascript compile()用法及代碼示例
- Node.js Date.compile()用法及代碼示例
- Tensorflow.js tf.Tensor.buffer()用法及代碼示例
- Java String repeat()用法及代碼示例
- Tensorflow.js tf.LayersModel.evaluate()用法及代碼示例
- Tensorflow.js tf.data.Dataset.batch()用法及代碼示例
- Tensorflow.js tf.Sequential.add()用法及代碼示例
注:本文由純淨天空篩選整理自girishthatte大神的英文原創作品 Tensorflow.js tf.LayersModel class .compile() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。