Skip to content

Commit

Permalink
Add example in R (#1025)
Browse files Browse the repository at this point in the history
  • Loading branch information
teytaud authored Jan 28, 2021
1 parent 3670721 commit 87a083f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Learn more about parametrization in the :ref:`Parametrization section <parametri
optimizers_ref.rst
parametrization_ref.rst
benchmarking.rst
r.rst
benchmarks.rst
pyomo.rst
windows.md
Expand Down
77 changes: 77 additions & 0 deletions docs/r.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
.. _r:

Examples - Nevergrad for R
==========================


.. code-block:: R
# Library and environment for Reticulate/Nevergrad.
library("reticulate")
conda_create("r-reticulate")
conda_install("r-reticulate", "nevergrad", pip=TRUE)
use_condaenv("r-reticulate")
# Only if you use parallelism.
library(doParallel)
# Choose your optimization method below.
optimizer_name <- "NGOpt"
# optimizer_name <- "DoubleFastGADiscreteOnePlusOne"
# optimizer_name <- "OnePlusOne"
# optimizer_name <- "DE"
# optimizer_name <- "RandomSearch"
# optimizer_name <- "TwoPointsDE"
# optimizer_name <- "Powell"
# optimizer_name <- "MetaModel" CRASH !!!!
# optimizer_name <- "SQP"
# optimizer_name <- "Cobyla"
# optimizer_name <- "NaiveTBPSA"
# optimizer_name <- "DiscreteOnePlusOne"
# optimizer_name <- "cGA"
# optimizer_name <- "ScrHammersleySearch"
# Now we can play with Nevergrad as usual.
# We assume here that we have 17 continuous hyperparameters with values in [0, 1].
# We can do other instrumentations, as discussed below.
my_tuple <- tuple(17)
instrumentation <- ng$p$Array(shape=my_tuple)
instrumentation$set_bounds(0., 1.)
num_workers <- 3 # We want to be able to evaluate 3 hyperparametrizations simultaneously.
num_iterations <- 100 * num_workers # Let us say we have a budget of 100xnum_workers hyperparameters to evaluate.
# Let us create a Nevergrad optimization method.
optimizer <- ng$optimizers$registry[optimizer_name](instrumentation, budget=num_iterations, num_workers=num_workers)
# Dummy initializations.
nevergrad_hp <- 0
nevergrad_hp_val <- 0
score <- 0
for (i in 1:num_iterations) {
for (j in 1:num_workers) {
nevergrad_hp[j] <- optimizer$ask()
nevergrad_hp_val[j] <- nevergrad_hp[j]$value
}
# Sequential version.
# for (j in 1:num_workers) { # In a perfect world this would be parallel.
# score[j] <- norm(nevergrad_hp_val[j]
# }
# Parallel version.
# Actually this could be asynchronous, Nevergrad is ok for that, you do not have to
# do the tell's in the same order as the ask's.
registerDoParallel(cores=num_workers)
getDoParWorkers()
foreach(i=1:num_workers) %dopar% score[j] <- norm(nevergrad_hp_val[j])
for (j in 1:num_workers) {
optimizer$tell(nevergrad_hp[j], score[j])
}
}
print(optimizer$recommend()$value)
Don't forget the "pip=TRUE". I wasted so much time because of this :-)

For other instrumentations (discrete variables, logarithmic continuous variables...), please check `Different instrumentations: <https://github.com/facebookresearch/nevergrad/blob/master/docs/parametrization.rst>`. Or for simple examples for machine learning `machine learning <https://github.com/facebookresearch/nevergrad/blob/master/docs/machinelearning.rst>`.

0 comments on commit 87a083f

Please sign in to comment.