Chainerã®å¦ç¿ã®æ§åããªã¢ã¼ãã§ç¢ºèªããExtensionãä½ã£ã
çããããã«ã¡ã¯
ãå
æ°ã§ãããç§ã¯GWã§ãªãã¬ãã·ã¥ãã¦ãçãè¿ãã¾ããã
Kaggleããã£ã¦ããã¨ãï¼ç¹ã«ç»åãªã©ã®é·ãå ´åï¼ã«ãªã¢ã¼ãã§
ä»å¦ç¿ããã¦ãããã©ããã誤差ã¯ã©ãããªã©ã®ã¢ãã«ã®
æ§åãæ°ã«ãªããã¨ã¯ããã¾ãããï¼
ç§ã¯ç»åèªèç³»ã®ã³ã³ããå®éã«è¡ã£ã¦ããæã«ãæ°ã«ãªããã¨ãããã¾ãã
ããã©ãããããã¨èãã¦ããã®ã§ãããæ©ãã¦ããæã«ãµã¨æãã¤ããã®ã§å®è£
ãã¾ããã
ãã®ã¢ã¤ãã¢ã®å®è£
ã®ããã«ãæ°ããChainerã®Extensionãéçºãã¾ãããï¼Trainerã使ãæ³å®ã§ãï¼
ã¢ã¤ãã¢
Slackã§ããã°å¤åºä¸ãè¦ããã¨èãã¾ããã
ãã®ãããå¦ç¿ã®éä¸çµéï¼lossãªã©ï¼ãæ稿ããã°è¦ããï¼
å®è£
ã¤ã¡ã¼ã¸ã¯æ¬¡ã®å³ã«æ²è¼ãã¾ããã
ã³ã¼ããè¦ãéãã ã¨ãExtensionã§å®è£ ã§ãããã ã£ãã®ã§ããã©ã¤ãã¾ããã
Extensionã®å®è£ æ¹æ³
Extensionã®å®è£
ã§ãããå
ã«å¿
è¦ãªæ
å ±ã__init__ã«å®è£
ãã¾ãã
ããã¦ã__call__ãå¼ã³åºãããå¦çãããä»çµã¿ã¨ãªã£ã¦ãã¾ãã
ä»åã¯æ¢ã«å®è£ ããã¦ããPrintReportãåèã«å®è£ ãã¾ãã
åæºå
Slackã®Web APIã®Incoming Webhooksã¨æ稿ããChannelã¨usernameãå¿
è¦ã§ãã
Slackã®APIã«å¿
è¦ãªæ
å ±ã¯ä»¥ä¸ã®urlããé·ç§»ãã¦åå¾ãã¦ãã ããã
ã³ã¼ã
æ©éExtensionãå®è£
ãã¾ããã
表示æ
å ±ã¯æ¢ã«å®è£
ããã¦ããPrintReportã¨åãã«ãã¾ããã
_throw_slackã«Slackã«æ稿ããé¨åãå®ç¾©ãã¦ãã¾ãã
__init__ã«åæè¨å®ã§å¿
è¦ãªæ
å ±ã__call__ã«è¡¨ç¤ºããé¨åãè¨è¼ãã¾ããã
# coding:utf-8 import os import sys from chainer.training import extension from chainer.training.extensions import log_report as log_report_module import requests import json class SlackReport(extension.Extension): def __init__(self, entries, log_report='LogReport', username="", url="",channel="",out=sys.stdout): self._entries = entries self._log_report = log_report self._log_len = 0 # number of observations already printed self._out = out # format information entry_widths = [max(10, len(s)) for s in entries] header = ' '.join(('{:%d}' % w for w in entry_widths)).format( *entries) + '\n' self._header = header # printed at the first call templates = [] for entry, w in zip(entries, entry_widths): templates.append((entry, '{:<%dg} ' % w, ' ' * (w + 2))) self._templates = templates self.username = username self.url = url self.channel = channel def __call__(self, trainer): if self._header: self._throw_slack(self._header) self._header = None log_report = self._log_report if isinstance(log_report, str): log_report = trainer.get_extension(log_report) elif isinstance(log_report, log_report_module.LogReport): log_report(trainer) # update the log report else: raise TypeError('log report has a wrong type %s' % type(log_report)) log = log_report.log log_len = self._log_len while len(log) > log_len: self._observation_throw_slack(log[log_len]) log_len += 1 self._log_len = log_len def _throw_slack(self, text): try: payload_dic = { "text": text, "username": self.username, "channel": self.channel, } requests.post(self.url, data=json.dumps(payload_dic)) except: self._out.write("error!") def serialize(self, serializer): log_report = self._log_report if isinstance(log_report, log_report_module.LogReport): log_report.serialize(serializer['_log_report']) def _observation_throw_slack(self, observation): text = "" for entry, template, empty in self._templates: if entry in observation: text += template.format(observation[entry]) else: text += empty self._throw_slack(text)
使ãæ¹
APIã«å¿
è¦ãªæ
å ±ã®å®ç¾©ã¨Extensionã«SlackReportã追å ããã®ã¿ã§ãã
ä¾ãã°ã公式のMNISTサンプルã§ã¯æ¬¡ã®Extensionã追è¨ãã¦ãã ããã
username,channel,urlã¯ããããèªåã®è¨å®ãæå®ãã¦ãã ããã
trainer.extend(SlackReport(['epoch', 'main/loss', 'validation/main/loss', 'main/accuracy', 'validation/main/accuracy', 'elapsed_time'], username="YOUR USER NAME", channel="YOUR SLACK CHANNEL", url="SLACK API URL", ))
Slackã¸ã®æ稿çµæ
Exampleã®MNISTã§å®é¨ãããããããªæãã«ãªãã¾ããã¡ãã£ã¨ã¬ã¿ã¬ã¿ãªã®ã¯ãæå¬ã
å¤åºæã«æ°ã«ãªãå ´åã¯çããã使ã£ã¦ã¿ã¾ãããã
è¨å®ãééã£ã¦ããã¨ãã®å¦çã¯â¦ãçµæ§é©å½ã§ãã