Keeping the eye on Keras models with CodeMonitor
Last Updated :
10 May, 2022
If you work with deep learning, you probably have faced a situation where your model takes too long to learn, and you have to keep watching its progress, however staying in front of the desk watching it learn is not exactly the most exciting thing ever, for this reason, this article's purpose is to introduce CodeMonitor, a simple but very useful python library. It helps you to make better use of your time and still keep an eye on your model wherever it is being trained by sending reports on each epoch directly to your phone in the form of messages.
The first thing you need to know about CodeMonitor is that it is an open-source initiative from the person who writes this article thus any suggestions or problem retorts are more than welcome. The code is being hosted on Github and for any report, please use the issues section.
Currently, this library uses only Telegram to send the messages and have two ways to do it, you can make use of a Keras callback that will send a report of the logs generated on the training to your Telegram chat at the end of each epoch, these logs may be the loss or the accuracy, for example, it may vary according to your project. The other way you could use it is just sending any string you like, as a message thus you can send messages of any nature, in any context you come up with.
In this article we are assuming that you already have a Keras model, if it is not your case you can learn more about Keras with this tutorial that addresses the classification of handwriting texts on images and come back later or just read it without fully implementing the code.
Set up
To make use of this library you can download the source code directly from GitHub or get it with pip, at this tutorial we are going to use the second option for sake of simplicity, to do so just copy and paste the following line of code on your terminal (we are assuming that you already have pip installed, to learn more about pip visit their installation page).
pip install CodeMonitor
Once you have it installed you need to set up the Telegram side, which is pretty simple, if you already have the app installed on your phone just open it and touch the search icon at the right upper corner and search for CodeMonitor and in the image below, select it.

At this point you just need to send any message to it, in response, you will receive a code, this is the chat id, we are using it to send messages to this chat, keep this code in secret, because if anyone who has it can send you messages.

Now everything is set up on Telegram side, so we can come back to the computer and send our first message from python! On python side, we have two classes that are responsible for the functionalities we have discussed earlier, lets import those.
from CodeMonitor.telegram import Messenger, FitMonitor
Please note that on the sample code, we will use "123456789" as chat id, however, you'll need to replace it on your code
Messenger
This class allows you to send any string you want to the Telegram chat, only requiring the chat id as mentioned on the Telegram side section.
messenger = Messenger("123456789")
When the class is instantiated it sends the message "All ready!", to send your messages to use the function send_message.
messenger.send_message("your awesome message!")
FitMonitor
As mentioned above this class allows you to send reports based on the logs generated on the training as in the first class you will have to provide the chat id to identify your chat. To simply send all logs generated on the training just add the FitMonitor class to the callbacks list as shown below.
model.fit(X_train, Y_train,
epochs = 10,
validation_data = (X_test, Y_test),
callbacks=[FitMonitor("123456789")])
To specify the logs you want to send you can set a list of the ones you want to include in the message by using the log_keys parameter, as shown in the code snippet which specifies that only the loss should be included in the message.
model.fit(X_train, Y_train,
epochs = 10,
validation_data = (X_test, Y_test),
callbacks=[FitMonitor("123456789", log_keys=["loss"])])
Conclusion
In this article, you have been introduced to a simple but useful tool that may help you to keep monitoring your model from anywhere with your phone, as mentioned above this is an open-source initiative and is still being developed so if you have any idea or suggestion you are more than welcome to drop it at comments.
Similar Reads
How to create Models in Keras? Keras is an open-source API used for solving a variety of modern machine learning and deep learning problems. It enables the user to focus more on the logical aspect of deep learning rather than the brute coding aspects. Keras is an extremely powerful API providing remarkable scalability, flexibilit
4 min read
Deploying a TensorFlow 2.1 CNN model on the web with Flask When starting to learn Machine Learning, one of the biggest issues people face is deploying whatever they make to the web for easier demonstration/use. This article will help those beginners bridge the gap between creating a TensorFlow model and deploying it on the web with Flask and hopefully gain
4 min read
Python - Create UIs for prototyping Machine Learning model with Gradio Gradio is an open-source python library which allows you to quickly create easy to use, customizable UI components for your ML model, any API, or any arbitrary function in just a few lines of code. You can integrate the GUI directly into your Python notebook, or you can share the link to anyone.Requ
4 min read
Manipulating Jupyter Notebooks with the NBFormat Python Library The NBFormat Python library is a powerful tool, that is used to manipulate Jupyter Notebooks programmatically. It provides a set of functions that allow developers to create, read, modify, and validate Jupyter Notebook files (.ipynb) without manually doing it. We can use this library to automate not
6 min read
Save and Load Machine Learning Models in Python with scikit-learn In this article, let's learn how to save and load your machine learning model in Python with scikit-learn in this tutorial. Once we create a machine learning model, our job doesn't end there. We can save the model to use in the future. We can either use the pickle or the joblib library for this purp
4 min read
How to Save the Final Model Using Keras Saving your final model in Keras using the HDF5 format is an effective way to capture all aspects of the model for later use, whether for further training, evaluation, or deployment. Itâs a critical step for preserving your work and making your models reusable and shareable. Always ensure that the e
2 min read