DEV Community

Lucas Frota
Lucas Frota

Posted on

Keeping the eye on the ML model training with CodeMonitor

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 keep an eye on your model even when you are away from your computer 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 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.

GitHub logo Lucasfrota / CodeMonitor

Code Monitor is a simple way to send feedback about your code to your smartphone as a Telegram message!

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 I am 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 import this library you can download the source code directly from GitHub or get it with pip, at this tutorial I am 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
Enter fullscreen mode Exit fullscreen mode

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.

Search for CodeMonitor bot

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 fist 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
Enter fullscreen mode Exit fullscreen mode

Please note that on the sample code, we will use "123456789" as chat id, however, you'll need to replace it on your code


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")
Enter fullscreen mode Exit fullscreen mode

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!")
Enter fullscreen mode Exit fullscreen mode


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., Y_train,          
          epochs = 10,
          validation_data = (X_test, Y_test), 
Enter fullscreen mode Exit fullscreen mode

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., Y_train,          
          epochs = 10,
          validation_data = (X_test, Y_test), 
          callbacks=[FitMonitor("123456789", log_keys=["loss"])])
Enter fullscreen mode Exit fullscreen mode


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 please drop it at comments and thanks for reading.

Top comments (0)