아래와 같이 1개의 은닉층을 가진 신경망을 구성해 보았습니다.
import numpy as np
def sigmoid(x):
return 1/(1+np.exp(-x))
def DeltaSGD(W1, W2, X, D):
alpha = 0.9
idx = 0
for k in X:
a1 = np.dot(W1, k)
z1 = sigmoid(a1)
a2 = np.dot(W2, z1)
y = sigmoid(a2)
e = D[idx] - y
delta = y * (1-y) * e
e1 = W2 * delta
delta1 = z1 * (1-z1) * e1
dw1 = alpha * delta1
dw1 = dw1.reshape(4,1)
x = k.reshape(1,3)
W1 += np.dot(dw1, x)
dw2 = alpha * delta * z1
W2 += dw2
idx+=1
def Train(epoch, x, y, W1, W2):
for ep in range (epoch):
DeltaSGD(W1, W2, x, y)
def main():
W1 = np.random.randn(4,3)
W2 = np.random.randn(4)
x = np.array([[0, 0, 1], [0, 1, 1], [1, 0, 1], [1, 1, 1]])
y = np.array([0, 1, 1, 0])
trainCount = 10000
print('---Before Training---')
print(W1)
print(W2)
print('---After Training---')
Train(trainCount, x, y, W1, W2)
print(W1)
print(W2)
print('---Result---')
for k in x:
a1 = np.dot(W1, k)
z1 = sigmoid(a1)
a2 = np.dot(W2, z1)
y = sigmoid(a2)
print(y)
if __name__ == '__main__':
main()
---Before Training--- [[-0.82542712 -0.65157477 0.09131077] [-0.12367401 1.07601221 -1.43941252] [-1.00401084 -0.04547823 0.97194216] [ 0.02719329 -0.22932678 -0.45809196]] [ 0.16840571 -0.55206913 1.75255171 -0.54203599] ---After Training--- [[-6.3608461 -6.35976778 2.71126418] [ 3.61918383 3.62562196 -5.65337702] [-3.70117648 -3.70158904 5.76910815] [-1.51981715 -1.56682777 -0.58710802]] [-11.12882426 -6.71646542 6.50908732 -1.52377049] ---Result--- 0.01083108954199271 0.9896067125952175 0.9895881791368779 0.01040259419256147 |