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
推荐阅读
- JS中的各种宽高度定义及其应用
- python学习之|python学习之 实现QQ自动发送消息
- 逻辑回归的理解与python示例
- python自定义封装带颜色的logging模块
- 【Leetcode/Python】001-Two|【Leetcode/Python】001-Two Sum
- Python基础|Python基础 - 练习1
- Python爬虫|Python爬虫 --- 1.4 正则表达式(re库)
- Python(pathlib模块)
- python青少年编程比赛_第十一届蓝桥杯大赛青少年创意编程组比赛细则
- Python数据分析(一)(Matplotlib使用)