Mutable Vector field is not updating in F#

删除回忆录丶 提交于 2019-12-13 07:37:19

问题


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)

Even though matrices and vectors are mutable, their dimension is fixed and cannot be changed after creation.

http://numerics.mathdotnet.com/Matrix.html

I have theta which is a vector of size 2x1 I'm trying to update theta.[0] and theta.[1] iteratively but when I look at it after each iteration it remains to be [0;0]. I know that F# is immutable but I quoted above from their website that Vectors and Matrix are mutable so I'm not sure why this isn't working.

My hunch is that it has something to do with Shadowing... because I'm declaring a let next_theta0 in a for loop but I'm not too sure

Also, as a follow up question. I feel like the way I implemented this is incredibly terrible. There's literally no reason for me to have implemented this in F# when it would have been much easier in C# (using this methodology) because it doesn't feel very "functional" Could anyone suggest ways to approve on this to make it more functional.


回答1:


The destructive update operator in F# is written <-, so:

theta.[0] <- next_theta0

What you're doing in your code is comparing theta.[0] with next_theta0, which is an operation that results in a bool, which is why you had to add an ignore call after it, to avoid the compiler warning.

Here's a good general rule: when you see a compiler warning, don't just try tricks to appease the compiler. Instead, try to understand why the warning appears. Chances are, it points to a legitimate problem.

And here's a more F#-specific rule: using ignore is a code smell. ignore is a sort of hack, mostly used for interaction with external code, when the external code returns something, but doesn't really expect the consumer to use that return value.



来源:https://stackoverflow.com/questions/35816215/mutable-vector-field-is-not-updating-in-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!