VOOZH about

URL: https://www.geeksforgeeks.org/javascript/tensorflow-js-tf-callbacks-earlystopping-function/

⇱ Tensorflow.js tf.callbacks.earlyStopping() Function - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Tensorflow.js tf.callbacks.earlyStopping() Function

Last Updated : 23 Jul, 2025

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:

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:

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

Comment
Article Tags: