New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Interop between DiffSharp and MathNet and FSharp Stats For optimization Problems #382
Comments
Hi @AndyAbok thanks for sharing this idea with some example code. Really helpful to understand how this type of scenario can work. I used the code you shared and the following revision of it works for me: [<AutoOpen>]
module Converter =
let vectorToTensor (x:Vector<float>) = x.AsArray() |> dsharp.tensor
let tensorToVector (x:Tensor) = x.toArray1D() |> vector
let tensorToScalar (x:Tensor) = x |> float
let rosenbrock (xs: Tensor) =
let x, y = xs.[0], xs.[1]
pown (1.0 - x) 2 + 100.0 * pown (y - pown x 2) 2
// Vector<float> -> float
let rosenbrockV = vectorToTensor >> rosenbrock >> tensorToScalar
// Vector<float> -> Vector<float>
let rosenbrockGradV = vectorToTensor >> dsharp.grad rosenbrock >> tensorToVector
let LBFGS f grad initialVal =
let objectiveFunction =
new System.Func<Vector<float>,float>(f)
let gradientFunction =
new System.Func<Vector<float>,Vector<float>>(grad)
let obj = ObjectiveFunction.Gradient(objectiveFunction,gradientFunction)
let solver = LimitedMemoryBfgsMinimizer(1e-5, 1e-5, 1e-5, 5, 1000)
let result = solver.FindMinimum(obj,initialVal)
result.MinimizingPoint
|> Vector.toSeq
let initialVal = [|0.0;0.0|] |> vector
printfn "%A" initialVal
let res = LBFGS rosenbrockV rosenbrockGradV initialVal
printfn "%A" res this seems to work and print
The So what seems to be needed is a lightweight conversion API as implemented by the (Extra note: this was a quick experiment and there are more efficient ways of getting the function's output and gradient. What is implemented above runs the |
@gbaydin Thanks for taking time to look at it. I was testing this and it seems not to work on my end I get |
Hi @AndyAbok it seems like your example was using a package version that is slightly older than the latest. If you use
the code above should work. |
if diffsharp could support interop between mathnet mostly for the the optimization module so as to take advantage of automatic differentiation capability for optimization problems.
I think this would apply to Fsharp stats as well, as it also does use vectors in such kind of problems.
The text was updated successfully, but these errors were encountered: