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
// Importing tensorflowimport * 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 coefficientsconst 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();Â
// Defining learning rate of adamax algorithmconst 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}`);}); |
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
// Importing tensorflowimport * 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 coefficientsconst a = tf.scalar(Math.random()).variable();const b = tf.scalar(Math.random()).variable();Â
// Defining 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 algorithmconst 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}`);}); |
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
Â
