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)
Despite the fact that matrices and vectors are mutable, their dimension is fixed and cannot be changed after creation.
http://numerics.mathdotnet.com/Matrix.html
I have a theta which is a 2x1 size vector I'm trying to update theta. [0] and theta [1]. iteratively, but when I look at it after each iteration, it remains [0; 0]. I know that F # is immutable, but I quoted above from my website that Vectors and Matrix are mutable, so I'm not sure why this is not working.
I suspect this has something to do with Shadowing ... because I am declaring let next_theta0 in a for loop, but I'm not sure.
In addition, as a follow-up question. I feel that the way I did it is incredibly horrible. There is literally no reason for me to implement this in F #, when it would be much easier in C # (using this methodology), because it does not feel very "functional". Can anyone suggest ways to endorse this to make it more functional.
source share