Tensorflow.js tf.train.adamax() Function
Last Updated :
20 Jul, 2022
Tensorflow.js is an open-source library developed by Google for running machine learning models and deep learning neural networks in the browser or node environment.
The tf.train.adamax() function us used to create a tf.AdamaxOptimizer that uses adamax algorithm.
Syntax:
tf.train.adamax(learningRate, beta1, beta2, epsilon, decay)
Parameters:
- learningRate: It specifies the learning rate which will be used by adamax gradient descent algorithm.
- beta1: It specifies the estimated exponential decay rate for the 1st moment.
- beta2: It specifies the estimated exponential decay rate for the 2nd moment.
- epsilon: It specifies a small constant for numerical stability.
- decay: It specifies the decay rate for each update.
Return value: It returns a tf.adamaxOptimizer.
Example 1: Fit a function f = (a*x + y) using adamax optimiser by learning the coefficients a and b.
Javascript
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 f = x => a.mul(x).add(b);
const loss = (pred, label) => pred.sub(label).square().mean();
const learningRate = 0.01;
const optimizer = tf.train.adamax(learningRate);
for (let i = 0; i < 10; i++) {
optimizer.minimize(() => loss(f(xs), ys));
}
console.log(
`a: ${a.dataSync()}, b: ${b.dataSync()}}`);
const preds = f(xs).dataSync();
preds.forEach((pred, i) => {
console.log(`x: ${i}, pred: ${pred}`);
});
|
Output:
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
Example 2: Fit a quadratic equation using adamax optimizer, by learning coefficients a, b and c. The configurations of our optimiser are as follows:
- learningRate = 0.01;
- beta1 = 0.1;
- beta2 = 0.1;
- epsilon = 0.3;
- decay = 0.5;
Javascript
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 f = x => a.mul(x).add(b);
const loss = (pred, label) => pred.sub(label).square().mean();
const learningRate = 0.01;
const beta1 = 0.1;
const beta2 = 0.1;
const epsilon = 0.3;
const decay = 0.5;
const optimizer = tf.train.adamax(
learningRate, beta1, beta2, epsilon, decay);
for (let i = 0; i < 10; i++) {
optimizer.minimize(() => loss(f(xs), ys));
}
console.log(
`a: ${a.dataSync()}, b: ${b.dataSync()}}`);
const preds = f(xs).dataSync();
preds.forEach((pred, i) => {
console.log(`x: ${i}, pred: ${pred}`);
});
|
Output:
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
Reference: https://js.tensorflow.org/api/1.0.0/#train.adamax
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...