ãã¿ãŸããã察象ã¯æè€åº·æ¯ ããŒãããäœãDeep Learning âPythonã§åŠã¶ãã£ãŒãã©ãŒãã³ã°ã®çè«ãšå®è£ ã(O'REILLY) èªè éå®ã®èšäºã«ã€ããæ°çãç®æ±ããé¿ããããæ¥ä»ãããã®ãŒã£ãŠå ¬éããŸããåŒããã°ã§ã¯ããšãã©ãããããããšããããŸãã
Â
2幎åãæŸçœ®ããŠããã·ãªãŒãºãããã
æ¹éã¯ãæä»çè«çåEORãå®çŸããéã¿è¡å W0 ãå®æ°ãã¯ãã« b0ãéã¿ãã¯ãã« W1ãå®æ° b1 ããæ©æ¢°åŠç¿ã«ãã£ãŠæ±ããããšã ã£ãã
ä»æ°ã¥ãããã©ãäžæããŠãããšã³ããªãŒã§ã¯æä»çè«çåãEORã§ã¯ãªãXORãšæžããŠããŸã£ãŠãããªãäžçµ±äžãã¿ãŸããã
念ã®ããççå€è¡šã瀺ããå ¥åããŒã¿0ãšå ¥åããŒã¿1ã«å¯Ÿããæåž«ããŒã¿0ãæä»çè«çåãæåž«ããŒã¿1ã¯æä»çè«çåã®åŠå®ã§ããã
å ¥å0 | å ¥å1 | æåž«0 | æåž«1 |
0 | 0 | 0 | 1 |
1 | 0 | 1 | 0 |
0 | 1 | 1 | 0 |
1 | 1 | 0 | 1 |
ãšããããããããªããªãæã£ãéãã«ãããªãã£ããåä¿æ°ããã€ãŸã§ãã£ãŠãåæããªãã£ãã®ã ã
ãããžå®ç掻ã§ãã£ãããªãã©ãã«ã«èŠèãããäžéå端ã«æŸçœ®ããããåŸãªãã£ãã
ãã©ãã«ã¯ãªããšãçä»ããã®ã«çŽ2幎ããã£ããçµç·¯ã¯ããã°ã«æžãããã©ãããããè¿·ã£ãŠããã
å幎ã»ã©åãããæ©æ¢°åŠç¿ã®å匷ããŒã€ãŒã€ãšåéããã2幎ãéãéããšã现ãããšãããå¿ããŠããŸã£ãŠãããããããäžæãããšãããŸã§è¿œãã€ããã®ããä»å¹Žã®å¹Žæããããã ã£ããÂ
ãŒãããäœãDeep Learning âPythonã§åŠã¶ãã£ãŒãã©ãŒãã³ã°ã®çè«ãšå®è£
- äœè :æè€ åº·æ¯
- çºå£²æ¥: 2016/09/24
- ã¡ãã£ã¢: åè¡æ¬ïŒãœããã«ããŒïŒ
Excel ãžã®ç§»æ€ã¯ãã°ããè«ŠããPython ã§èšè¿°ããããšã«ããã
次ã«ç€ºã Python ã¹ã¯ãªããã¯ãæä»çè«çåEORãå®çŸããäžäŸã§ãããäžæ²æžããŒãããäœãDeep Learningãã®å¯Ÿå¿ããŒãžãã³ã¡ã³ãã«ç€ºããã
以äžã䟿å®çã«ãã³ãŒã1ããã³ãŒã2ãâŠãªã©ãšå称ãã€ããã
#ã³ãŒã1
import sys, os
import numpy as np
sys.path.append(os.pardir) #P73 äžäœãã©ã«ãããã¹æå®
from common.functions import *x = np.array([[0, 0], [1, 0], [0, 1], [1, 1]]) # å ¥å
W1 = np.array([[-0.5, 0.5], [-0.5, 0.5]]) #第1å±€ P61æ¹å€
b1 = np.array([0.7, -0.3]) #ä¿æ°ã¯P26ããã®ãŸãŸäœ¿çšA1 = np.dot(x,W1) + b1
Z1 = step_function(A1) #ã¹ãããé¢æ° P47W2 = np.array([[0.5, -0.5], [0.5, -0.5]]) #第2å±€ P61æ¹å€
b2 = np.array([-0.7, 0.7]) #ä¿æ°ã¯P27ããã®ãŸãŸäœ¿çšA2 = np.dot(Z1,W2) + b2
y = step_function(A2) #ã¹ãããé¢æ° P47print(y)
Â
ããŒãããäœãDeep LearningããŸãããviiãxiã«ãããµã³ãã«ã¹ã¯ãªãããããŠã³ããŒããããã£ã¬ã¯ããªã«ç§»åã㊠Anagonda ããã³ããã®å¯Ÿè©±ã¢ãŒãç»é¢ã«äžæ²ã³ãŒãã貌ãä»ãããšãå®è¡çµæã
[[0 1]
[1 0]
[1 0]
[0 1]]Â
ã®ããã«è¡šç€ºãããã¯ãã§ããïŒWindows 10 Homeãpython version3.8.3ã«ãŠç¢ºèªïŒã
ãªã察話åã¢ãŒãã®ãããšããã¯ãããã³ããã«å€æ°ãå ¥åããã ãã§èšç®éçšã衚瀺ã»ç¢ºèªã§ããããšã§ããã
>>> A1
array([[ 0.7, -0.3],
[ 0.2, 0.2],
[ 0.2, 0.2],
[-0.3, 0.7]])
>>> Z1
array([[1, 0],
[1, 1],
[1, 1],
[0, 1]])
>>> A2
array([[-0.2, 0.2],
[ 0.3, -0.3],
[ 0.3, -0.3],
[-0.2, 0.2]])
ãA1 = np.dot(x,W1) + b1ããA2 = np.dot(Z1,W2) + b2ãã® "np.dot" ã¯å ç© P55ã"+" ã¯ãããŒããã£ã¹ãæŒç® P14 ã§ããã
ã€ãŸã第1å±€ã§ã¯ORãšNANDãäœãã第2å±€ã§ã¯äž¡è ã®ANDããšã£ãŠããã®ã§ããã
ãã®ä¿æ°W1ãW2ãb1ãb2ãã人éãäžããã®ã§ã¯ãªãæ©æ¢°åŠç¿ã§æ±ãããã
Â
è©Šè¡é¯èª€ã®éçšã¯çç¥ãçµè«ãæ¥ãã ç¶ããŠããŒãããäœãDeep LearningãP114ã®2å±€ãã¥ãŒã©ã«ãããã¯ãŒã¯ãæ¹é ããŠã次ã®ãããªã³ãŒããäœæããã
#ã³ãŒã2
import sys, os
sys.path.append(os.pardir)
import numpy as np
from common.functions import *
from common.gradient import numerical_gradientx_e = np.array([[0, 0], [1, 0], [0, 1], [1, 1]]) #å ¥åããŒã¿
t_e = np.array([[1, 0], [0, 1], [0, 1], [1, 0]]) #æåž«ããŒã¿weight_init_std=0.1
W1 = weight_init_std * np.random.randn(2, 2) #ã¬ãŠã¹ååž
W2 = weight_init_std * np.random.randn(2, 2) #ä¹±æ°ã§åæå
b1, b2 = np.zeros(2), np.zeros(2) #ãŒãã§åæådef predict(x):
  A1 = np.dot(x,W1) + b1
  Z1 = sigmoid(A1) #ã·ã°ã¢ã€ãé¢æ°P48
  A2 = np.dot(Z1,W2) + b2
  y = softmax(A2) #ãœããããã¯ã¹é¢æ°P66
  return yÂ
倪åããã©ã³ãè²å€æŽãããŠããã Anaconda ããã³ããã«ã¯è²Œãã€ãã¯ãã§ããã
éã¿W1ãW2ã®åœ¢ç¶ã¯2Ã2ããã€ã¢ã¹b1ãb2ã®èŠçŽ æ°ã¯2ã§ããã
W1ãW2ãã¬ãŠã¹ååžã«ããçæãããä¹±æ°ã§ãb1ãb2ããŒãã§åæåããŠããããšãã·ã°ã¢ã€ãé¢æ°ãšãœããããã¯ã¹é¢æ°ãå°å ¥ããŠããããšãªã©ãå°ã説æãããç®æããããããã£ããçç¥ããã
ã ãããã§ã¯ãæã£ãããã«åäœããŠãããªãã£ããW1ãW2ãb1ãb2ãæ±ããããšãã§ããªãã£ãã
è¿œèšïŒ
weight_init_std ã®å€ãå€æŽãããšè§£ãåŸãããããšããã£ããã ãå®å®çã«åŸãããããã§ã¯ãªãããªãã§ã ããïŒ
è¿œèšããã
è¿œèšã®è¿œèšïŒ
ã#ã³ãŒã2ãã®äžã®æåž«ããŒã¿ t_e ãççå€ã«ãããš
å ¥å0 | å ¥å1 | æåž«0 | æåž«1 |
0 | 0 | 1 | 0 |
1 | 0 | 0 | 1 |
0 | 1 | 0 | 1 |
1 | 1 | 1 | 0 |
ãš1åç®ãæåž«0ããEORã®åŠå®ã2åç®ãEORãšãªããããã¯ã¿ãæ¬ããŒãããäœãDeep Learningãã§ãåºåçµæã«ããäžæ®µ numpy ã® argmax() ã¡ãœããïŒæ倧å€ãæã€åãåãåºãé¢æ°ãP80åç §ïŒãåãŸããŠããã®ãããã®ãŸãŸèžè¥²ããããã
è¿œèšã®è¿œèšããã
ããã§ãW1ã®åœ¢ç¶ã2Ã3ãW2ã®åœ¢ç¶ã3Ã2ããã€ã¢ã¹b1ã®èŠçŽ æ°ã3ãb2ã®èŠçŽ æ°ã2ã«å€æŽãããâŠ
#ã³ãŒã3
import sys, os
sys.path.append(os.pardir)
import numpy as np
from common.functions import *
from common.gradient import numerical_gradientx_e = np.array([[0, 0], [1, 0], [0, 1], [1, 1]]) #å ¥åããŒã¿
t_e = np.array([[1, 0], [0, 1], [0, 1], [1, 0]]) #æåž«ããŒã¿weight_init_std=0.1
W1 = weight_init_std * np.random.randn(2, 3) #ã¬ãŠã¹ååž
W2 = weight_init_std * np.random.randn(3, 2) #ä¹±æ°ã§åæå
b1, b2 = np.zeros(3), np.zeros(2) #ãŒãã§åæådef predict(x):
  A1 = np.dot(x,W1) + b1
  Z1 = sigmoid(A1) #ã·ã°ã¢ã€ãé¢æ°P48
  A2 = np.dot(Z1,W2) + b2
  y = softmax(A2) #ãœããããã¯ã¹é¢æ°P66
  return yÂ
ãŸãã¯W1ã®åœ¢ç¶ã2Ã4ãW2ã®åœ¢ç¶ã4Ã2ããã€ã¢ã¹b1ã®èŠçŽ æ°ã4ãb2ã®èŠçŽ æ°ã2ã«å€æŽã«ãããšããâŠ
#ã³ãŒã4
import sys, os
sys.path.append(os.pardir)
import numpy as np
from common.functions import *
from common.gradient import numerical_gradientx_e = np.array([[0, 0], [1, 0], [0, 1], [1, 1]]) #å ¥åããŒã¿
t_e = np.array([[1, 0], [0, 1], [0, 1], [1, 0]]) #æåž«ããŒã¿weight_init_std=0.1
W1 = weight_init_std * np.random.randn(2, 4) #ã¬ãŠã¹ååž
W2 = weight_init_std * np.random.randn(4, 2) #ä¹±æ°ã§åæå
b1, b2 = np.zeros(4), np.zeros(2) #ãŒãã§åæådef predict(x):
  A1 = np.dot(x,W1) + b1
  Z1 = sigmoid(A1) #ã·ã°ã¢ã€ãé¢æ°P48
  A2 = np.dot(Z1,W2) + b2
  y = softmax(A2) #ãœããããã¯ã¹é¢æ°P66
  return yÂ
æ©æ¢°åŠç¿ã«ããW1ãW2ãb1ãb2ãæ±ããããšãã§ããïŒ ãªãã§ã ïŒïŒ
Â
ããªãã¡äžæ²ãã³ãŒã3ããŸãã¯ãã³ãŒã4ããšã次ã«ç€ºããã³ãŒã5ããAnacondaããã³ããã«è²Œãä»ããŠãæ£è§£çaccãšæ倱é¢æ°ã®å€lossã®ã°ã©ãã衚瀺ãããã®ã§ããã
æ倱é¢æ°ã®ã°ã©ãã¯ããŒãããäœãDeep LearningãP119å³4-11ãæ£è§£çã®ã°ã©ãã¯P121å³4-12ã
#ã³ãŒã5 ch04\two_layer_net æ¹é
def loss(x, t): #æ倱é¢æ°
  y = predict(x)
  return cross_entropy_error(y, t)
def accuracy(x, t): #æ£çç
  y = predict(x)
  y = np.argmax(y, axis=1)
  t = np.argmax(t, axis=1)
  accuracy = np.sum(y == t) / float(x.shape[0])
  return accuracy
import matplotlib.pyplot as plt #ã°ã©ãæç»æºå
loss_W = lambda W: loss(x_e, t_e)
loss_list = [ ]
acc_list = [ ]
learning_rate = 5.0 #åŠç¿ç
step_num = 200 #ç¹°ãè¿ãåæ°for i in range(step_num): #ç¹°ãè¿ãåŠç¿
  W1 -= learning_rate*numerical_gradient(loss_W, W1)
  b1 -= learning_rate*numerical_gradient(loss_W, b1)
  W2 -= learning_rate*numerical_gradient(loss_W, W2)
  b2 -= learning_rate*numerical_gradient(loss_W, b2)
  loss_list.append(loss(x_e,t_e))
  acc_list.append(accuracy(x_e, t_e))
x = np.arange(len(loss_list))
plt.plot(x, loss_list, label='loss')
plt.plot(x, acc_list, label='acc', linestyle='--')
plt.xlabel("iteration") #x軞ã©ãã«
plt.legend() #å¡äŸ
plt.show()Â
 ããã¯ãã£ãä»ãã³ãŒã3ãšã³ãŒã5ã貌ãä»ããŠå®è¡ããçµæâŠ
Â
ããã¯ã³ãŒã4ãšã³ãŒã5ã貌ãä»ããŠå®è¡ããçµæãåŸãããã°ã©ãã§ããã
Â
iterationã¯ç¹°ãè¿ãåæ°ã§ãããæ倱é¢æ°ã®å€lossã¯0ã«è¿ãã»ã©ãããæ£è§£çaccã¯1ã«ãªã£ãŠã»ããã
ä»åã¯ããŸããŸãã³ãŒã3+ã³ãŒã5ãã³ãŒã4+ã³ãŒã5ãæåŸ éãã®çµæãåŸãããã
ãšããããå®è¡ãããã³ã«ã°ã©ãã®åœ¢ç¶ãæ¿ããéã£ãã®ã ïŒ
æ£è§£çã1ã«ãªããªããããªãã¡åŠç¿çµæãæ£ããåŸãããªãããšããå°ãªããªãã£ãïŒ
ãã£ããäœãèµ·ããŠããã®ã ããïŒ
ãšããããšã§ãããå°ãæ·±ãæã£ãŠã¿ãããšã«ããã
æå¿«ãªçµè«ãåºããããã§ã¯ãªãããããã§ãå°ãããã£ãããšãããã®ã§ãã ãã ãç¶ããããšæãã
ã¹ãã³ãµãŒãªã³ã¯
Â