STOR-i / GaussianProcesses.jl

A Julia package for Gaussian Processes
https://stor-i.github.io/GaussianProcesses.jl/latest/
Other
308 stars 53 forks source link

Using keyword arguments in optimize! #50

Closed SamuelWiqvist closed 7 years ago

SamuelWiqvist commented 7 years ago

Hi all,

I have a problem with passing keyword arguments from optimize! to the function optimize of the Optim.jl package.

When I try to set the number of iterations for the optimization (i.e. setting the keyword argument iterations) I get:

kwarg_problem

What is the proper way to use keyword arguments in optimize!?

Information about my system: juliaversioninfo

Version of GaussianProcesses.jl: gpversion

maximerischard commented 7 years ago

Ah yes, I've bumped into this. The Optim syntax changed a little bit. What seems to be needed now is:

optimize(func,init,method, Optim.Options(;kwargs...))

instead of

optimize(func,init; method=method, kwargs...)

Would you be interested in putting together a pull request? The source file that needs to be updated is src/optimize.jl

SamuelWiqvist commented 7 years ago

Thanks for the fast answer!

It didn't work to change the syntax to the Optim syntax. However I instead changed the optimize! function to use a Optim.Options object instead of storing the optimization settings in kwargs. I now have following code om my local computer:

@doc """
# Description
A function for optimising the GP hyperparameters based on type II maximum likelihood estimation. This function performs gradient based optimisation using the Optim pacakge to which the user is referred to for further details.

# Arguments:
* `gp::GP`: Predefined Gaussian process type
* `noise::Bool`: Noise hyperparameters should be optmized
* `mean::Bool`: Mean function hyperparameters should be optmized
* `kern::Bool`: Kernel function hyperparameters should be optmized
* `options`: Optim.Options object containing the keyword arguments for the optimize function from the Optim package

# Return:
* `::Optim.MultivariateOptimizationResults{Float64,1}`: optimization results object
""" ->
function optimize!(gp::GP; noise::Bool=true, mean::Bool=true, kern::Bool=true,
                    method=ConjugateGradient(), options::Optim.Options{Void} = Optim.Options())
    func = get_optim_target(gp, noise=noise, mean=mean, kern=kern)
    init = get_params(gp;  noise=noise, mean=mean, kern=kern) # Initial hyperparameter values
    results=optimize(func,init, method, options) # Run optimizer
    set_params!(gp, Optim.minimizer(results), noise=noise,mean=mean,kern=kern)
    update_mll!(gp)
    return results
end

And if I now run

res = optimize!(gp, options = Optim.Options(iterations = 10))`

I get

res

I will put together a pull request during the next few days.

chris-nemeth commented 7 years ago

Thanks for this. I've merged the PR.