🚪✊Knock Knock: Get notified when your training ends with only two additional lines of code

Overview

Knock Knock

made-with-python Downloads Downloads GitHub stars

A small library to get a notification when your training is complete or when it crashes during the process with two additional lines of code.

When training deep learning models, it is common to use early stopping. Apart from a rough estimate, it is difficult to predict when the training will finish. Thus, it can be interesting to set up automatic notifications for your training. It is also interesting to be notified when your training crashes in the middle of the process for unexpected reasons.

Installation

Install with pip or equivalent.

pip install knockknock

This code has only been tested with Python >= 3.6.

Usage

The library is designed to be used in a seamless way, with minimal code modification: you only need to add a decorator on top your main function call. The return value (if there is one) is also reported in the notification.

There are currently twelve ways to setup notifications:

Platform External Contributors
email -
Slack -
Telegram -
Microsoft Teams @noklam
Text Message @abhishekkrthakur
Discord @watkinsm
Desktop @atakanyenel @eyalmazuz
Matrix @jcklie
Amazon Chime @prabhakar267
DingTalk @wuutiing
RocketChat @radao
WeChat Work @jcyk

Email

The service relies on Yagmail a GMAIL/SMTP client. You'll need a gmail email address to use it (you can setup one here, it's free). I recommend creating a new one (rather than your usual one) since you'll have to modify the account's security settings to allow the Python library to access it by Turning on less secure apps.

Python

", " "], sender_email=" ") def train_your_nicest_model(your_nicest_parameters): import time time.sleep(10000) return {'loss': 0.9} # Optional return value ">
from knockknock import email_sender

@email_sender(recipient_emails=["
       
       
        
        "
       
       , "
       
       
        
        "
       
       ], sender_email="
       
       
        
        "
       
       )
def train_your_nicest_model(your_nicest_parameters):
    import time
    time.sleep(10000)
    return {'loss': 0.9} # Optional return value

Command-line

knockknock email \
    --recipient-emails <[email protected]>,<[email protected]> \
    --sender-email <grandma'[email protected]> \
    sleep 10

If sender_email is not specified, then the first email in recipient_emails will be used as the sender's email.

Note that launching this will asks you for the sender's email password. It will be safely stored in the system keyring service through the keyring Python library.

Slack

Similarly, you can also use Slack to get notifications. You'll have to get your Slack room webhook URL and optionally your user id (if you want to tag yourself or someone else).

Python

" @slack_sender(webhook_url=webhook_url, channel=" ") def train_your_nicest_model(your_nicest_parameters): import time time.sleep(10000) return {'loss': 0.9} # Optional return value ">
from knockknock import slack_sender

webhook_url = "
     
     
      
      "
     
     
@slack_sender(webhook_url=webhook_url, channel="
      
      
       
       "
      
      )
def train_your_nicest_model(your_nicest_parameters):
    import time
    time.sleep(10000)
    return {'loss': 0.9} # Optional return value

You can also specify an optional argument to tag specific people: user_mentions=[ , ] .

Command-line

knockknock slack \
    --webhook-url <webhook_url_to_your_slack_room> \
    --channel <your_favorite_slack_channel> \
    sleep 10

You can also specify an optional argument to tag specific people: --user-mentions , .

Telegram

You can also use Telegram Messenger to get notifications. You'll first have to create your own notification bot by following the three steps provided by Telegram here and save your API access TOKEN.

Telegram bots are shy and can't send the first message so you'll have to do the first step. By sending the first message, you'll be able to get the chat_id required (identification of your messaging room) by visiting https://api.telegram.org/bot /getUpdates and get the int under the key message['chat']['id'].

Python

", chat_id=CHAT_ID) def train_your_nicest_model(your_nicest_parameters): import time time.sleep(10000) return {'loss': 0.9} # Optional return value ">
from knockknock import telegram_sender

CHAT_ID: int = <your_messaging_room_id>
@telegram_sender(token="
    
    
     
     "
    
    , chat_id=CHAT_ID)
def train_your_nicest_model(your_nicest_parameters):
    import time
    time.sleep(10000)
    return {'loss': 0.9} # Optional return value

Command-line

knockknock telegram \
    --token <your_api_token> \
    --chat-id <your_messaging_room_id> \
    sleep 10

Microsoft Teams

Thanks to @noklam, you can also use Microsoft Teams to get notifications. You'll have to get your Team Channel webhook URL.

Python

") def train_your_nicest_model(your_nicest_parameters): import time time.sleep(10) return {'loss': 0.9} # Optional return value ">
from knockknock import teams_sender

@teams_sender(token="
     
     
      
      "
     
     )
