繁体   English   中英

为什么我的自定义神经网络实现的成本停留在 0.25?

[英]Why is my custom neural network implementation stuck at 0.25 cost?

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

单个训练样例的 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....

伙计们,我感染了新冠病毒,它激发了我的大脑,我修复了这个错误。 我忘记在每个时期之前重置每个神经元的成本。 这使得成本加起来。 我非常希望这对其他人有帮助。 我添加了 layer.costs[j] = 0; 行前:layer.costs[j] +=....;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM