Tensorflow.js 是 Google 開發的一個 javascript 庫,用於在瀏覽器或 Node.js 中運行和訓練機器學習模型。
Adam 優化器(或 Adaptive Moment Estimation)是一種基於 first-order 和 second-order 矩的自適應估計的隨機梯度下降方法。在處理大量數據和參數時,優化技術非常有效。有關更多詳細信息,請參閱這篇文章。
在 Tensorflow.js 中使用了 tf.train.adam() 函數,它創建了使用 adam 算法的 tf.AdamOptimizer。
用法:
tf.train.adam (learningRate?, beta1?, beta2?, epsilon?)
參數:
- learningRate:用於 Adam 梯度下降算法的學習率。它是可選的。
- beta1:一階矩估計的 index 衰減率。它是可選的。
- beta2:二階矩估計的 index 衰減率。它是可選的。
- epsilon:數值穩定性的小常數。它是可選的。
返回值:亞當優化器。
範例1:二次函數定義為將 x, y 輸入張量和 a, b, c 作為隨機係數。然後我們計算預測的均方損失並將其傳遞給 adam 優化器以最小化損失並理想地改變係數。
Javascript
// A cubic function with its coeffiecient l,m,n.
const x = tf.tensor1d([0, 1, 2, 3]);
const y = tf.tensor1d([1., 2., 5., 11.]);
const l = tf.scalar(Math.random()).variable();
const m = tf.scalar(Math.random()).variable();
const n = tf.scalar(Math.random()).variable();
// y = l * x^3 - m * x + n.
const f = x => l.mul(x.pow(3)).sub(m.mul(x)).add(n);
const loss = (pred, label) => pred.sub(label).square().mean();
const learningRate = 0.01;
const optimizer = tf.train.adam(learningRate);
// Training the model and printing the coefficients.
for (let i = 0; i < 10; i++) {
optimizer.minimize(() => loss(f(x), y));
console.log(
`l:${l.dataSync()}, m:${m.dataSync()}, n:${n.dataSync()}`);
}
// Predictions are made.
const preds = f(x).dataSync();
preds.forEach((pred, i) => {
console.log(`x:${i}, pred:${pred}`);
});
輸出:
l:0.5212615132331848, m:0.4882013201713562, n:0.9879841804504395 l:0.5113212466239929, m:0.49809587001800537, n:0.9783468246459961 l:0.5014950633049011, m:0.5077731013298035, n:0.969675600528717 l:0.49185076355934143, m:0.5170749425888062, n:0.9630305171012878 l:0.48247095942497253, m:0.5257879495620728, n:0.9595866799354553 l:0.47345229983329773, m:0.5336435437202454, n:0.9596782922744751 l:0.4649032950401306, m:0.5403363704681396, n:0.9626657962799072 l:0.4569399356842041, m:0.5455683469772339, n:0.9677067995071411 l:0.4496782124042511, m:0.5491118431091309, n:0.9741682410240173 l:0.44322386384010315, m:0.5508641004562378, n:0.9816395044326782 x:0, pred:0.9816395044326782 x:1, pred:0.8739992380142212 x:2, pred:3.4257020950317383 x:3, pred:11.29609203338623
範例2:下麵是我們設計一個簡單模型的代碼,我們通過 tf.train.adam 定義了一個優化器,學習率參數為 0.01,並將其傳遞給模型編譯。
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// defining the model
const model = tf.sequential({
layers:[tf.layers.dense({ units:1, inputShape:[12] })],
});
// in the compilation we use tf.train.adam optimizer
const optimizer = tf.train.adam(0.001);
model.compile({ optimizer:optimizer, loss:"meanSquaredError" },
(metrics = ["accuracy"]));
// evaluate the model which was compiled above
const result = model.evaluate(tf.ones([10, 12]), tf.ones([10, 1]), {
batchSize:4,
});
// print the result
result.print();
輸出:
Tensor 1.520470142364502
參考:https://js.tensorflow.org/api/3.6.0/#train.adam
相關用法
- PHP imagecreatetruecolor()用法及代碼示例
- p5.js year()用法及代碼示例
- d3.js d3.utcTuesdays()用法及代碼示例
- PHP ImagickDraw getTextAlignment()用法及代碼示例
- PHP Ds\Sequence last()用法及代碼示例
- PHP Imagick floodFillPaintImage()用法及代碼示例
- PHP geoip_continent_code_by_name()用法及代碼示例
- d3.js d3.map.set()用法及代碼示例
- PHP GmagickPixel setcolor()用法及代碼示例
- Tensorflow.js tf.layers.embedding()用法及代碼示例
- PHP opendir()用法及代碼示例
- PHP cal_to_jd()用法及代碼示例
- d3.js d3.bisectLeft()用法及代碼示例
- PHP stream_get_transports()用法及代碼示例
注:本文由純淨天空篩選整理自barnadipdey2510大神的英文原創作品 Tensorflow.js tf.train.adam() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。