ãµãã¼ããã¯ãã«ãã·ã³ (SVM) ãç¨ãã¦ãMNIST ææ¸ãæ°åãã¼ã¿ã®åé¡ã試ãã¦ã¿ã¾ãã
SVM ã®å®è£
ã¯åºã使ããã¦ãããã®ãããã¤ãããã¾ãã*1ãä»å㯠LIBSVM ãå©ç¨ãã¾ããLIBSVM ã¯ä»¥ä¸ã®ã¦ã§ããµã¤ãããå
¥æã§ãã¾ãã
LIBSVM -- A Library for Support Vector Machines
ãã¼ã¿ã®æºå
ã¾ãããã¦ã³ãã¼ãããå§ç¸®ãã¡ã¤ã«ãå±éãã¦ãã«ããã¾ããæ£å¸¸ã«ãã«ããçµäºããã¨ãsvm-scale, svm-train, svm-predict ã¨ãã 3 ã¤ã®å®è¡ãã¡ã¤ã«ãçæããã¾ãã
$ tar zxf libsvm-3.18.tar.gz $ cd libsvm-3.18 $ make
次ã«ãMNIST ææ¸ãæ°åãã¼ã¿ã LIBSVM ã®å ¥åãã¼ã¿å½¢å¼ã«å¤æãã¾ããããã¹ãå½¢å¼ã«å¤æããã¨ããã¾ã§ã¯ã前回の記事 ãåç §ãã¦ãã ãããååä½æããããã¹ãå½¢å¼ã®ãã¼ã¿ã¯ããã¯ã»ã«ãã¼ã¿ã¨ã©ãã«ãã¼ã¿ã®ããããã«ã¤ã㦠1 ç»åã 1 è¡ã¨ãã¦æ°åãåæãããã®ã§ããããããã LIBSVM ã®å ¥åãã¼ã¿ãã¡ã¤ã«ãä½æããã«ã¯ã以ä¸ã®ããã«ãã¾ãã
$ cat train-images.txt |\ awk '{ for (i = 1; i <= NF; i++) if ($i > 0) printf("%d:%f%s", i, $i / 255, i < NF ? " " : ""); printf("\n"); }' |\ paste -d' ' train-labels.txt - >train.svm
åºåããã train.svm ãã¡ã¤ã«ã®å é è¡ã¯ä»¥ä¸ã®ããã«ãªãã¾ã*2ã
$ head -n 1 train.svm 5 153:0.011765 154:0.070588 155:0.070588 ... 682:0.529412 683:0.517647 684:0.062745
LIBSVM ã®å ¥åãã¼ã¿å½¢å¼ã«ã¤ãã¦ã¯ãREADME ãã¡ã¤ã«ã«èª¬æãããã¾ãã詳細ã¯ãã¡ããåç §ãã¦ãã ããã大ã¾ãã«èª¬æããã¨ã1 ãã¼ã¿ã 1 è¡ãå é åã¯ã©ãã«ã§ã2 åç®ä»¥éã«ãã¼ã¿ã®å次å ã®å¤ã "<index>:<value>" ã®å½¢ã§åæãã¾ããå¤ã 0 ã®æ¬¡å ã¯çç¥ã§ãã¾ããã¾ããLIBSVM ã§ã¯ããã¼ã¿ãé©å®ã¹ã±ã¼ãªã³ã°ãã¦åãæ±ããã¨ãæ¨å¥¨ããã¦ããã®ã§ãåãã¯ã»ã«ã®å¤ã 255 ã§å²ã£ã¦ 0 ãã 1 ã®ç¯å²ã®å¤ã«å¤æãã¦ãã¾ã*3ã
ä½æãã train.svm ã®å¦ç¿ã«ã¯æéããããã®ã§ããããããµã³ããªã³ã°ãã¦å°ããªè¨ç·´ãã¼ã¿ã»ãããä½æãã¾ããè¨ç·´ãã¼ã¿ã®ãµã³ããªã³ã°ã«ã¯ãLIBSVM ã«å«ã¾ãã¦ãã subset.py ã¹ã¯ãªãããå©ç¨ã§ãã¾ãã以ä¸ã®å®è¡ä¾ã§ã¯ãtrain.svm ãã 1,000 ãã¼ã¿ããµã³ããªã³ã°ã㦠train_subset.svm ã«ä¿åãã¦ãã¾ããsubset.py ã¯ã層åæ½åºæ³ãç¨ãã¦ãå ãã¼ã¿ã«ãããåã©ãã«ã®æ¯çãä¿åããããã«ãµã³ããªã³ã°ãã¦ããã¾ãã
$ libsvm-3.18/tools/subset.py train.svm 1000 >train_subset.svm
ææ¸ãæ°åãã¼ã¿ã®åé¡
train_subset.svm ã«å¯¾ãã¦ã以ä¸ã®ããã« SVM ã®å¦ç¿ãå®è¡ãã¾ãããã©ã¡ã¼ã¿ã®è¨å®ãªã©ãããããæ¤è¨ã§ããã®ã§ãããããã§ã¯ããã©ã«ãã®è¨å®ã§å®è¡ãã¦ã¿ã¾ããå®è¡çµæ㯠train_subset.model ãã¡ã¤ã«ã«åºåããã¾ãã
$ libsvm-3.18/svm-train train_subset.svm train_subset.model * optimization finished, #iter = 57 nu = 0.432358 obj = -57.291330, rho = -0.347079 nSV = 93, nBSV = 86 ... optimization finished, #iter = 88 nu = 0.695029 obj = -90.474083, rho = 0.346886 nSV = 140, nBSV = 125 Total nSV = 905
å¦ç¿ãããã¢ãã«ã使ã£ã¦ããã¹ããã¼ã¿ã®åé¡ãè¡ãã¾ããåé¡ã®å¯¾è±¡ã¨ãã t10k.svm ã¯ãtrain.svm ã¨åæ§ã«ä½æãã¦ããã¾ãããªããtrain.svm ã¨åãæé 㧠t10k.svm ãä½æãã㨠t10k.svm ã«ãå é åã«æ£è§£ã®ã©ãã«ãä»ãã¾ãããããã¯æ£è§£çã®è¨ç®ã®ããã«ä½¿ãããã ãã§ããã¡ããåé¡ã®éã«ã¯å©ç¨ããã¾ããã
$ libsvm-3.18/svm-predict t10k.svm train_subset.model t10k.output Accuracy = 85.62% (8562/10000) (classification)
å®è¡ã®çµæãt10k ã«å«ã¾ãã 10,000 ç¹ã®ç»åãåé¡ããçµæãæ£è§£ç㯠85.62% ã ã£ããã¨ãåããã¾ããåç»åã®åé¡çµæ㯠t10k.output ãã¡ã¤ã«ã«åºåããã¦ãã¾ãã®ã§ã以ä¸ã®ããã« confusion matrix ãåºåãã¦ãããããæ£è§£çãè¨ç®ãããã¨ãã§ãã¾ãã以ä¸ã®å®è¡ä¾ã§ã¯ã2 åç®ãæ£è§£ã©ãã«ã3 åç®ãåé¡çµæã1 åç®ããã®ãããªãã¼ã¿ã®åæ°ã«ãªãã¾ãã
$ cut -f1 -d' ' t10k.svm | paste -d' ' - t10k.output | sort | uniq -c | head 896 0 0 5 0 2 3 0 3 7 0 4 38 0 5 24 0 6 2 0 7 5 0 8 1104 1 1 2 1 2
ãã©ã¡ã¼ã¿ãã¥ã¼ãã³ã°
ãã¦ãããã¾ã§ã¯ããã©ã«ãã®ãã©ã¡ã¼ã¿è¨å®ã§å®è¡ãã¾ãããã次ã«ãå¦ç¿ã®éã®ãã©ã¡ã¼ã¿ãã¥ã¼ãã³ã°ã試ãã¦ã¿ã¾ããåé¡ã«åããã¦ãã©ã¡ã¼ã¿ãè¨å®ãããã¨ã§ãæ£è§£çã®åä¸ãæå¾ ã§ãã¾ãã
LIBSVM ã«å«ã¾ãã¦ãã grid.py ã¨ããã¹ã¯ãªãããç¨ãã¦ããã©ã¡ã¼ã¿ã®ãã¥ã¼ãã³ã°ãè¡ãã¾ãããã®ã¹ã¯ãªããã¯ãsvm-train ã§ã®ä¸»è¦ãªãã©ã¡ã¼ã¿ã§ãã cost 㨠gamma ã«ã¤ãã¦ããããã®å¤ãå¤ããªãã svm-train ã®å¦ç¿ãç¹°ãè¿ãã交差æ¤å®ãè¡ã£ã¦æ£è§£çãæãé«ããã®ãæãã¦ããã¾ãã以ä¸ã®ããã«å®è¡ãã¾ãã
$ libsvm-3.18/tools/grid.py train_subset.svm [local] 5 -7 90.9 (best c=32.0, g=0.0078125, rate=90.9) [local] -1 -7 88.1 (best c=32.0, g=0.0078125, rate=90.9) [local] 5 -1 16.4 (best c=32.0, g=0.0078125, rate=90.9) ... [local] 13 -9 88.9 (best c=8.0, g=0.03125, rate=92.0) [local] 13 -3 61.0 (best c=8.0, g=0.03125, rate=92.0) 8.0 0.03125 92.0
å®è¡ã®çµæãcost = 8.0, gamma = 0.03125 ãææ¡ããã¾ããããã®ã¨ãã®äº¤å·®æ¤å®ã®æ£è§£ç㯠92.0% ã¨ãªã£ã¦ãã¾ããã¾ããcost 㨠gamma ã«ããæ£è§£çã®å¤åãçé«ç·ã°ã©ãã§åºåãã¦ããã¾ã*4ãä»åã®å®è¡ã§ã¯ã以ä¸ã®ãããªæ§åã«ãªãã¾ããã
ã°ã©ããè¦ãã¨ãæ£è§£ç 92.0% ã«ãªã£ã¦ããé åã¯ããªãçããã§ã詳ããæ§åãåããã¾ãããgrid.py ã®å®è¡æã«ãã©ã¡ã¼ã¿ã®æ¢ç´¢ç¯å²ãæå®ãããã¨ã§ãç¹å®ã®ç¯å²ããã詳ãã調ã¹ããã¾ãã次ã®å®è¡ä¾ã§ã¯ãcost 㯠0 ãã 4 ã¾ã§ã 0.5 å»ã¿ãgamma 㯠-4 ãã -8 ã¾ã§ã -0.5 å»ã¿ã§è©¦ãããã«æ示ãã¦ãã¾ãã
$ libsvm-3.18/tools/grid.py -log2c 0,4,0.5 -log2g -4,-8,-0.5 train_subset.svm
å®è¡çµæã¯ä»¥ä¸ã®ããã«ãªããcost = 1.414, gamma = 0.03125 ãããã¨ããææ¡ã«ãªãã¾ããã
ææ¡ããããã©ã¡ã¼ã¿ã§ã¢ãã«å¦ç¿ãåé¡ãå®è¡ãã¦ã¿ã¾ãã92.69% ã¨ããæ£è§£çãå¾ããã¾ããã
$ libsvm-3.18/svm-train -c 1.41421356237 -g 0.03125 train_subset.svm train_subset.model $ libsvm-3.18/svm-predict t10k.svm train_subset.model t10k.out Accuracy = 92.69% (9269/10000) (classification)
ããã¾ã§ãè¨ç·´ãã¼ã¿ã¯ 1,000 ç¹ã¨ãã¦å®è¡ãã¦ãã¾ããããããå¤ãã®ãã¼ã¿ã使ããã¨ã§æ£è§£çãé«ããªãã¾ãããã©ã¡ã¼ã¿ã®å¤ã¯åºå®ããç¶æ
ã§ãå©ç¨ãããã¼ã¿æ°ãå¤ãã¦ã¢ãã«å¦ç¿ãåé¡ãç¹°ãè¿ããã¨ãããæ£è§£çã¯ä»¥ä¸ã®ããã«å¤åãã¾ããã60,000 ãã¼ã¿ãã¹ã¦ã使ã£ã¦å¦ç¿ããã¢ãã«ã§ã¯ã98.49% ã®æ£è§£çãå¾ããã¾ãããMNIST ã®ã¦ã§ããã¼ã¸ã«ã¯ãã¬ã¦ã·ã¢ã³ã«ã¼ãã«ã® SVM ã§èª¤èå¥ç 1.4% ã¨ãããä»åã®å®é¨ã§ã¯ããã«è¿ãçµæãå¾ããããã¨ã«ãªãã¾ãã
*1:ä»åå©ç¨ãã LIBSVM ã®ã»ãã«ãSVM-Light, TinySVM ãªã©ãããã¾ããã¾ããæ©æ¢°å¦ç¿ã©ã¤ãã©ãªã«ã SVM ãå«ããã®ãå¤ãããããã§ãã
*2:é·ãã®ã§ãéä¸ãçç¥ãã¦ãã¾ãã
*3:svm-scale ã使ãã¨ãè¨ç·´ãã¼ã¿ã®ç¯å²ã調ã¹ã¦èªåçã«ãã¼ã¿ãã¹ã±ã¼ãªã³ã°ã§ãã¾ããä»åã®ãã¼ã¿ã§ã¯ãåãã¯ã»ã«ã®å¤ã 0 ãã 255 ã®ç¯å²ã ã¨åãã£ã¦ããã®ã§ãsvm-scale ãå©ç¨ããã«ç´æ¥å¤æãã¾ããã