bp神经网络怎么实际应用啊?Python

原始数据输入,但是输出节点使用激活函数之后结果都是小于一的数,求出来的误差很大,而且误差会先减小后变大,感觉错误好多但不知道在哪


 import numpy as np


def sigmoid(x):
    return 1 / (1 + np.exp(-x))


def main():
    # 14条数据
    data = np.array([
        [1, 0, 0, 1, 0, 1, 1, 1],
        [0, 0, 1, 1, 0, 0, 1, 0],
        [1, 1, 0, 1, 1, 1, 0, 1],
        [0, 1, 0, 1, 0, 0, 1, 1],
        [1, 0, 1, 1, 0, 1, 1, 1],
        [1, 1, 0, 0, 1, 1, 1, 0],
        [0, 0, 0, 1, 0, 0, 1, 1],
        [1, 0, 1, 1, 0, 1, 1, 1],
        [1, 1, 0, 1, 0, 1, 0, 1],
        [1, 0, 0, 0, 1, 0, 1, 1],
        [1, 0, 0, 1, 0, 1, 1, 0],
        [0, 0, 1, 1, 0, 1, 0, 1],
        [1, 0, 0, 1, 0, 0, 1, 1],
        [0, 1, 0, 1, 0, 1, 1, 1]])
    print("原始数据:\n", data)
    # 十四条数据的跳高成绩
    highJump = np.array(
        [0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0])
    print("十四条数据的跳高成绩:\n", highJump)
    # 第十五条数据的输入
    data15 = np.array([0, 1, 0, 1, 1, 0, 1, 0])
    print("第十五条数据的输入:\n", data15)
    # 设置输入层与隐藏层之间的权值和阈值
    wInput = np.random.random(size=(6, 8))/10
    print("输入层与隐藏层之间的六组权值:\n", wInput)
    bInput = np.random.random(size=(6, 8))/10
    print("输入层与隐藏层之间的六组阈值:\n", bInput)
    # 设置隐藏层与输出层之间的权值和阈值
    wOutput = np.random.random(size=6)/10
    print("隐藏层与输出层之间的一组权值", wOutput)
    bOutput = np.random.random(size=6)/10
    print("隐藏层与输出层之间的一组阈值", bOutput)
    loss = 1
    count = 0
    while loss > 0.1:
        count = count + 1
        loss = 0
        outputNode = []
        for i in range(0, 14):

            # 正向传播
            # 计算隐藏层节点输入
            hide = []
            for j in range(0, 6):
                hideNode = 0
                for k in range(0, 8):
                    hideNode = data[i, k] * wInput[j, k] + \
                        bInput[j, k] + hideNode
                # print(hideNode)
                hideNode = sigmoid(hideNode)  # 激活函数
                hide.append(hideNode)
            hide = np.array(hide)
            # print("隐藏层结点", hide)
            output = 0
            for j in range(0, 6):
                output = hide[j] * wOutput[j] + bOutput[j] + output
            output = sigmoid(output)
            outputNode.append(output)
            # print("输出层结点", output)
            loss = ((output - highJump[i]) * (output - highJump[i])) / 2 + loss
        outputNode = np.array(outputNode)
        # 反向传播
        # print("隐藏层结点", hide)
        for i in range(0, 14):
            # 隐藏层与输出层之间权值阈值更新
            wOutputLoss = []
            for j in range(0, 6):
                wOutputLoss.append((outputNode[i] - highJump[i]) *
                                   outputNode[i] * (1 - outputNode[i])
                                   * hide[j])
            wOutputLoss = np.array(wOutputLoss)
            # print("wOutputLoss", wOutputLoss)
            bOutputLoss = []
            for j in range(0, 6):
                bOutputLoss.append((outputNode[i] - highJump[i]) *
                                   outputNode[i] * (1 - outputNode[i]))
            bOutputLoss = np.array(bOutputLoss)
            # print("bOutputLoss", bOutputLoss)
            for j in range(0, 6):
                wOutput[j] = wOutput[j] - 0.1 * wOutputLoss[j]
                bOutput[j] = bOutput[j] - 0.1 * bOutputLoss[j]
            # print("隐藏层与输出层更新后权值和阈值", wOutput, bOutput)
            # 输入层与隐藏层之间权值更新
            wInputLoss = np.ones((6, 8)) * 0
            for j in range(0, 6):
                for k in range(0, 8):
                    wInputLoss[j][k] = ((outputNode[i] - highJump[i]) *
                                        outputNode[i] *
                                        (1 - outputNode[i]) * wOutput[j]
                                        * hide[j] * (1 - hide[j]) * data[i][k])
            wInputLoss = np.array(wInputLoss)
            # print("wIutputLoss", wInputLoss)
            bInputLoss = np.ones((6, 8)) * 0
            for j in range(0, 6):
                for k in range(0, 8):
                    bInputLoss[j][k] = ((outputNode[i] - highJump[i]) *
                                        outputNode[i] * (1 - outputNode[i]) *
                                        wOutput[j] * hide[j] * (1 - hide[j]))
            bInputLoss = np.array(bInputLoss)
            # print("bIutputLoss", bInputLoss)
            for j in range(0, 6):
                for k in range(0, 8):
                    wInput[j][k] = wInput[j][k] - 0.1 * wInputLoss[j][k]
                    bInput[j][k] = bInput[j][k] - 0.1 * bInputLoss[j][k]
            # print("输入层与隐藏层之间更新后的权值和阈值", wInput, bInput)
            # print("输出", output)
        print("学习前的loss", loss)
        loss = 0
        for i in range(0, 14):
            # 正向传播
            # 计算隐藏层节点输入
            hide = []
            for j in range(0, 6):
                hideNode = 0
                for k in range(0, 8):
                    hideNode = data[i, k] * wInput[j, k] + \
                        bInput[j, k] + hideNode
                hideNode = sigmoid(hideNode)  # 激活函数
                hide.append(hideNode)
            hide = np.array(hide)
            output = 0
            for j in range(0, 6):
                output = hide[j] * wOutput[j] + bOutput[j] + output
            output = sigmoid(output)
            loss = ((output - highJump[i]) * (output - highJump[i])) / 2 + loss
            # print("输出", output)
        print("学习后的loss", loss)

    # 预测
    hide = []
    for j in range(0, 6):
        hideNode = 0
        for k in range(0, 8):
            hideNode = data15[k] * wInput[j, k] + \
                bInput[j, k] + hideNode
            hideNode = sigmoid(hideNode)  # 激活函数
        hide.append(hideNode)
    hide = np.array(hide)
    output = 0
    for j in range(0, 6):
        output = hide[j] * wOutput[j] + bOutput[j] + output
    output = sigmoid(output)
    print(output)
    print(loss)
    print(count)


if __name__ == '__main__':
    main()

2个回答

解决了。。。。。。。

GDdenvfen
GDdenvfen 能发下代码么
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问