ä»äººã®äœã£ãã¯ã©ã¹ïŒæã ã£ããé¢æ°ããµãã«ãŒãã³ïŒã¯ãå 容ã100%è¿ãç解ããŠããªããšäœ¿ããªããšããæã蟌ã¿ããããããæ¬åœã«ãããªã ânumpyâ ã䜿ããªããã ãã©ãã
äžæ¹ã§ãããã°ã©ã ãäžããèªäœãããšãŸãæåãããŸãšãã«åãããããã¯ãªããããä»äººã®äœã£ãããã°ã©ã ã§åããŠãã®ãããã°ã極åæµçšããããšããã¢ã¿ããããã
ã€ãŸãç§ã®äœãããã°ã©ã ã¯ãã©ã£ã¡ã«ãããŸãšãã«åããªããšããããšã ãã»ã£ãšããã
ããã ååã®ãšã³ããªãŒ ã§4ã»ã°ã¡ã³ãLEDã®æ©æ¢°åŠç¿ããã°ã©ã ãèªäœããã«ããã£ãŠãããŒãããäœãDeep Learning âPythonã§åŠã¶ãã£ãŒãã©ãŒãã³ã°ã®çè«ãšå®è£ ãïŒä»¥äžãããã¹ããïŒP114ïœ115 ã§å®çŸ©ãããã¯ã©ã¹ âTwoLayerNetâ ãããã³ãŒããããããæµçšãããã ã£ãããã£ããã®ã¯ã©ã¹ããããã厩ãããšã¯ãªããããªãããšæ°ã¥ããããšããååã®æ«å°Ÿã§è¿°ã¹ãã
ããããã¯ã©ã¹ã¯ã³ãŒããåå©çšããããã®ãã®ã ãããæ°ã¥ãã®ãé ããã ãã©ãã§ãã³ãŒããæµçšããããšã«ããã³ãŒãã®ç解ãé²ãã ãããåãããšããäºå確èªã«ããªã£ããããããã¯ããã§ããã£ãã®ãã
âTwoLayerNetâ ã¯ãããã¹ãP118ã§ããããªãµãã«åŒã³åºãããŠããã
network = TwoLayerNet(input_size=784, hidden_size=50, output_size=10)
åŒæ° âinput_sizeâ ãâhidden_sizeâ ãâoutput_sizeâ ã¯ãããã¹ãã®æµåã«åŸã£ãŠè¡åã®åœ¢ç¶ãè¡šèšããå Žåã®ãå ¥å âxâã第1å±€éã¿ âW1âã第2å±€éã¿ âW2âãåºå âyâ ã®ããããã®æ°å€ã«å¯Ÿå¿ããŠããããªã âxâ ãšâyâ ã®ããŒã¿æ°100ã¯ãããã°ã©ã ã®å¥ã®åæã®ãç¹°ãè¿ãïŒforïŒãã«ãã£ãŠå®çŸããã
ãã£ãœã4ã»ã°ã¡ã³ãLEDã®è¡åã®åœ¢ç¶ã¯ã次ã®ããã«ãªãã
ã ã£ãã âinput_sizeâ ã784ãã4ã«ã âhidden_sizeâã50ãã3ã«ã âoutput_sizeâ ã10ãã2ã«å€æŽããã ãã§ãããããªãããšæãã€ãããšã¯ã決ããŠé£ããã¯ãªãïŒãªãããããã®æ°å€ã®æå³ã¯ãç解ããŠããã€ããã ãããã§ã®èª¬æã¯çç¥ããïŒã
ã¹ãã³ãµãŒãªã³ã¯
Â
Â
次ã®ãããªã³ãŒãã§ãå®è¡ã§ããã
import sys, os
sys.path.append(os.pardir)
import numpy as np
from two_layer_net import TwoLayerNet
network = TwoLayerNet(input_size=4, hidden_size=3, output_size=2)x = np.array([[1, 1, 1, 1],[0, 0, 1, 0],[1, 0, 0, 0]])
t = np.array([[1, 0], [0, 1], [0, 1]])
learning_rate = 1.0Â
åååæ§ âxâ ã¯å ¥åããŒã¿ã âtâ ã¯æåž«ããŒã¿ã ãåŠç¿ç âleaning_rateâ ã1.0ãšããã®ãååãšåæ§ã ãæ¹é ããã®ã¯ãã®ãããã ã
ãããŠä»¥äžã®ã³ãŒããç¹°ãè¿ã [Ctrl] + [v] 㧠Anaconda Prompt ã«è²Œãä»ããã
network.predict(x)
np.argmax(network. predict(x), axis =1)
network.loss(x, t)
grad = network.numerical_gradient(x, t)
for key in ('W1', 'b1', 'W2', 'b2'):
  network.params[key] -= learning_rate * grad[key]Â
 ânetwork.predictâ ã¯æšå®å€ãä»åã argmax ïŒããã¹ãP80ïŒãåãŸããã
 ânetwork.lossâ ã¯æ倱ã§ããã
