前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >训练三层BP神经网络实现异或运算 Python 代码实现

训练三层BP神经网络实现异或运算 Python 代码实现

作者头像
用户1148525
发布2019-05-26 12:15:48
2K0
发布2019-05-26 12:15:48
举报

本文主要使用下面的网络结构来完成 异或运算 异或运算 : 0^0 = 0, 1^0 = 1, 0^1 = 1, 1^1 = 0 。

这里写图片描述
这里写图片描述

上图的公式推导可以参考博文: 三层神经网络前向后向传播示意图

代码语言:javascript
复制
import numpy as np

def nonlin(x, deriv = False):
    if(deriv == True):
        return x*(1-x)
    else:
        return 1/(1+np.exp(-x))

#input dataset
X = np.array([[0,0],
             [1,0],
             [0,1],
             [1,1]])

#output dataset
y = np.array([[0,1,1,0]]).T

1 #the first-hidden layer weight value
syn0 = 2*np.random.random((2,3)) - 

#the hidden-hidden layer weight value
syn1 = 2*np.random.random((3,2)) - 1 

#the hidden-output layer weight value
syn2 = 2*np.random.random((2,1)) - 1 

for j in range(60000):
     #the first layer,and the input layer 
    l0 = X     
    # the first hidden layer                      
    l1 = nonlin(np.dot(l0,syn0)) 

    # the second hidden layer        
    l2 = nonlin(np.dot(l1,syn1)) 

    # the output layer 
    l3 = nonlin(np.dot(l2,syn2)) 


    l3_error = y-l3       #the hidden-output layer error

    if(j%10000) == 0:
        print ("Error:"+str(np.mean(l3_error)))

    l3_delta = l3_error*nonlin(l3,deriv = True)

    l2_error = l3_delta.dot(syn2.T)  

    l2_delta = l2_error*nonlin(l2,deriv = True)

    #the first-hidden layer error
    l1_error = l2_delta.dot(syn1.T)     
    l1_delta = l1_error*nonlin(l1,deriv = True)

    syn2 += l2.T.dot(l3_delta)
    syn1 += l1.T.dot(l2_delta)
    syn0 += l0.T.dot(l1_delta)

print( "outout after Training:")
print (l3)
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016年12月07日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档