簡體   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