ååãšã®éãã¯ãfor æã«ãã£ã¯ã·ã§ããªã®ã㌠'W1'ã 'b1'ã 'W2'ã 'b2' ã䜿çšããããšã«ãã£ãŠãéã¿ãšãã€ã¢ã¹ã®æŽæ°ã2è¡ã§èšè¿°ã§ããããšã ãããããæ¹ã楜ã«æ±ºãŸã£ãŠãã python ã®ææ³ã«äžæ £ããªãã§ããã¿ãŸããã
貌ãä»ãå®è¡äžåç®ã
äºåç®ã
ä»åã¯34åç®ã®è²Œãä»ãã§æ£è§£ãåºãã
ããã ãã§ãæºè¶³åºŠãé«ãã£ããïŒãããŸã§å人ã®ææ³ïŒãã¡ãã£ãšæ¬²ãåºãã4ã»ã°ã¡ã³ãLEDãªããŠå®çšæ§ã®ãªããã®ã§ã¯ãªãã7ã»ã°ã¡ã³ãLEDã®æ©æ¢°åŠç¿ãããããããªãã ããããšã
ãããïŒãããããããïŒãããããããïŒ
7ã»ã°ã¡ã³ãLEDã«é¢ããŠã¯ããããªã«èª¬æã¯èŠããªããšæãããããã§èª¿ã¹ãŠãMSBïŒæäžäœãããïŒããLSBïŒæäžäœããããŸã§ããäžèšã®ããã«å²ãåœãŠãã
ããšäžå³ã¯ç§ããã¯ãŒãã€ã³ããçšããŠæãããã®ã ããæ®æ®µæã ãç®ã«ããŠãã7ã»ã°ã¡ã³ãLEDããç®ç«ãã¬ãšããã§å·¥å€«ãåããããŠããããã§ãããããã¶ã€ã³çã«ããã«æŽç·Žããããã®ã§ãããã«æ°ã¥ãããããæ©ãèšãã°èªåã§æãããã®ã¯ã€ã±ãŠãªãã
å ¥åããŒã¿ âxâ ãšæåž«ããŒã¿ âtâ ãã次ã®ããã«å®çŸ©ããããã£ãšã¹ããŒããªèšè¿°ãã§ãããã ãã欲ã¯åºããªãã§ããã
import numpy as np
x = np.array([\
[0, 1, 1, 1, 1, 1, 1], #0\
[0, 0, 0, 0, 1, 1, 0], #1\
[0, 1, 1, 0, 0, 0, 0], #1\
[1, 0, 1, 1, 0, 1, 1], #2\
[1, 0, 0, 1, 1, 1, 1], #3\
[1, 1, 0, 0, 1, 1, 0], #4\
[1, 1, 0, 1, 1, 0, 1], #5\
[1, 1, 1, 1, 1, 0, 1], #6\
[0, 0, 0, 0, 1, 1, 1], #7\
[1, 1, 1, 1, 1, 1, 1], #8\
[1, 1, 0, 1, 1, 1, 1]])#9t = np.array([\
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0], #0\
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0], #1\
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0], #1\
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0], #2\
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0], #3\
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0], #4\
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0], #5\
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0], #6\
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0], #7\
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0], #8\
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1] ])#9
ä»åã®å ¥å âxâã第1å±€éã¿ âW2âã第2å±€éã¿ âW2âãåºå âyâ ã®åœ¢ç¶ã¯ã次ã®ããã«ããŠã¿ãã
ãããšã¯ã©ã¹ âTwoLayerNetâ ã®åŒã³åºãæ¹ã¯ã次ã®ããã«ãªãã
import sys, os
sys.path.append(os.pardir)
from two_layer_net import TwoLayerNet
network = TwoLayerNet(input_size=7, hidden_size=11, output_size=10)learning_rate, step_num = 1.0, 100
åŠç¿ç âlearning_rateâ ã¯1.0ãšããŠã¿ããä»åã¯åæãŸã§äœçŸåã貌ãä»ããå¿ èŠããã£ãã®ã§ãforæã2éã«ããŠç¹°ãè¿ãåæ° âspep_numâ ã 100 ãšããŠã¿ããå®ã¯æåã«è©Šãããšãã«ã¯ãæåã§äœçŸåãšè²Œãä»ããã®ã ããå šç¶èŠã«ãªããªãã£ããã ããããã«ãããä»äººã«ãŸã§å§ããããã«ã¯ãããŸãã
network.predict(x)
np.argmax(network. predict(x), axis =1)
network.loss(x, t)for i in range(step_num):
  grad = network.numerical_gradient(x, t)
  for key in ('W1', 'b1', 'W2', 'b2'):
    network.params[key] -= learning_rate * grad[key] Â
