Tensorflow.js tf.callbacks.earlyStopping() Function
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.
Tensorflow.js tf.callbacks.earlyStopping() is a callback function used for stopping training when training data stop improving.
Syntax:
tf.callbacks.earlyStopping(args);
Parameters: This method accepts the following parameters.
- args: It is an object with the following fields:
- monitor: It should be a string. It is the value that is to be monitored.
- minDelta: It should be a number. It is the minimum value below which is not considered an improvement in training.
- patience: It should be a number. It is the number of times it should not stop when it encounters a value that is below than minDelta.
- verbose: It should be a number. It is the value of verbosity.
- mode: It should be one of these three:
- "auto": In auto mode, the direction is inferred automatically from the name of the monitored quantity.
- "min": In min mode, training will stop when the value of data that is monitored stop decreasing.
- "max": In max mode, training will stop when the value of data that is monitored stop increasing.
- baseline: It should be a number. It is the number that tells when training doesn't keep up with this value training will stop. It is the end line for the quantity which is monitored.
- restoreBestWeights: It should be a boolean value. It tells whether to restore the best value from the monitored quantity in each epoch or not.
Return Value: It returns an object (EarlyStopping).
Below are some examples of this function.
Example 1: In this example we will see how to use tf.callbacks.earlyStopping() function in fitDataset:
import * as tf from "@tensorflow/tfjs";
const xArray = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[8, 7, 6, 5],
[1, 2, 3, 4],
];
const x1Array = [
[0, 1, 0.5, 0],
[1, 0.5, 0, 1],
[0.5, 1, 1, 0],
[1, 0, 0, 1],
];
const yArray = [1, 2, 3, 4];
const y1Array = [4, 3, 2, 1];
// Create a dataset from the JavaScript array.
const xDataset = tf.data.array(xArray);
const x1Dataset = tf.data.array(x1Array);
const y1Dataset = tf.data.array(x1Array);
const yDataset = tf.data.array(yArray);
// Combining the Dataset with zip function
const xyDataset = tf.data
.zip({ xs: xDataset, ys: yDataset })
.batch(4)
.shuffle(4);
const xy1Dataset = tf.data
.zip({ xs: x1Dataset, ys: y1Dataset })
.batch(4)
.shuffle(4);
// Creating model
const model = tf.sequential();
model.add(
tf.layers.dense({
units: 1,
inputShape: [4],
})
);
// Compiling model
model.compile({ loss: "meanSquaredError",
optimizer: "sgd", metrics: ["acc"] });
// Using tf.callbacks.earlyStopping in fitDataset.
const history = await model.fitDataset(xyDataset, {
epochs: 10,
validationData: xy1Dataset,
callbacks: tf.callbacks.earlyStopping({
monitor: "val_acc" }),
});
// Printing value
console.log("The value of val_acc is :",
history.history.val_acc);
Output: The value you get is different because with training value its val_acc value changes.
The value of val_acc is :0.4375,0.375
Example 2: In this example, we will see how to use tf.callbacks.earlyStopping() with fit:
import * as tf from "@tensorflow/tfjs";
// Creating tensor for training
const x = tf.tensor([5, 6, 7, 8, 9, 2], [3, 2]);
const x1 = tf.tensor([8, 7, 6, 5, 2, 9], [3, 2]);
const y = tf.tensor([1, 3, 3, 4, 4, 6, 6, 8, 9], [3, 3]);
const y1 = tf.tensor([2, 2, 2, 1, 5, 5, 2, 3, 8], [3, 3]);
// Creating model
const model = tf.sequential();
model.add(
tf.layers.dense({
units: 3,
inputShape: [2],
})
);
// Compiling model
model.compile({ loss: "meanSquaredError",
optimizer: "sgd", metrics: ["acc"] });
// Using tf.callbacks.earlyStopping in fit.
const history = await model.fit(x, y, {
epochs: 10,
validationData: [x1, y1],
callbacks: tf.callbacks.earlyStopping({
monitor: "val_acc" }),
});
// Printing value
console.log("the value of val_acc is :",
history.history.val_acc);
Output: The value of your executing code will be different because with training data value changes:
the value of val_acc is : 0.3333333432674408,0.3333333432674408
Reference: https://js.tensorflow.org/api/latest/#callbacks.earlyStopping