2017-03-27 17 views
0

私はプロジェクトのJavaScript Neural Networkをコーディングしていました。以下のソースコードをすべて見ることができます。しかし、コードは無限ループを投げつけ続けます。私は "最適化"プロパティで私のループに何か問題があることを発見するために私のプログラムをデバッグしました。正直言って、私はループに何も間違いを見ないJavascript Neural Netの無限ループ?

このようなループがスローされないように修正するにはどうすればよいですか?

また、ニューラルネットワークを数回繰り返した後、何らかの理由で「NaN」がスローされます。誰かがこれを修正して理由を説明してもらえますか?

EDIT(私の恐ろしいMLプラクティスのすべてがしかし、彼らは無限ループに貢献するなら、私に思い出させてください気にしないでください。):無限ループの問題が修正されます。私はNaNを返すので、微分関数を改善する人が必要です

ありがとうございました。ここにソースコードがあります:

// I will make a single layer feed-forward Javascript neural network 

var x = [1, 2, 3, 4, 5, 6, 7, 8, 9]; 
var y = [5.2, 7.6, 2.3, 4.5, 3, 9, 10, 10.1, 10.2]; 
// Pretty much, I'll be doing my computations on a toy dataset 

//Time to create a neural network object with three main components: predict, error, and optimize 
// "predict" pretty much fits the dataset to the equation y = mx + b and then tries placing a new value of x in the equation to predict a value 
// "error" returns how much "wrong" the neural network is in making predictions 
// "optimize" conducts gradient descent to fix the error at hand 

// Initializing some base values for m and b to be soon improved. 
var m = 1; 
var b = 2; 

function derivative(f) { 
    var h = 0.001; 
    return function(x) { return (f(x + h) - f(x - h))/(2 * h); }; 
} // Imagine going on a mountain and trying to see the shortest number of steps to get down. This is what the derivative here will do to do umerical optimization 

var neural = { 
    predict: function(m, x, b) { 
    return m * x + b; // pretty much write out the equation. time to now define the error 
    }, 

    error: function(y, m, x, b) { 
    var error = 0; 
    for (i=0;i < x.length;i++) { 
     var pred = neural.predict(m, x[i], b); // Checking again for the equation results to see how wrong they are 
     zed = y[i] - pred; 
     error += zed; 
    return error; // Pretty much this computes the difference between the predict amount and the actual amount befire 
    } 

    }, 

    optimize: function(iter, rate, m, b, x, y) { // In this step, we compute the do a "gradient descent" by subtracting the derivative 
    for (i=0;i<iter;i++){ // We have training iterations, since a few times can't work 
    console.log(neural.error(y, m, x, b).toString()); 
     m = m - derivative(neural.error(y, m, x, b)) * rate; 
     b = b - derivative(neural.error(y, m, x, b)) * rate; // Don't make the learning rate too high/low pl0x 
    } 
    } 

}; 

var net = neural.optimize(10, 0.01, m, b, x, y); 
+1

'return error;は* for *ループ内であることを意味しますか? –

+0

まあ、違いはありません –

答えて

1

問題は、カウンタiがグローバルであることです。あなたの関数をインライン化した場合、彼らは次のようになりたい:

for (i = 0; i < 10; i++) { 
    for (i = 0; i < 10; i++) { 
    } 
} 

を、私はあなたが問題を参照してくださいと思います。

使用for (var i...ではなくfor (i...


もう一つの問題は、derivative機能、ない数を返すということです。

ラインm = m - derivative(neural.error(y, m, x, b)) * rate;

より次のようになります。

m = m - function(){...} * rate;

これは、常にNaNmになります。

derivativeは、関数ではない値を返します。


私の以前のコメントで述べたように、私はreturn errorerror()のループのためにすべきだと思います。

+0

真。私は微分関数の助けが必要です –