BP神经网络及其Python实现

BP神经网络及其Python实现
文章图片

【BP神经网络及其Python实现】BP神经网络及其Python实现
文章图片


BP神经网络及其Python实现
文章图片



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 + deltaW1deltaW2 = np.multiply( ( alpha * delta ),np.transpose( y1 ) ) W2 = W2 + deltaW2return W1, W2def 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, W2def 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

    推荐阅读