【问题标题】:Why is my custom neural network implementation stuck at 0.25 cost?为什么我的自定义神经网络实现的成本停留在 0.25?
【发布时间】:2022-01-18 20:27:03
【问题描述】:

我制作了一个自定义神经网络库,该库的损失率为 0.25。该网络是一个 2-2-1 网络,这意味着它有两个输入,一个大小为 2 的隐藏层和一个输出。我在 XOR 数据集上训练它。 (异或)
我知道的:
此实现仅适用于一层。
这让我相信错误出现在我的代码部分的某个地方,在那里我找到了早期层中激活对成本函数的导数,因为当只有两层时,你不需要这样做,如果你这样做没关系。

单个训练样例back-prop的伪代码:

forward(inputs)
for each neuron in last layer:
    neuron.cost = neuron.activation - neuron.expected_output
#this is getting the cost for each neuron

for each layer except the last one(i):
    for each neuron in layer(j):
        for each neuron in layer+1(h):
            #this is where i calc the deriv of each weight
            layer.weight[(the one connecting J and H)].deriv += layer.activations[j] *
                            (layer+1.actFuncPrime(layerNext.z[h]) * 2 * layerNext.costs[h]);
           # "z" is the weighted sum before activation function is applied
            layer.neurons[j].cost += layer.weights[(the one connecting J and H)] *
                            (layer+1.actFuncPrime(layerNext.z[h]) * 2 * layerNext.costs[h]);
           

#also i calculate the bias sensitivities but I dont think it needs to be shows here.

真正的javascript代码:
this.forward(input);

for (var i = 0; i < this.layers[this.layers.length - 1].size; i++) {
    let layer = this.layers[this.layers.length - 1];
    layer.costs[i] = layer.activations[i] - expectedOut[i]; //we will power^2 at the end.
}

for (i = this.layers.length - 2; i >= 0; i--) {
    let layer = this.layers[i];
    let layerNext = this.layers[i + 1];
    for (var j = 0; j < layer.size; j++) {
        for (var h = 0; h < layerNext.size; h++) {
            layer.ws[h + j * layerNext.size] +=
                layer.activations[j] *
                (layerNext.actFuncPrime(layerNext.z[h]) * 2 * layerNext.costs[h]);

            layer.costs[j] +=
                layer.w[h + j * layerNext.size] *
                (layerNext.actFuncPrime(layerNext.z[h]) * 2 * layerNext.costs[h]);//error here maybe?
        }
    }
    for (h = 0; h < layerNext.size; h++) {
        layerNext.bs[h] +=
            layerNext.actFuncPrime(layerNext.z[h]) * 2 * layerNext.costs[h];
    }
}

要更新权重和偏差,我只需将它们添加到各自负的 grads * 0.001(学习率)。

训练后:
无论我输入什么,它都只输出 ~0.5,损失大约 0.25....

【问题讨论】:

  • 我强烈建议您看一下 Michael Nielsen 的在线书籍《神经网络和深度学习》,第 2 章。它教您计算过程以及如何矩阵化您的计算,这样您就可以避免循环地狱并减少要编写的代码量。使用线性代数库比重新发明轮子更重要。在感知器层反向传播中,您需要使用损失(成本)梯度而不是损失函数本身,因此即使我在您的代码中找不到它也不需要 ^2。
  • @rkuang25 谢谢 :)

标签: javascript machine-learning neural-network


【解决方案1】:

我忘记在每个 epoch 之前重置每个神经元的成本。这使得成本加起来。

我补充说:

layer.costs[j] = 0;

在这一行之前:

layer.costs[j] +=....;

【讨论】:

    猜你喜欢
    • 2018-02-05
    • 2020-01-28
    • 2018-03-11
    • 2018-06-05
    • 2019-01-01
    • 1970-01-01
    • 2020-03-27
    • 2018-03-23
    • 1970-01-01
    相关资源
    最近更新 更多