
We’ve all been there. Whether you are experimenting with a new fun model or grinding for that Kaggle competition prize pool, it can be hard to leave your models running in peace. The problem with overparenting your projects is is that while your model is training it’s the perfect time for you to go out and about and take a break from the computer, whether that be grocery shopping, exercising, or whatever you enjoy doing when you aren’t being a data scientist.
On a recent project, I had this exact problem. I wanted to experiment with new parameters and see how they performed after a few epochs, the problem was I didn’t want to spend all day in my room watching a model train, adjusting parameters, and repeating the process, so I devised a solution I thought worth sharing. Rather than spend all day waiting around for portions of the model to finish I would instead have the script send notifications to my phone with updates on its progress. If the model began to underperform I could finish what I was doing, return home, halt the process, change the parameters, and start again. This meant that while I was out I could still keep an eye on the project, which for me gave some much-needed peace of mind.
So how do you set it up?
First, you want to set up a Twilio account. Twilio provides the backend infrastructure for a messaging API you can implement in python. Normally it costs money to use Twilio’s API, however, in our case we can utilize the software for free because we are only texting ourselves.
Now that you have an account let’s understand the basic functionality we will need for using the API. After creating your account and verifying your phone number, Twilio will provide you with an "account_sid", "auth_token", and a test phone number. With each of the provided parameters you can experiment with the API:
!pip install twilio
from twilio.rest import Client
account_sid = "Your acc ID"
auth_token = 'Your Token'
client = Client(account_sid, auth_token)
message = client.messages .create(
body = "Testing 1, 2, 3", #Message you send
from_ = "+1**********+,#Provided phone number
to = "+1**********")#Your phone number
message.sid
This example will send a message to your phone from the test phone number with the text "Testing 1, 2, 3 ". Unfortunately, unless you choose to upgrade to a premium account you can only send texts to the phone number you registered the account with, however, for our goals the free account is sufficient.
Implementing the API Into Your Model Training Process
Now that we understand how the API works let’s get a little creative and have it text us with the average loss and the mean absolute error of our model at the end of each training epoch. To do this for Keras we have to define a custom callback and place the texting API within the callback:

Finally, we have to make sure the model refers to our custom callback. We can do this by adding callbacks=[LossAndErrorPrintingCallback()] to our model.fit() call:
history = model.fit(X, y, batch_size=batch_size, epochs=epochs, verbose=0, validation_split=0.2, callbacks=[LossAndErrorPrintingCallback()])
That’s it!
Start training the model and if everything is implemented and running correctly you can expect to see a text on your phone after every epoch that looks just like this:

Your callbacks don’t have to just occur at the end of each epoch. Check out the official TensorFlow docs for more examples of custom callbacks.
Twilio’s SMS API isn’t the only one on the market and doesn’t just need to be used for this example, it can be implemented into your projects in a variety of useful ways. Feel free to comment below with any creative examples you have come up with for applying a texting notification system to your projects.
Further Reading:
- An example of a project where this would have been very useful
- Build your first Keras Model
- Twilio API Docs
- Custom Keras Callbacks
All images used are either created by myself or used with the explicit permission of the authors. Links to the author’s material are included under each image.