Tensorflow.js是Google開發的開源庫,用於在瀏覽器或節點環境中運行機器學習模型和深度學習神經網絡。
tf.train.adadelta() 函數用於創建使用 adadelta 算法的 tf.AdadeltaOptimizer。 adadelta 算法是梯度下降優化算法的擴展。它用於優化神經網絡。
用法:
tf.train.adadelta(learningRate)
參數:
- learningRate:它指定了 adadelta 梯度下降算法將使用的學習率。
- rho:它指定了每次更新的學習率衰減。
- epsilon:它指定了一個常量 epsilon,用於改善 grad 更新的條件。可選的
返回值:它返回一個 tf.adadeltaOptimizer
範例1:通過學習係數 a 和 b,使用 adadelta 優化器擬合函數 f=(a*x+y)。
Javascript
// Importing tensorflow
import * as tf from "@tensorflow/tfjs"
const xs = tf.tensor1d([0, 1, 2, 3]);
const ys = tf.tensor1d([1.1, 5.9, 16.8, 33.9]);
// Choosing variable coefficients
const a = tf.scalar(Math.random()).variable();
const b = tf.scalar(Math.random()).variable();
// Defining function f = (a*x + b)
const f = x => a.mul(x).add(b);
const loss = (pred, label) => pred.sub(label).square().mean();
const learningRate = 0.01;
// Creating optimizer
const optimizer = tf.train.adadelta(learningRate);
// Train the model.
for (let i = 0; i < 10; i++) {
optimizer.minimize(() => loss(f(xs), ys));
}
// Make predictions.
console.log(
`a:${a.dataSync()}, b:${b.dataSync()}}`);
const preds = f(xs).dataSync();
preds.forEach((pred, i) => {
console.log(`x:${i}, pred:${pred}`);
});
輸出:
a:5.39164924621582, b:1.8858184814453125} x:0, pred:1.8858184814453125 x:1, pred:7.277467727661133 x:2, pred:12.669116973876953 x:3, pred:18.060766220092773
範例2:通過學習係數 a、b 和 c,使用 adadelta 優化器擬合二次方程。優化器配置如下:
- 學習率 = 0.01
- ρ = 0.2
- ε = 0.5
Javascript
// Importing tensorflow
import * as tf from "@tensorflow/tfjs"
const xs = tf.tensor1d([0, 1, 2, 3]);
const ys = tf.tensor1d([1.1, 5.9, 16.8, 33.9]);
const a = tf.scalar(Math.random()).variable();
const b = tf.scalar(Math.random()).variable();
const c = tf.scalar(Math.random()).variable();
const f = x => a.mul(x.square()).add(b.mul(x)).add(c);
const loss = (pred, label) => pred.sub(label).square().mean();
// Setting configurations for our optimizer
const learningRate = 0.01;
const rho = 0.2;
const epsilon = 0.5;
// Creating the optimizer
const optimizer = tf.train.adadelta(learningRate, rho, epsilon);
// Train the model.
for (let i = 0; i < 10; i++) {
optimizer.minimize(() => loss(f(xs), ys));
}
// Make predictions.
console.log(
`a:${a.dataSync()}, b:${b.dataSync()}, c:${c.dataSync()}`);
const preds = f(xs).dataSync();
preds.forEach((pred, i) => {
console.log(`x:${i}, pred:${pred}`);
});
輸出:
a:3.1871466636657715, b:1.5096971988677979, c:0.8317463397979736 x:0, pred:0.8317463397979736 x:1, pred:5.528590202331543 x:2, pred:16.599727630615234 x:3, pred:34.04515838623047
參考:https://js.tensorflow.org/api/1.0.0/#train.adadelta
相關用法
- 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()用法及代碼示例
注:本文由純淨天空篩選整理自abhinavjain194大神的英文原創作品 Tensorflow.js tf.train.adadelta() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。