【问题标题】:Neural Network increases loss instead of decreasing it神经网络增加损失而不是减少损失
【发布时间】:2021-11-07 05:37:24
【问题描述】:

我使用了两个在线参考在 python 中构建了一个具有四个输入节点、一层 4 个隐藏节点和 6 个输出节点的神经网络。当我运行网络时,损失增加而不是减少,我认为这意味着它的预测越来越差。

对不起,代码的海洋,我不知道代码中的问题可能出在哪里。我所做的一切都无法解决这个问题。我的代码有问题,还是我对损失函数的假设有误?

import numpy as np

#defining inputs and real outputs
inputData = np.array([[10.0, 5.0, 15.0, 3.0],
                      [9.0, 6.0, 16.0, 4.0],
                      [8.0, 4.0, 17.0, 5.0],
                      [7.0, 3.0, 18.0, 6.0],
                      [6.0, 2.0, 19.0, 7.0]])

statsReal = np.array([[0.0, 0.2, 0.4, 0.6, 0.8, 1.0],
                      [0.0, 0.2, 0.4, 0.6, 0.8, 1.0],
                      [0.0, 0.2, 0.4, 0.6, 0.8, 1.0],
                      [0.0, 0.2, 0.4, 0.6, 0.8, 1.0],
                      [0.0, 0.2, 0.4, 0.6, 0.8, 1.0]])


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

def sigmoid_d_dx(x):
    return sigmoid(x) * (1 - sigmoid(x))

def softmax(A):
    expA = np.exp(A)
    return expA / expA.sum(axis=1, keepdims=True)

#defining the hidden and output nodes, and the weights and biases for hidden and output layers
instances = inputData.shape[0]
attributes = inputData.shape[1]
hidden_nodes = 4
output_nodes = 6

wh = np.random.rand(attributes,hidden_nodes)
bh = np.random.randn(hidden_nodes)

wo = np.random.rand(hidden_nodes,output_nodes)
bo = np.random.randn(output_nodes)
learningRate = 10e-4

error_cost = []

for epoch in range(100):

    #Feedforward Phase 1
    zh = np.dot(inputData, wh) + bh
    ah = sigmoid(zh)

    #Feedforward Phase 2
    zo = np.dot(ah, wo) + bo
    ao = softmax(zo)
    
    #Backpropogation Phase 1
    dcost_dzo = ao - statsReal
    dzo_dwo = ah
    dcost_wo = np.dot(dzo_dwo.T, dcost_dzo)
    dcost_bo = dcost_dzo

    #Backpropogation Phase 2
    dzo_dah = wo
    dcost_dah = np.dot(dcost_dzo, dzo_dah.T)
    dah_dzh = sigmoid_d_dx(zh)
    dzh_dwh = inputData
    dcost_wh = np.dot(dzh_dwh.T, dah_dzh * dcost_dah)
    dcost_bh = dcost_dah*dah_dzh

    #Weight Updates
    wh -= learningRate * dcost_wh
    bh -= learningRate * dcost_bh.sum(axis=0)
    wo -= learningRate * dcost_wo
    bo -= learningRate * dcost_bo.sum(axis=0)

    loss = np.sum(-statsReal * np.log(ao))
    print(loss)
    error_cost.append(loss)

print(error_cost)```

【问题讨论】:

  • 您的目标总和不等于 1.0,应该是一个热编码数组,如 [0.0, 0.0., 1.0, 0.0, 0.0, 0.0](真实类为 1.0,否则为 0.0)。损失函数应该是交叉熵。我不知道你的损失函数是做什么的。

标签: python numpy neural-network


【解决方案1】:

当您使用合理的数据进行训练时,您的网络正在学习。

以这个数据为例。我为每个类添加了一个不同的案例,并对目标进行了热编码。我将输入缩放为[0.0, 1.0]

inputData = np.array([[1.0, 0.5, 0.0, 0.0],
                      [0.0, 1.0, 0.5, 0.0],
                      [1.0, 0.0, 0.0, 1.0],
                      [0.0, 1.0, 0.0, 0.5],
                      [0.0, 0.0, 0.0, 1.0],
                      [1.0, 1.0, 0.5, 0.0]])

statsReal = np.array([[1.0, 0.0, 0.0, 0.0, 0.0, 0.0],
                      [0.0, 1.0, 0.0, 0.0, 0.0, 0.0],
                      [0.0, 0.0, 1.0, 0.0, 0.0, 0.0],
                      [0.0, 0.0, 0.0, 1.0, 0.0, 0.0],
                      [0.0, 0.0, 0.0, 0.0, 1.0, 0.0],
                      [0.0, 0.0, 0.0, 0.0, 0.0, 1.0]])

提高学习率

learningRate = 10e-2

训练更多的时期并减少打印的频率。

for epoch in range(1000):
#....
    if epoch % 100 == 99: print(loss)

损失函数的输出

6.116573523774877
2.6901680150532847
1.323221228926058
0.7688474199923144
0.5186915091033664
0.38432651801528794
0.3024486736712547
0.24799685736356275
0.20944414625474833
0.1808455098847857

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-12
    • 2022-10-21
    • 2020-06-17
    • 2018-12-15
    • 1970-01-01
    • 2019-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多