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

本文介绍了一种使用三层神经网络解决异或问题的方法。通过随机初始化权重,并利用梯度下降进行训练,最终实现了对异或函数的有效逼近。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

这里写图片描述

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

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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值