Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the callback function callbacks

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Callback function callbacks how to use, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Let's introduce the callback function.

First, an overview of callback functions

The callback function of tf.keras is actually a class, which is usually specified as a parameter in model.fit, which is used to control the beginning or end of each epoch training, and perform some operations at the beginning or end of each batch training, such as collecting some log information, changing the learning rate and other hyperparameters, terminating the training process in advance, and so on.

Similarly, the callbacks parameter can be specified for model.evaluate or model.predict to control some actions to be performed at the beginning or end of each batch at the beginning or end of an evaluation or prediction, but this usage is relatively rare.

Most of the time, the callback function class defined in the keras. callbacks submodule is sufficient, and if there is a specific need, we can also construct a custom callback function by subclassing keras.callbacks.Callbacks.

All callback functions inherit from the keras.callbacks.Callbacks base class and have two attributes, params and model.

Where params is a dict, which records training parameters (eg. Verbosity, batch size, number of epochs...).

Model is a reference to the currently associated model.

In addition, for some methods in the callback class, such as on_epoch_begin,on_batch_end, there will be an input parameter logs, which provides some information about the current epoch or batch, and can record the calculation result. If model.fit specifies more than one callback function class, these logs variables will be passed sequentially between the functions of the same name in these callback function classes.

Second, built-in callback function

BaseLogger: collect the average value of metrics on each epoch on each batch, and directly take the final value for the indicator with intermediate state in the stateful_metrics parameter without averaging each batch. The mean result of the indicator will be added to the logs variable. The callback function is added by default by all models and is the first to be added.

History: record the metrics results of each epoch calculated by BaseLogger into the dict variable history as the return value of model.fit. The callback function is added by default for all models and is added after BaseLogger.

EarlyStopping: when the monitored index is not improved after several epoch, the training will be terminated ahead of time.

TensorBoard: save log information for Tensorboard visualization. Support the visualization of evaluation indicators, calculation charts, model parameters, etc.

ModelCheckpoint: save the model after each epoch.

ReduceLROnPlateau: if the monitoring index is not improved after several epoch, the learning rate will be reduced by a certain factor.

TerminateOnNaN: if you encounter loss as NaN, stop the training ahead of time.

LearningRateScheduler: learning rate controller. Given the functional relationship between the learning rate lr and epoch, the learning rate is adjusted before each epoch according to the functional relationship.

CSVLogger: record the logs results after each epoch in the CSV file.

ProgbarLogger: prints the logs results after each epoch to the standard output stream.

Third, customize the callback function

You can write simpler callback functions using callbacks.LambdaCallback, or you can write more complex callback function logic by subclassing callbacks. Callback.

If you need to learn more about the callback function in tf.Keras, don't hesitate to read the source code of the built-in callback function.

After reading the above, have you mastered how to use the callback function callbacks? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report