當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Tensorflow.js tf.train.adamax()用法及代碼示例

Tensorflow.js是Google開發的開源庫,用於在瀏覽器或節點環境中運行機器學習模型和深度學習神經網絡。

tf.train.adamax() 函數用於創建使用 adamax 算法的 tf.AdamaxOptimizer。

用法:

tf.train.adamax(learningRate, beta1, beta2, epsilon, decay)

參數:

  • learningRate:它指定了 adamax 梯度下降算法將使用的學習率。
  • beta1:它指定了第一時刻的估計 index 衰減率。
  • beta2:它指定了第二時刻的估計 index 衰減率。
  • epsilon:它為數值穩定性指定了一個小常數。
  • decay:它指定每次更新的衰減率。

返回值:它返回一個 tf.adamaxOptimizer。



範例1:通過學習係數 a 和 b 使用 adamax 優化器擬合函數 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 random coefficients
const a = tf.scalar(Math.random()).variable();
const b = tf.scalar(Math.random()).variable();
  
// Defning function f = (a*x + b).
const f = x => a.mul(x).add(b);
const loss = (pred, label) => pred.sub(label).square().mean();
  
// Defining learning rate of adamax algorithm
const learningRate = 0.01;
  
// Creating our optimizer.
const optimizer = tf.train.adamax(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:0.4271160364151001, b:0.21284617483615875}
x:0, pred:0.21284617483615875
x:1, pred:0.6399621963500977
x:2, pred:1.0670782327651978
x:3, pred:1.4941942691802979

範例2:通過學習係數 a、b 和 c,使用 adamax 優化器擬合二次方程。我們優化器的配置如下:

  • 學習率 = 0.01;
  • β1 = 0.1;
  • β2 = 0.1;
  • ε = 0.3;
  • 衰減 = 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]);
  
// Choosing random coefficients
const a = tf.scalar(Math.random()).variable();
const b = tf.scalar(Math.random()).variable();
  
// Defning function f = (a*x^2 + b*x + c).
const f = x => a.mul(x).add(b);
const loss = (pred, label) => pred.sub(label).square().mean();
  
// Defining configurations of adamax algorithm
const learningRate = 0.01;
const beta1 = 0.1;
const beta2 = 0.1;
const epsilon = 0.3;
const decay = 0.5;
  
// Creating our optimizer.
const optimizer = tf.train.adamax(
    learningRate, beta1, beta2, epsilon, decay);
  
// 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:0.8346626162528992, b:0.5925931334495544}
x:0, pred:0.21284617483615875
x:1, pred:1.4272557497024536
x:2, pred:2.261918306350708
x:3, pred:3.096580982208252

參考:https://js.tensorflow.org/api/1.0.0/#train.adamax




相關用法


注:本文由純淨天空篩選整理自abhinavjain194大神的英文原創作品 Tensorflow.js tf.train.adamax() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。