def train_your_nicest_model(your_nicest_parameters):
    import time
    time.sleep(10)
    return {'loss': 0.9} # Optional return value

Command-line

knockknock teams \
    --webhook-url <webhook_url_to_your_teams_channel> \
    sleep 10

You can also specify an optional argument to tag specific people: user_mentions=[ , ] .

Text Message (SMS)

Thanks to @abhishekkrthakur, you can use Twilio to send text message notifications. You'll have to setup a Twilio account here, which is paid service with competitive prices: for instance in the US, getting a new number and sending one text message through this service respectively cost $1.00 and $0.0075. You'll need to get (a) a phone number, (b) your account SID and (c) your authentification token. Some detail here.

Python

" AUTH_TOKEN: str = " " @sms_sender(account_sid=ACCOUNT_SID, auth_token=AUTH_TOKEN, recipient_number=" ", sender_number=" ") def train_your_nicest_model(your_nicest_parameters): import time time.sleep(10) return {'loss': 0.9} # Optional return value ">
from knockknock import sms_sender

ACCOUNT_SID: str = "
       
       
        
        "
       
       
AUTH_TOKEN: str = "
       
       
        
        "
       
       
@sms_sender(account_sid=ACCOUNT_SID, auth_token=AUTH_TOKEN, recipient_number="
        
        
         
         "
        
        , sender_number="
        
        
         
         "
        
        )
def train_your_nicest_model(your_nicest_parameters):
    import time
    time.sleep(10)
    return {'loss': 0.9} # Optional return value

Command-line

knockknock sms \
    --account-sid <your_account_sid> \
    --auth-token <your_account_auth_token> \
    --recipient-number <recipient_number> \
    --sender-number <sender_number>
    sleep 10

Discord

Thanks to @watkinsm, you can also use Discord to get notifications. You'll just have to get your Discord channel's webhook URL.

Python

" @discord_sender(webhook_url=webhook_url) def train_your_nicest_model(your_nicest_parameters): import time time.sleep(10000) return {'loss': 0.9} # Optional return value ">
from knockknock import discord_sender

webhook_url = "
    
    
     
     "
    
    
@discord_sender(webhook_url=webhook_url)
def train_your_nicest_model(your_nicest_parameters):
    import time
    time.sleep(10000)
    return {'loss': 0.9} # Optional return value

Command-line

knockknock discord \
    --webhook-url <webhook_url_to_your_discord_channel> \
    sleep 10

Desktop Notification

You can also get notified from a desktop notification. It is currently only available for MacOS and Linux and Windows 10. For Linux it uses the nofity-send command which uses libnotify, In order to use libnotify, you have to install a notification server. Cinnamon, Deepin, Enlightenment, GNOME, GNOME Flashback and KDE Plasma use their own implementations to display notifications. In other desktop environments, the notification server needs to be launched using your WM's/DE's "autostart" option.

Python

from knockknock import desktop_sender

@desktop_sender(title="Knockknock Desktop Notifier")
def train_your_nicest_model(your_nicest_parameters):
    import time
    time.sleep(10000)
    return {"loss": 0.9}

Command Line

knockknock desktop \
    --title 'Knockknock Desktop Notifier' \
    sleep 2

Matrix

Thanks to @jcklie, you can send notifications via Matrix. The homeserver is the server on which your user that will send messages is registered. Do not forget the schema for the URL (http or https). You'll have to get the access token for a bot or your own user. The easiest way to obtain it is to look into Riot looking in the riot settings, Help & About, down the bottom is: Access Token: . You also need to specify a room alias to which messages are sent. To obtain the alias in Riot, create a room you want to use, then open the room settings under Room Addresses and add an alias.

Python

" # e.g. https://matrix.org TOKEN = " " # e.g. WiTyGizlr8ntvBXdFfZLctyY ROOM = "
from knockknock import matrix_sender

HOMESERVER = "
      
      
       
       "
      
       # e.g. https://matrix.org
TOKEN = "
      
      
       
       "
      
                    # e.g. WiTyGizlr8ntvBXdFfZLctyY
ROOM = "
      
                           # e.g. #knockknock:matrix.org

@matrix_sender(homeserver=HOMESERVER, token=TOKEN, room=ROOM)
def train_your_nicest_model(your_nicest_parameters):
    import time
    time.sleep(10000)
    return {'loss': 0.9} # Optional return value

Command-line

knockknock matrix \
    --homeserver <homeserver> \
    --token <token> \
    --room <room> \
    sleep 10

Amazon Chime

Thanks to @prabhakar267, you can also use Amazon Chime to get notifications. You'll have to get your Chime room webhook URL.

Python

") def train_your_nicest_model(your_nicest_parameters): import time time.sleep(10) return {'loss': 0.9} # Optional return value ">
from knockknock import chime_sender

