Skip to content

Commit

Permalink
Merge pull request #39 from pazner/master
Browse files Browse the repository at this point in the history
Fix parallel execution bug (issue #38)
  • Loading branch information
baggepinnen authored Oct 18, 2020
2 parents 5b0930f + 1ec42c8 commit 92c0515
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/Hyperopt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Lazy.@forward Hyperoptimizer.history Base.length, Base.getindex

function Base.iterate(ho::Hyperoptimizer, state=1)
state > ho.iterations && return nothing
samples = ho.sampler(ho)
samples = ho.sampler(ho, state)
push!(ho.history, samples)
[state;samples], state+1
end
Expand Down Expand Up @@ -103,6 +103,7 @@ function pmacrobody(ex, params, candidates, sampler_)
function workaround_function()
ho = Hyperoptimizer(iterations = $(esc(candidates[1])), params = $(esc(params[2:end])), candidates = $(Expr(:tuple, esc.(candidates[2:end])...)), sampler=$(esc(sampler_)))
ho.sampler isa GPSampler && error("We currently do not support running the GPSampler in parallel. If this is an issue, open an issue ;)")
init!(ho.sampler, ho)
res = pmap(1:ho.iterations) do i
$(Expr(:tuple, esc.(params)...)),_ = iterate(ho,i)
res = $(esc(ex.args[2])) # ex.args[2] = Body of the For loop
Expand Down
15 changes: 8 additions & 7 deletions src/samplers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
Sample a value For each parameter uniformly at random from the candidate vectors. Log-uniform sampling available by providing a log-spaced candidate vector.
"""
struct RandomSampler <: Sampler end
function (s::RandomSampler)(ho)

function init!(::RandomSampler, ho) end

function (s::RandomSampler)(ho, iter)
[list[rand(1:length(list))] for list in ho.candidates]
end

Expand All @@ -26,9 +29,8 @@ function init!(s::LHSampler, ho)
s.samples = copy(X')
end

function (s::LHSampler)(ho)
function (s::LHSampler)(ho, iter)
init!(s, ho)
iter = length(ho.history)+1
[list[s.samples[dim,iter]] for (dim,list) in enumerate(ho.candidates)]
end

Expand All @@ -51,9 +53,8 @@ function init!(s::CLHSampler, ho)
s.samples = copy(X')
end

function (s::CLHSampler)(ho)
function (s::CLHSampler)(ho, iter)
init!(s, ho)
iter = length(ho.history)+1
[list[s.samples[dim,iter]] for (dim,list) in enumerate(ho.candidates)]
end

Expand Down Expand Up @@ -120,7 +121,7 @@ function init!(s::GPSampler, ho)
end


function (s::GPSampler)(ho)
function (s::GPSampler)(ho, iter)
init!(s, ho)
iter = length(ho.history)+1
if iter <= 3
Expand Down Expand Up @@ -226,7 +227,7 @@ end
function successive_halving(hb, ho, costfun, n, r=1, s=round(Int, log(hb.η, n)))
η = hb.η
minimum = Inf
T = [ hb.inner(ho) for i=1:n ]
T = [ hb.inner(ho, i) for i=1:n ]
# append!(ho.history, T)
Juno.progress() do id
for i in 0:s
Expand Down

0 comments on commit 92c0515

Please sign in to comment.