BP神经网络及其Python实现

BP神经网络及其Python实现_第1张图片

BP神经网络及其Python实现_第2张图片

 

BP神经网络及其Python实现_第3张图片

 

 

import numpy as np
def Sigmoid( v ):
    y = 1 / ( 1 + np.exp( -v ) )
    return y
def TrainWByBP( W1, W2, X, D ):
    alpha = 0.9
    [ M, N ] = np.shape( X ) 
    for i in range( M ):
        #Forword propagation
        x = np.array( X[i] )
        d = D[i]
        v1 = np.dot( W1, x )
        y1 = Sigmoid( v1 ) 
        v = np.dot( W2, y1 )    
        y = Sigmoid( v )
        #Back propagation
        e = d - y 
        delta = y * ( 1 - y ) * e
        e1 = np.transpose( W2 ) * delta
        delta1 = np.multiply( np.multiply( y1, ( np.ones( np.shape(y1) ) - y1 ) ), np.transpose(e1) )
        
        x1 = np.zeros( [1, N] )
        k = 0
        for data in x:
            x1[0][k] = data
            k += 1
        deltaW1 = np.dot( ( alpha * np.transpose(delta1) ), x1 )
        W1 = W1 + deltaW1
        
        deltaW2 = np.multiply( ( alpha * delta ),  np.transpose( y1 ) )
        W2 = W2 + deltaW2
      
    return W1, W2
        
def trainMultiNNBP( X, D ): 
    k = 5
    [ M, N ] = np.shape( X )
    W1 = np.random.rand( k, N )
    W2 = np.random.rand( 1, k )
    for epoch in range(10000):
        [W1, W2]  = TrainWByBP( W1, W2, X, D )
    return W1, W2

def main():
    X = [ [0,0,1], [0,1,1], [1,0,1], [1,1,1] ]
    D = [ 0, 1, 1, 0 ]
    [ M, N ] = np.shape( X )
    [W1, W2] = trainMultiNNBP( X, D )
    print( 'W1 = ', W1 )
    print( 'W2 = ', W2 )
    print()
    for i in range( M ):
        x = X[i]
        v1 = np.dot( W1, x )
        y1 = Sigmoid( v1 )
        v = np.dot( W2, y1 )
        y = Sigmoid( v )
        print( y )
    print()
    
if __name__ == '__main__':
    main()

作者:YangYF

你可能感兴趣的:(BP神经网络及其Python实现)