ååã®å®è¡çµæãæšå®å€ ânetwork.predictâ ã®ãã³ããã§ãããªã£ãã®ã§ãargmax ã䜿ãããããã¿ãåºãããã«æããã
å®è¡100åç®ãæšå®å€ãçç¥ã㊠argmax ãšæ倱 ânetwork.lossâ ã ãã貌ãã
200åç®ã
300åç®ãã€ãã«æ£è§£ãåºãïŒ
ç¹°ãè¿ãåæ° âspep_numâ ã 100 ã§ã¯ãªã 500 ãªã©ã«ãããšãæåã®å®è¡ã§æ£è§£ãåºããã ãåŠçã«æéãããããçµæã衚瀺ãããã®ã«å°ãïŒäœç§ãïŒåŸ ããããã
ãããããããŸãããã®ã ã次ã®5ç« ã§èª€å·®äŒææ³ãšããã®ãå°å ¥ããŠãé«éåãå³ãå¿ èŠæ§ããããããããã ã
ãããïŒãããããããïŒãããããããïŒ
æåŸ éãã®çµæãåºãããšã«ãããããã«è©ŠããŠã¿ããããšã次ã ãšã§ããããã£ãŠãã®ã·ãªãŒãºã¯ãä»åã§å®çµãšãããå°»åããã³ãã®ãããªçµããæ¹ã ããããããããšã«éãã¯ãªãã®ã§ã
ãŒãããäœãDeep Learning âPythonã§åŠã¶ãã£ãŒãã©ãŒãã³ã°ã®çè«ãšå®è£
- äœè : æè€åº·æ¯
- åºç瀟/ã¡ãŒã«ãŒ: ãªã©ã€ãªãŒãžã£ãã³
- çºå£²æ¥: 2016/09/24
- ã¡ãã£ã¢: åè¡æ¬ïŒãœããã«ããŒïŒ
- ãã®ååãå«ãããã° (16件) ãèŠã