5

gradient descent係数の計算について学習しています。以下は私がやっていることです:

#!/usr/bin/Python

 import numpy as np


   # m denotes the number of examples here, not the number of features
 def gradientDescent(x, y, theta, alpha, m, numIterations):
     xTrans = x.transpose()
     for i in range(0, numIterations):
        hypothesis = np.dot(x, theta)
        loss = hypothesis - y
        # avg cost per example (the 2 in 2*m doesn't really matter here.
        # But to be consistent with the gradient, I include it)
        cost = np.sum(loss ** 2) / (2 * m)
        #print("Iteration %d | Cost: %f" % (i, cost))
        # avg gradient per example
        gradient = np.dot(xTrans, loss) / m
        # update
        theta = theta - alpha * gradient
     return theta

 X = np.array([41.9,43.4,43.9,44.5,47.3,47.5,47.9,50.2,52.8,53.2,56.7,57.0,63.5,65.3,71.1,77.0,77.8])
 y = np.array([251.3,251.3,248.3,267.5,273.0,276.5,270.3,274.9,285.0,290.0,297.0,302.5,304.5,309.3,321.7,330.7,349.0])
 n = np.max(X.shape)
 x = np.vstack([np.ones(n), X]).T      
 m, n = np.shape(x)
 numIterations= 100000
 alpha = 0.0005
 theta = np.ones(n)
 theta = gradientDescent(x, y, theta, alpha, m, numIterations)
 print(theta)

今、私の上記のコードは正常に動作します。複数の変数を試して、次のように置き換えXた場合X1:

  X1 = np.array([[41.9,43.4,43.9,44.5,47.3,47.5,47.9,50.2,52.8,53.2,56.7,57.0,63.5,65.3,71.1,77.0,77.8], [29.1,29.3,29.5,29.7,29.9,30.3,30.5,30.7,30.8,30.9,31.5,31.7,31.9,32.0,32.1,32.5,32.9]])

私のコードは失敗し、次のエラーが表示されます。

  JustTestingSGD.py:14: RuntimeWarning: overflow encountered in square
  cost = np.sum(loss ** 2) / (2 * m)
  JustTestingSGD.py:19: RuntimeWarning: invalid value encountered in subtract
  theta = theta - alpha * gradient
  [ nan  nan  nan]

gradient descentを使用する方法を教えてもらえますX1か? を使用して期待される出力X1は次のとおりです。

[-153.5 1.24 12.08]

私は他の Python 実装にもオープンです。coefficients (also called thetas)forX1とが欲しいだけですy

4

1 に答える 1

3

問題は、アルゴリズムが収束していないことです。代わりに発散します。最初のエラー:

JustTestingSGD.py:14: RuntimeWarning: overflow encountered in square
cost = np.sum(loss ** 2) / (2 * m)

これは、64 ビット浮動小数点数が数値を保持できない (つまり、> 10^309) ため、ある時点で何かの 2 乗を計算することが不可能であるという問題に由来します。

JustTestingSGD.py:19: RuntimeWarning: invalid value encountered in subtract
theta = theta - alpha * gradient

これは、以前のエラーの結果にすぎません。数値は計算上妥当ではありません。

デバッグ印刷行のコメントを外すと、実際に相違を確認できます。収束がないため、コストが増加し始めます。

X1alpha の値を小さくして関数を試すと、収束します。

于 2014-06-25T20:10:54.143 に答える