@chime_sender(webhook_url="
     
     
      
      "
     
     )
def train_your_nicest_model(your_nicest_parameters):
    import time
    time.sleep(10)
    return {'loss': 0.9} # Optional return value

Command-line

knockknock chime \
    --webhook-url <webhook_url_to_your_chime_room> \
    sleep 10

You can also specify an optional argument to tag specific people: user_mentions=[ , ] .

DingTalk

DingTalk is now supported thanks to @wuutiing. Given DingTalk chatroom robot's webhook url and secret/keywords(at least one of them are set when creating a chatroom robot), your notifications will be sent to reach any one in that chatroom.

Python

" @dingtalk_sender(webhook_url=webhook_url, secret=" ", keywords=[" "]) def train_your_nicest_model(your_nicest_parameters): import time time.sleep(10000) return {'loss': 0.9} # Optional return value ">
from knockknock import dingtalk_sender

webhook_url = "
      
      
       
       "
      
      
@dingtalk_sender(webhook_url=webhook_url, secret="
       
       
        
        "
       
       , keywords=["
       
       
        
        "
       
       ])
def train_your_nicest_model(your_nicest_parameters):
    import time
    time.sleep(10000)
    return {'loss': 0.9} # Optional return value

Command-line

knockknock dingtalk \
    --webhook-url <webhook_url_to_your_dingtalk_chatroom_robot> \
    --secret <your_robot_secret_if_set> \
    sleep 10

You can also specify an optional argument to at specific people: user_mentions=[" "] .

RocketChat

