Coursera で機械学習に入門成功できたã®ã§å¿ç¨ã«ææ¦ãã¦ã¿ã¾ãããã¡ããã©å£ç¯ã®å¤ããç®ã¨ãããã¨ã§ãéå»ã«è¦ã¦æ°ã«ãã£ãã¢ãã¡ã®ç¹å¾´ãå¦ç¿ãã¦ãæªç¥ã®ã¢ãã¡ããæ°ã«ãããããªã¢ãã¡ã¨æ°ã«ãããªãããªã¢ãã¡ã«åé¡ãããã¼ã«ãä½ã£ã¦ãã½ããã¦ã§ã¢ã«ä»å£è¦ãã¹ãã¢ãã¡ãæ¨è¦ãã¦ãããããã¨æãã¾ãã
ã¢ãã¡ã®ç¹å¾´é
ããã¢ãã¡ãæ°ã«ãããã©ããã¯ã話ã®ãããããããçµµæã®æãããã£ã©ã¯ã¿ã¼ã®é åãªã©ã«ãã£ã¦æ±ºã¾ãããã§ãããã ã話ã®ãããããããçµµã®ç¾ããããã£ã©ã¯ã¿ã¼ã®é åãç¹å¾´éã¨ãã¦æ°å¤åããã®ã¯ãããããã®ã§ãã¢ãã¡ã®æ åãå¶ä½ãã¦ããã¹ã¿ãããä¼ç¤¾ã声ãå½ã¦ã¦ãããã£ã¹ãã«ãã©ã¼ã«ã¹ãããã¨ã«ãã¾ããã
Courseraã®æ©æ¢°å¦ç¿ã®ã³ã¼ã¹ã§ã¯ãç¹å¾´ã¨ãã¦å¦¥å½ãã©ãããå¤æããã®ã«ã人éãåãç¹å¾´ãä¸ãããã¦åé¡ã¨ãã£ãã¿ã¹ã¯ãå¯è½ããèãã¦ã¿ãã¨ã¢ããã¤ã¹ãã¦ãã¾ãããã¢ãã¡ä½åã®å¶ä½ä¼ç¤¾ãç£ç£ãèæ¬ãåºæ¼å£°åªã®ååãè¦ãã°ãªãã¨ãªãèªåã«åããããªã¢ãã¡ãã¯ãããæ°ãããã®ã§ãç¹å¾´ã¨ãã¦ã¯è¯ãããã§ãã
ç¹å¾´ãã¯ãã«ã®è¡¨ç¾
åæåãã¢ãã¡ã®å¶ä½ä¼ç¤¾ãã¹ã¿ããããã£ã¹ããªã©ã«å¯¾å¿ãããããªããã¯ãã«ãèãã¾ããããã¢ãã¡ã®ç¹å¾´ãã¯ãã«ã¯ããã®ãã¯ãã«ã«ãã£ã¦è¡¨ç¾ãã¾ãããã¯ãã«ã®åæåã®å¤ã¯ãã®æåã«å¯¾å¿ãããã£ã¹ããããããã®ã¢ãã¡ã«é¢ä¿ãã¦ããã°1ãã¦ããªãã°0ã¨ãã¾ããä¾ãã°ä»¥ä¸ã®ããã«ãªãã¾ã
ã¿ã¤ãã«å±æ§ | ãµã³ã©ã¤ãº | é³¥æµ·æµ©è¼ | æ«»äºåå® | ä¸ææ ä¸ | æ²¢åã¿ãã | é è¤ç¶¾ | éå 寿å | ... |
---|---|---|---|---|---|---|---|---|
éè¡ã®ãªã«ãã§ã³ãº | 1 | 1 | 1 | 1 | 0 | 0 | 1 | ... |
ã¯ã³ãã³ãã³ | 0 | 1 | 1 | 1 | 1 | 0 | 0 | ... |
ããæ¾ãã | 0 | 0 | 1 | 1 | 0 | 1 | 0 | ... |
ããã§ã¯ãã¯ãã«ã®å¤§ããã¯7ãããã§ãããå®éã«ã¯ãã£ã¨æ²¢å±±ã®ã¢ãã¡é¢ä¿è ããã¯ãã«ã«å¯¾å¿ã¥ãã¾ããä»åã¯å¦ç¿ã«ä½¿ã£ãã¢ãã¡ã¨æ¨è¦ãã対象ã«ãªãä»æã®ã¢ãã¡ã®é¢ä¿è ããåºç¾é »åº¦ã®ãããã»ããã1000ãããé¸ã³ã¾ããã
ããã¼ãã«ã¬ã³ãã¼ã®å©ç¨
ã¢ãã¡ã«é¢ä¿ãã¦ããå¶ä½ä¼ç¤¾ãã¹ã¿ããããã£ã¹ããèªåã§èª¿æ»ããã®ã¯é常ã«å¤§å¤ãªã®ã§ããã¤ããä¸è©±ã«ãªã£ã¦ããしょぼいカレンダーãå©ç¨ããã¦ããã ãã¾ããã
ããã¼ãã«ã¬ã³ãã¼ã«ã¯ã¢ãã¡ä½åãã¨ã®ãã¼ã¸ããã£ã¦(ä¾: http://cal.syoboi.jp/tid/3424)ãã¹ã¿ããããã£ã¹ãã®æ å ±ãããã«ã¾ã¨ããã¦ãã¾ãã公開されているAPI ãå©ç¨ããã°ããã®æ å ±ãåå¾ã§ããã®ã§å©ç¨ãã¾ãã
ã¾ããããã¼ãã«ã¬ã³ãã¼ã¯åã¢ãã¡ã«IDãæ¯ã£ã¦ããã¦ããã®ã§ããã®IDãããã°ã©ã ä¸ã§ç¹å®ã®ã¢ãã¡ãæå®ããããã®èå¥åã¨ãã¦å©ç¨ããã¦ãããã¾ããã
éå»ã«è¦ãã¢ãã¡ã®è©ä¾¡
èªåã®å¥½ã¿ãå¦ç¿ãã¦ä½¿ãããã®ã§ãéå»ã«è¦ãã¢ãã¡ã«å¯¾ãã¦ã©ãã«ä»ããã¦ããã¾ããã©ãã«ã¯ãã®ã¢ãã¡ãèªåãç¹ã«æ°ã«ãã£ã¦ããã°1ããããã§ããªããã°0ãã¤ãã¾ãã
ã©ãã«ã¥ãã®ããã«ã2013å¹´ãã2015å¹´ã«æ¾éãããã¢ãã¡ãé çªã«è¡¨ç¤ºãã¦è©ä¾¡ãå ¥åã§ãããç°¡åãªã¹ã¯ãªãããæ¸ãã¾ããããã®ã¹ã¯ãªããã使ã£ã¦ãã¢ãã¡ã«ã©ãã©ãè©ä¾¡ããããã¦ãã£ã¦ãã®ã以ä¸ã®ç»åã§ããyã¨å ¥åããã°ç¹ã«æ°ã«ãã£ã¦ãããã以å¤ã¯ããã§ããªãã¨ããæå³ã§ãããã®èª¿åã§580åãããã®ã¢ãã¡ãè©ä¾¡ãã¦ã©ãã«ãä»ãã¾ããã
ãã¥ã¼ã©ã«ãããã¯ã¼ã¯ã使ã£ãåé¡å¨
ããã¾ã§ã§580åãããã®ã©ãã«ä»ãã®ã¢ãã¡ã®ç¹å¾´ãã¯ãã«ãå¾ããã¾ããã®ã§ããã®ãã¼ã¿ã«åºã¥ãã¦åé¡å¨ãæ§ç¯ãã¾ããæ©æ¢°å¦ç¿ã«ããåé¡å¨ã®ææ³ããããã試ãã¦ã¿ã¾ããããä¸çªãã¾ããã£ããã¥ã¼ã©ã«ãããã¯ã¼ã¯ã使ã£ãåé¡å¨ãç´¹ä»ãã¾ãã
ãã¥ã¼ã©ã«ãããã¯ã¼ã¯ã®åé¡å¨ãèªåã§ä¸å¯§ã«ä½ã£ã¦ãè¯ãã£ãã®ã§ãããã§ããã ããã§ã«ããç©ã使ãã¹ãã¨ãCourseraã®æ©æ¢°å¦ç¿ã§ããã£ã¦ã¾ããããChainerã使ã£ã¦ã¿ã¾ããã
Chainerã§ã¯ãã¥ã¼ã©ã«ãããã¯ã¼ã¯èªä½ã¯ä»¥ä¸ã®ããã«å®ç¾©ãã¾ããããã¯3層ã®ãã¥ã¼ã©ã«ãããã¯ã¼ã¯ã«ãªããããªè¨ç®ã°ã©ããæ§ç¯ãã¦ãã¾ããå ¥åãã¯ãã«ã®é·ãã1000ã¨ãã¦ããã®ã§ãå ¥å層ã¯1000ãã¼ãããã¾ããé ã層ã¯100ãã¼ããã£ã¦ãåºå層ã¯2ãã¼ãã§ããæ´»æ§åé¢æ°ã¨ãã¦ã¯sigmoidé¢æ°ã使ã£ã¦ãã¾ãã
åºåã¯é·ãã2ã®ãã¯ãã«ã«ãªã£ã¦ãã¦ã1çªç®ã®æåã®å¤ã¯ãå ¥åãããç¹å¾´ãã¯ãã«ã0ã«ã©ãã«ä»ããããã¹ã度æ°ãããããã¾ãã2çªç®ã®æåã®å¤ã¯åæ§ã«ãå ¥åãããç¹å¾´ãã¯ãã«ã1ã«ã©ãã«ä»ããããã¹ã度æ°ãããããã¾ãã
from chainer import Chain from chainer.links import Linear from chainer.functions import sigmoid class AnimeChain(Chain): def __init__(self): super(AnimeChain, self).__init__( l1=Linear(1000, 300), l2=Linear(300, 2) ) def __call__(self, x): h1 = sigmoid(self.l1(x)) o = self.l2(h1) return o
ãã®ãã¥ã¼ã©ã«ãããã¯ã¼ã¯ã«ç¹å¾´ãã¯ãã«ã¨æ£è§£ã®ã©ãã«ãå¦ç¿ããããããªé¢æ°ãå®ç¾©ãã¾ãããã®é¢æ°ã§ã¯ããã段éã§ã®ãã¥ã¼ã©ã«ãããã¯ã¼ã¯ã§è¨ç®ããçµæã¨æ¬æ¥å¾ããæ°å¤ã®èª¤å·®ãè¨ç®ãã¦ããã®èª¤å·®ã«åºã¥ãã¦ãã¥ã¼ã©ã«ãããã¯ã¼ã¯ã誤差ãæå°ã«ãªãæ¹åã«æ´æ°ãã¦ããã¾ãããã®æã«èª¤å·®ãè¨ç®ã°ã©ãã®éæ¹åã«ä¼æ¬ããã¦ãã£ããããå¿ è¦ãããã®ã§ãããããã¯ã©ã¤ãã©ãªããã¾ãé è½ãã¦ããã¦ãã¦ãããããã³ã¼ããæ¸ããã«ããã§ãã¾ããç´°ããªã³ã¼ãã®æå³ã¯Chainerã®ãã¥ã¼ããªã¢ã«ãªã©ãåèã«ãã¦ãã ããã
from chainer.optimizers import Adam from chainer import Variable from chainer.functions import softmax_cross_entropy # åã¢ãã¡ã®ç¹å¾´ãã¯ãã«ãæã¤è¡åXã¨ã # åã¢ãã¡ã¸ã®ã©ãã«ä»ãã®çµæãå«ããã¯ãã«tãåãã¨ã£ã¦è¨ç®ãã def train(X, t): model = AnimeChain() # å®ç¾©ãããã¥ã¼ã©ã«ãããã¯ã¼ã¯ optimizer = Adam() # Adamã¨ããæ¹å¼ã使ã£ã¦æé©åãã optimizer.setup(model) for e in range(1500): V_X = Variable(X) # è¨ç®ã°ã©ãã«å¤ãä¸ããã¨ãã¯Variableã«ããã V_t = Variable(np.array(t, dtype='int32')) V_y = model(V_X) model.zerograds() loss = softmax_cross_entropy(V_y, V_t) # 誤差ãè¨ç® loss.backward() # éä¼æããã¦Xã®åæåãã¨ã®èª¤å·®ãè¨ç® optimizer.update() # å¾ããã誤差ã«ãã¨ã¥ãã¦ãã©ã¡ã¼ã¿ãæ´æ° if loss.data < 0.001: # lossãååå°ãããªãã¾ã§ç¹°ãè¿ã break return model
ãã®trainé¢æ°ã«å¯¾ãã¦ãç¨æããã¢ãã¡ãã¨ã®ç¹å¾´ãã¯ãã«ã¨ãæã§ããã°ã£ã¦å ¥åããã©ãã«ãæ師ãã¼ã¿ã¨ãã¦ä¸ãããã¨ã§ããã®å 容ãå¦ç¿ããåé¡å¨ãã§ããããã¾ãã
æ師ãã¼ã¿ãããããã¨ãã«ã¯1ã¨ã©ãã«ä»ãããããã¼ã¿ã¨0ã¨ãã©ãã«ä»ãããããã¼ã¿ã®æ°ãåãã«ãªãããã«ããªã¼ãã¼ãµã³ããªã³ã°ãã¾ããã0ã¨ã©ãã«ä»ããããã®ã»ããå¤ãã£ãã®ã§ããªã¼ãã¼ãµã³ããªã³ã°ããªãã¨ãã¾ãæ§è½ãã§ã¾ããã§ãã(Få¤ã§0.6ãããã«ãªã£ã¦ã ãã¶ããã¼ã)ã
åé¡å¨ã®æ§è½
ç¨æããã©ãã«ä»ãã®ã¢ãã¡ã®ç¹å¾´ãã¯ãã«ç¾¤ãæ師ãã¼ã¿ã¨ããªãã¼ã·ã§ã³ãã¼ã¿ã«ããããã¨ãæ師ãã¼ã¿ãã¤ãã£ã¦å¦ç¿ããåé¡å¨ã§ããªãã¼ã·ã§ã³ãã¼ã¿ã®ã©ãã«ãäºæ¸¬ã§ãããã©ããã確èªãã¾ãããè©ä¾¡å¤ã¨ãã¦ã¯ãFå¤ã使ãã¾ãã
ãã®åé¡å¨ã®å¦ç¿æ²ç·ã¯ä»¥ä¸ã®ããã«ãªãã¾ããæ師ãã¼ã¿ã®æ°ãå¢ããã°å¢ããã»ã©ãããªãã¼ã·ã§ã³ãã¼ã¿ã«ãããFå¤ã0.9以ä¸ã¨ãªã£ã¦ããããã¾ãå¦ç¿ã§ãã¦ãããã¨ããããã¾ããã ããããµãã£ã¦ããã§ãããæ師ãã¼ã¿ãå¢ããã¨ãããããè¯ããªãããããã¾ããã
追è¨: 2016/04/16 12:54
ãªã¼ãã¼ãµã³ããªã³ã°ããçµæããã®ãã¨ã·ã£ããã«ãã¦ããã®ã§ãæ師ãã¼ã¿ã¨ããªãã¼ã·ã§ã³ãã¼ã¿ã«åããã¼ã¿ãã¯ãã£ã¦ããã§ãããç´°ããããã¹ã¦ã¾ããããããªã«ã¹ã³ã¢ã¯è¯ããªããã..ã0.7ã¨ããããã§ã¯...ãã°ã©ããã¾ã¡ãã£ã¦ããã§ãã
追è¨: 2016/04/16 22:20
è©ä¾¡ã«ã¤ãã¦ã¯å¥ã«ã¨ã³ããªãæ¸ãã¾ããã
å®éã«äºæ¸¬ãã¦ã¿ã
ãã®åé¡å¨ã使ã£ã¦å®éã«åãä»å£è¦ãã¹ãã¢ãã¡ãäºæ¸¬ãã¾ããã
åãæ°ã«ãã£ãã¢ãã¡ãå¦ç¿ããåé¡å¨ã®çµæãªã®ã§ãä»ã®èª°ã®å½¹ã«ãç«ã¡ã¾ããããèªåã¨ãã¦ã¯ãµããµãç´å¾ã¨ããçµæãå¾ããã¾ãããããããã®åæ¾éãã¯ãã£ã¦ãã¾ããããã3å¹´ãããã®ã¢ãã¡ããæ師ãã¼ã¿ã¨ãã¦ãããã¦ããªãã¯ããªã®ã§ããããæ©æ¢°ã«ãã£ã¦äºæ¸¬ããã¦ããã¯ãã§ããããããæé«ã
å®éã®ã³ã¼ã
Python3ã§æ¸ããã³ã¼ããç´ æ´ã«GitHubã«ããã¦ããã¾ããæ°ãããããå®è¡ã®ä»æ¹ãªã©æ¸ãããã¨æãã¾ãã READMEã«å®è¡ã®ä»æ¹ã追å ãã¾ããã
追è¨:
âã®ããã«éãªç´¹ä»ãããã¦ããªãã®ã«ä½¿ã£ã¦ã¿ã¦ãã ãã£ã¦ãæ¹ããã¾ãã! å®è¡ã®ä»æ¹ã®è§£èª¬ããã£ã¦ããããã!
ç¶ã ãããã¦ããã ãã¦ãã¾ã
ä»å£è¦ãã¹ãã¢ãã¡ãæ©æ¢°å¦ç¿ã§æ¨è¦ãã - ã¯ãã¹ããã° â¨b.hatena.ne.jpæå ã§å®è¡ããçµæãã«ã¼ããã£ãã¿ã¼ãããã¨ããããï¼ãè¦ãã°è¯ããã¨ãåãã£ã
2016/04/15 20:15
ã¾ã¨ã
ã¨ããããã§ãCoursera ã§æ©æ¢°å¦ç¿ãå¦ãã ææã¨ãã¦ä»å£è¦ãã¹ãã¢ãã¡æ¨è¦ãããä½ã£ã¦ã¿ã¾ãããã¢ãã¡ã®å¶ä½ã«ããããããã£ã¹ãã¨ã¹ã¿ããã®äººåã«ãã¨ã¥ããç¹å¾´ã«åºã¥ããå¦ç¿ã§ããªããªãã®åé¡ç²¾åº¦ãåºããã¨ããããã¾ããã(追è¨: 2016/04/16 13:35 測å®ããã¹ã£ã¦ãã¦ç²¾åº¦ã§ã¦ãããè¬ã§ã.. => 精度についての記事 ãæ¸ãã¾ããã) ããããã便å©ãªãã¼ã«ãã§ããã¨æãã¾ãã
ããã§ã¯ä¸æ¬éã§ç´¹ä»ãã¾ããããå®éã®ã¨ããã¯ãã¸ã¹ãã£ãã¯å帰ããµãã¼ããã¯ã¿ã¼ãã·ã³ãªã©ããããªææ³ããããã¦ã¿ããããããããã©ã¡ã¼ã¿å¤æ´ãã¦ã¿ãã試è¡é¯èª¤ãéãã¾ãããä¸é±éãããã¯Få¤ã0.6ããããããªãã¦ããããããããã®ã®ããããã«ãªã¼ãã¼ãµã³ããªã³ã°ãã¦ã©ãã«ã1ã¨0ã®æ師ãã¼ã¿ã®å²åã1:1ã«ããã¨ãããã£ã¡ããã¡ã精度ãããã£ãããã¦ãè¯ãå¦ã³ä½é¨ãå¾ããã¾ããããã¾ã¡ãã£ã¦ããã§ãã..ã¤ããã
ã¨ã«ããä½ãä½ã£ã¦ã¿ããã¨ã«ã¯æåããã®ã§ãã³ãã³ãã¤ã¤ããããç解ãã¦ãªãã¨ããã沢山ãããã¨ãããã£ãã®ã§ããªããåå¼·ãã¾ãã