Coding/Python 삽질기2020. 7. 28. 23:56

 

import numpy as np

def sigmoid(x):
    return 1/(1+np.exp(-x))


def DeltaSGD(W, X, D):
    alpha = 0.9
    dwSum = np.array([0] * 3)
    
    idx = 0
    for k in X:
        a1 = np.dot(k, W)
        y = sigmoid(a1)
        
        e = D[idx] - y
        delta = y * (1-y) * e
        
        dw = alpha * delta * k
        W += dw
        idx+=1

        
def Train(epoch, x, y, W1):
    for ep in range (epoch):
        DeltaSGD(W1, x, y)

        
def main():
    W1 = np.random.randn(3)
    x = np.array([[0, 0, 1], [0, 1, 1], [1, 1, 1], [1, 0, 0]])
    y = np.array([1, 1, 1, 0])

    Train(10000, x, y, W1)
    
    for k in x:
        a1 = np.dot(k, W1)
        z1 = sigmoid(a1)
        print(z1)    
        
        
if __name__ == '__main__':       
    main()  

 

0.9962960119369554
0.9999259998523653
0.9929612087250037
0.010332095384834877

'Coding > Python 삽질기' 카테고리의 다른 글

[Python][AI] XOR 예제  (0) 2020.08.03
[Python] fisher-yates shuffle  (0) 2020.07.20
강아지 나이 계산기  (0) 2020.07.16
Posted by chobocho