You can use RocketChat to get notifications. You'll need the following before you can post notifications:

  • a RocketChat server e.g. rocketchat.yourcompany.com
  • a RocketChat user id (you'll be able to view your user id when you create a personal access token in the next step)
  • a RocketChat personal access token (create one as per this guide)
  • a RocketChat channel

Python

", rocketchat_user_id=" ", rocketchat_auth_token=" ", channel=" ") def train_your_nicest_model(your_nicest_parameters): import time time.sleep(10000) return {'loss': 0.9} # Optional return value ">
from knockknock import rocketchat_sender

@rocketchat_sender(
    rocketchat_server_url="
        
        
         
         "
        
        ,
    rocketchat_user_id="
        
        
         
         "
        
        ,
    rocketchat_auth_token="
        
        
         
         "
        
        ,
    channel="
        
        
         
         "
        
        )
def train_your_nicest_model(your_nicest_parameters):
    import time
    time.sleep(10000)
    return {'loss': 0.9} # Optional return value

You can also specify two optional arguments:

  • to tag specific users: user_mentions=[ , ]
  • to use an alias for the notification: alias="My Alias"

Command-line

knockknock rocketchat \
    --rocketchat-server-url <url_to_your_rocketchat_server> \
    --rocketchat-user-id <your_rocketchat_user_id> \
    --rocketchat-auth-token <your_rocketchat_auth_token> \
    --channel <channel_name> \
    sleep 10

WeChat Work

WeChat Work is now supported thanks to @jcyk. Given WeChat Work chatroom robot's webhook url, your notifications will be sent to reach anyone in that chatroom.

Python

" @wechat_sender(webhook_url=webhook_url) def train_your_nicest_model(your_nicest_parameters): import time time.sleep(10000) return {'loss': 0.9} # Optional return value ">
from knockknock import wechat_sender

webhook_url = "
    
    
     
     "
    
    
@wechat_sender(webhook_url=webhook_url)
def train_your_nicest_model(your_nicest_parameters):
    import time
    time.sleep(10000)
    return {'loss': 0.9} # Optional return value

Command-line

knockknock wechat \
    --webhook-url <webhook_url_to_your_wechat_work_chatroom_robot> \
    sleep 10

You can also specify an optional argument to tag specific people: user-mentions=[" "] and/or user-mentions-mobile=[" "] .

Note on distributed training

When using distributed training, a GPU is bound to its process using the local rank variable. Since knockknock works at the process level, if you are using 8 GPUs, you would get 8 notifications at the beginning and 8 notifications at the end... To circumvent that, except for errors, only the master process is allowed to send notifications so that you receive only one notification at the beginning and one notification at the end.

Note: In PyTorch, the launch of torch.distributed.launch sets up a RANK environment variable for each process (see here). This is used to detect the master process, and for now, the only simple way I came up with. Unfortunately, this is not intended to be general for all platforms but I would happily discuss smarter/better ways to handle distributed training in an issue/PR.

Owner
Hugging Face
The AI community building the future.
Hugging Face
A model to predict steering torque fully end-to-end

torque_model The torque model is a spiritual successor to op-smart-torque, which was a project to train a neural network to control a car's steering f

Shane Smiskol 4 Jun 03, 2022
TensorFlow Decision Forests (TF-DF) is a collection of state-of-the-art algorithms for the training, serving and interpretation of Decision Forest models.

TensorFlow Decision Forests (TF-DF) is a collection of state-of-the-art algorithms for the training, serving and interpretation of Decision Forest models. The library is a collection of Keras models

538 Jan 01, 2023
Machine Learning from Scratch

Machine Learning from Scratch Author: Shengxuan Wang From: Oregon State University Content: Building Machine Learning model from Scratch, without usin

ShawnWang 0 Jul 05, 2022
This machine-learning algorithm takes in data from the last 60 days and tries to predict tomorrow's price of any crypto you ask it.

Crypto-Currency-Predictor This machine-learning algorithm takes in data from the last 60 days and tries to predict tomorrow's price of any crypto you

Hazim Arafa 6 Dec 04, 2022
Model factory is a ML training platform to help engineers to build ML models at scale

Model Factory Machine learning today is powering many businesses today, e.g., search engine, e-commerce, news or feed recommendation. Training high qu

16 Sep 23, 2022
Microsoft 5.6k Jan 07, 2023
Empyrial is a Python-based open-source quantitative investment library dedicated to financial institutions and retail investors

By Investors, For Investors. Want to read this in Chinese? Click here Empyrial is a Python-based open-source quantitative investment library dedicated

Santosh 640 Dec 31, 2022
Retrieve annotated intron sequences and classify them as minor (U12-type) or major (U2-type)

(intron I nterrogator and C lassifier) intronIC is a program that can be used to classify intron sequences as minor (U12-type) or major (U2-type), usi

Graham Larue 4 Jul 26, 2022
Pytools is an open source library containing general machine learning and visualisation utilities for reuse

pytools is an open source library containing general machine learning and visualisation utilities for reuse, including: Basic tools for API developmen

BCG Gamma 26 Nov 06, 2022
This is my implementation on the K-nearest neighbors algorithm from scratch using Python

K Nearest Neighbors (KNN) algorithm In this Machine Learning world, there are various algorithms designed for classification problems such as Logistic

sonny1902 1 Jan 08, 2022
Stacked Generalization (Ensemble Learning)

Stacking (stacked generalization) Overview ikki407/stacking - Simple and useful stacking library, written in Python. User can use models of scikit-lea

Ikki Tanaka 192 Dec 23, 2022
Short PhD seminar on Machine Learning Security (Adversarial Machine Learning)

Short PhD seminar on Machine Learning Security (Adversarial Machine Learning)

141 Dec 27, 2022
A concept I came up which ditches the idea of "layers" in a neural network.

Dynet A concept I came up which ditches the idea of "layers" in a neural network. Install Copy Dynet.py to your project. Run the example Install matpl

Anik Patel 4 Dec 05, 2021
XGBoost-Ray is a distributed backend for XGBoost, built on top of distributed computing framework Ray.

XGBoost-Ray is a distributed backend for XGBoost, built on top of distributed computing framework Ray.

92 Dec 14, 2022
Model Validation Toolkit is a collection of tools to assist with validating machine learning models prior to deploying them to production and monitoring them after deployment to production.

Model Validation Toolkit is a collection of tools to assist with validating machine learning models prior to deploying them to production and monitoring them after deployment to production.

FINRA 25 Dec 28, 2022
A GitHub action that suggests type annotations for Python using machine learning.

Typilus: Suggest Python Type Annotations A GitHub action that suggests type annotations for Python using machine learning. This action makes suggestio

40 Sep 18, 2022
Tutorial for Decision Threshold In Machine Learning.

Decision-Threshold-ML Tutorial for improve skills: 'Decision Threshold In Machine Learning' (from GeeksforGeeks) by Marcus Mariano For more informatio

0 Jan 20, 2022
Tutorials, examples, collections, and everything else that falls into the categories: pattern classification, machine learning, and data mining

**Tutorials, examples, collections, and everything else that falls into the categories: pattern classification, machine learning, and data mining.** S

Sebastian Raschka 4k Dec 30, 2022
Distributed deep learning on Hadoop and Spark clusters.

Note: we're lovingly marking this project as Archived since we're no longer supporting it. You are welcome to read the code and fork your own version

Yahoo 1.3k Dec 28, 2022
Module for statistical learning, with a particular emphasis on time-dependent modelling

Operating system Build Status Linux/Mac Windows tick tick is a Python 3 module for statistical learning, with a particular emphasis on time-dependent

X - Data Science Initiative 410 Dec 14, 2022