Skip to content

Instantly share code, notes, and snippets.

@WillKoehrsen
Last active December 10, 2018 12:42
Show Gist options
  • Save WillKoehrsen/e13a2717695fbed5c4368f072e095f56 to your computer and use it in GitHub Desktop.
Save WillKoehrsen/e13a2717695fbed5c4368f072e095f56 to your computer and use it in GitHub Desktop.
def report_stats(text, channel):
"""Report training stats"""
r = slack.chat.post_message(channel=channel, text=text,
username='Training Report',
icon_emoji=':clipboard:')
class SlackUpdate(Callback):
"""Custom Keras callback that posts to Slack while training a neural network"""
def __init__(self, channel):
self.channel = channel
# Action at end of epoch
def on_epoch_end(self, batch, logs={}):
# Post current results
message = f'Epoch: {self.n_epochs} Training Loss: {self.train_loss[-1]:.4f} Validation Loss: {self.valid_loss[-1]:.4f}'
report_stats(message, channel=self.channel)
# Action at end of training
def on_train_end(self, logs={}):
# Post best results
message = f'Trained for {self.n_epochs} epochs. Best epoch was {best_epoch + 1}.'
report_stats(message, channel=self.channel)
message = f'Best validation loss = {valid_loss:.4f} Training Loss = {train_loss:.2f} Validation accuracy = {100*valid_acc:.2f}%'
report_stats(message, channel=self.channel)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment