蒙蒙蒙王 2018-10-26 08:28 采纳率: 100%
浏览 378
已采纳

跪求大神帮我检查一下BP算法计算异或问题程序的错误

感觉是权值更新部分出错了
运行出来都是0.5左右 不知道为啥

import math
import numpy as np
import random as rd
def sigmoid(x):
    f = 1/(1+np.exp(-1*x))
    return f

def sigmoid_(x):

    f = [[sigmoid(x[i][j])*(1-sigmoid(x[i][j]))  for j in range(len(x[i]))] for i in range(len(x))]
    return f

def differentail_matrix(x):
    f = np.diag(x[0])
    return f

input_X = np.array([[0,0],[1,0],[0,1],[1,1]])
#print(input_X.shape)
input_Y = np.array([[0],[1],[1],[0]])
#print(input_Y.shape)

W1 = np.random.rand(2,2) #later input,former output
b1 = np.zeros([2,1])
#W1 = np.array([[2,2],[-1,-1]])
#b1 = np.array([[-1],[1.5]])
W2 = np.random.rand(1,2)
b2 = np.random.rand(1,1)
# W2 = np.array([1,1])
# W2.shape = 1,2
# b2 = np.array([-1.0])
#print(W1,b1)

alpha = 0.05 #learn rate
for k in range(10000):

    r = rd.sample([0,1,2,3], 1)           #随机抽取



    X = np.array(input_X[r])
    X.shape = 2,1                        #transpose
    Y = np.array(input_Y[r])

    out1 = sigmoid(np.dot(W1,X) + b1)
    pred_y = sigmoid(np.dot(W2,out1) + b2)
    #print('predy:',pred_y)
    err = Y-pred_y
    #print('err:',err)


    #back propagation

    s2 = -2*sigmoid(pred_y)*(1-sigmoid(pred_y))*err         #计算敏感度

    temp = sigmoid_(out1)

    temp = np.array(temp)
    temp.shape = 1, 2
    temp2 = np.array(differentail_matrix(temp))
    s1 = np.dot(temp2, W2.T)*s2

    W2 = W2-alpha*s2*np.transpose(out1)               #权值更新
    b2 = b2 - alpha*s2

    W1 = W1 - alpha*np.dot(s1,np.transpose(X))
    b1 = b1 - alpha*s1


    #print('第:',i,'次迭代','\n','权值1',W1,'\n',b1)
    #print()


    if k%500 == 0:
        out1 = sigmoid(np.dot(W1,np.transpose(input_X)) + b1)
        pred_y = sigmoid(np.dot(W2,out1) + b2)
        print(pred_y)
  • 写回答

2条回答 默认 最新

  • 蒙蒙蒙王 2019-07-12 15:20
    关注

    暂时没有解决方法。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效