let gradientDescent (X : Matrix<double>) (y :Vector<double>) (theta : Vector<double>) alpha (num_iters : int) =
let J_history = Vector<double>.Build.Dense(num_iters)
let m = y.Count |> double
theta.At(0, 0.0)
let x = (X.Column(0).PointwiseMultiply(X*theta-y)) |> Vector.sum
for i in 0 .. (num_iters-1) do
let next_theta0 = theta.[0] - (alpha/m) * ((X.Column(0).PointwiseMultiply(X*theta-y)) |> Vector.sum)
let next_theta1 = theta.[1] - (alpha/m) * ((X.Column(1).PointwiseMultiply(X*theta-y)) |> Vector.sum)
theta.[0] = next_theta0 |> ignore
theta.[1] = next_theta1 |> ignore
J_history.[i] = computeCost X y theta |> ignore
()
(theta, J_history)
即使矩陣和向量是可變的,它們的尺寸是固定的 和創建之後不能改變。可變的矢量場中沒有F#更新
我有THETA其尺寸2×1的矢量 我試圖更新有峯。[0]和[1]迭代地,但是當我在每次迭代之後看它時,它仍然是[0; 0]。我知道F#是不可變的,但我在他們的網站上面引用了Vector和Matrix是可變的,所以我不知道爲什麼這不起作用。
我的直覺是,它是與陰影......因爲我在for循環中,宣佈了一個讓next_theta0但我也不太清楚
此外,作爲一個跟進的問題。我覺得我實施這個方式非常可怕。我真的沒有理由在F#中實現這一點,因爲它在C#中會更容易(使用這種方法),因爲它沒有什麼「功能」。任何人都可以建議通過這種方式來使它更加實用。
啊,是的,我已經讀過,完全忘記了它。我想知道爲什麼它會返回一個布爾....謝謝! –