avatarAnuj Arora

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1079

Abstract

le language_">self</span>, epoch, logs={}</span>): : do_somthing_here : logs[<span class="hljs-string">"metric_name"</span>] = metric_value</pre></div><p id="071e">Once custom callback is defined, the related custom metric can be included in the other callbacks</p><div id="5b4e"><pre>ckpt_callback = ModelCheckpoint( path_to_file, <span class="hljs-attribute">monitor</span>=<span class="hljs-string">"metric_name"</span>, <span class="hljs-attribute">mode</span>=<span class="hljs-string">"Specify depending on desired behaviour of the metric"</span>,
<span class="hljs-attribute">save_freq</span>=<span class="hljs-string">"as above"</span>, )</pre></div><p id="0418">This behaviour is not limited to exchanging data from custom callbacks to the in-built callbacks, but can be extended across all callbacks, since the 'log' object is passed automatically to all of them through <i>model.fit()</i>. Similar workflow will work with other default callbacks like EarlyStopping, etc. where the functionality is dependent on a cu

Options

stom defined value/metric.</p><p id="d158"><b><i>Attention</i></b><i>: Make sure that the callbacks are lined up in model.fit() in the order of information update. In the above example, if ModelCheckpoint callback is listed before the custom callback, it will work asynchronously with respect to the custom metric, as it <b>will be looking at the metric value from the previous step.</b></i></p><p id="bd49">Hence, here the desired order will be as follows:</p><div id="d317"><pre>model.fit( <span class="hljs-built_in"> train_generator,</span> <span class="hljs-title"> :</span> , <span class="hljs-built_in"> epochs,</span> callbacks = [CustomCallback, ckpt_callback] )</pre></div><p id="1ea9">With that, you are now ready to control the training of your Machine learning model more flexibly.</p><p id="a740">If you find stories like these valuable and would like to support me as a writer, please consider <b>following me</b> or <a href="https://anujarora04.medium.com/membership"><b>signing up</b></a> for Medium membership.</p></article></body>

Using custom metrics for callbacks in Keras model training

Keras provides several in-built metrics which can be directly used for evaluating the model performance. However, it is not uncommon to include custom callbacks, to extend beyond keras' capabilities, as I myself had to do recently. In this post, I discuss how to use custom calculated values (metrics, losses) with in-built callbacks.

Before proceeding, we will need to define our own custom callback to carry out calculations that we want for tracking the training progress or evaluate the model performance. Below is a dummy code for defining a custom callback.

CustomCallback(keras.callbacks.Callback):
   def on_epoch_end(self, epoch, logs={}):
            :
      do_somthing_here
            :
      logs["metric_name"] = metric_value

Once custom callback is defined, the related custom metric can be included in the other callbacks

ckpt_callback = ModelCheckpoint(
    path_to_file,
    monitor="metric_name",
    mode="Specify depending on desired behaviour of the metric",  
    save_freq="as above",
)

This behaviour is not limited to exchanging data from custom callbacks to the in-built callbacks, but can be extended across all callbacks, since the 'log' object is passed automatically to all of them through model.fit(). Similar workflow will work with other default callbacks like EarlyStopping, etc. where the functionality is dependent on a custom defined value/metric.

Attention: Make sure that the callbacks are lined up in model.fit() in the order of information update. In the above example, if ModelCheckpoint callback is listed before the custom callback, it will work asynchronously with respect to the custom metric, as it will be looking at the metric value from the previous step.

Hence, here the desired order will be as follows:

model.fit(
    train_generator,
         :         ,
    epochs,
    callbacks = [CustomCallback, ckpt_callback]
     )

With that, you are now ready to control the training of your Machine learning model more flexibly.

If you find stories like these valuable and would like to support me as a writer, please consider following me or signing up for Medium membership.

Keras
TensorFlow
Callback
Custom Metrics
Machine Learning
Recommended from ReadMedium