Skip to content

Fallback to random sampling does not get triggered #97

@janfrancu

Description

@janfrancu

load_bayes_cache returns an empty dictionary instead of nothing therefore the check in

function bayes_params(space, folder, fallback; add_model_seed=false, kwargs...)
cache = load_bayes_cache(folder)
if cache !== nothing
x0 = [v[:parameters] for v in values(cache)]
y0 = [objective_value(v[:runs], kwargs...) for v in values(cache)]
@info("Loaded cached results from $(folder).")
# conversion to tuples this makes skopt happy
# as it cannot parse list of numpy arrays as valid "2D" input
x0 = [(to_skopt(space, x)..., ) for x in x0]
opt = BayesianHyperOpt(collect(space))
tell(opt, x0, y0)
@info("Fitted GP with $(length(x0)) samples.")
p = from_skopt(space, ask(opt))
# registers only parameters with empty arrays and store cache
register_run!(cache, (;parameters=p); fit_results=false)
save_bayes_cache(folder, cache)
@info("Fetched new hyperparameters: $(p).")
return add_model_seed ? merge((;init_seed=rand(1:Int(1e8))), p) : p
else
@warn("Could not find bayesian cache file at $(folder), calling fallback.")
return fallback()
end
end
will not trigger.
Ideally we would like to provide the function with number of initial points, which will be sampled randomly before running the optimization and that if condition should reflect it.

I have found this when I misspelled a dataset name, which in turn caused the load_bayes_cache to return empty dictionary and all the array comprehensions after that did not error, leading to python error over an empty tuple.

[ Info: Loaded cached results from /home/francja5/Projects/modules/GenerativeAD.jl/data/experiments_bayes/tabular/sptn/arrhytmia.
ERROR: LoadError: PyError ($(Expr(:escape, :(ccall(#= /home/francja5/.julia/packages/PyCall/zqDXB/src/pyfncall.jl:43 =# @pysym(:PyObject_Call), PyPtr, (PyPtr, PyPtr, PyPtr), o, pyargsptr, kw
))))) <class 'ValueError'>
ValueError('cannot reshape array of size 0 into shape (0,newaxis)')
  File "/home/francja5/sklearn-env/lib/python3.8/site-packages/skopt/optimizer/optimizer.py", line 493, in tell
    return self._tell(x, y, fit=fit)
  File "/home/francja5/sklearn-env/lib/python3.8/site-packages/skopt/optimizer/optimizer.py", line 536, in _tell
    est.fit(self.space.transform(self.Xi), self.yi)
  File "/home/francja5/sklearn-env/lib/python3.8/site-packages/skopt/space/space.py", line 966, in transform
    Xt = np.hstack([np.asarray(c).reshape((len(X), -1)) for c in columns])
  File "/home/francja5/sklearn-env/lib/python3.8/site-packages/skopt/space/space.py", line 966, in <listcomp>
    Xt = np.hstack([np.asarray(c).reshape((len(X), -1)) for c in columns])
  
Stacktrace:
 [1] pyerr_check at /home/francja5/.julia/packages/PyCall/zqDXB/src/exception.jl:60 [inlined]
 [2] pyerr_check at /home/francja5/.julia/packages/PyCall/zqDXB/src/exception.jl:64 [inlined]
 [3] _handle_error(::String) at /home/francja5/.julia/packages/PyCall/zqDXB/src/exception.jl:81
 [4] macro expansion at /home/francja5/.julia/packages/PyCall/zqDXB/src/exception.jl:95 [inlined]
 [5] #110 at /home/francja5/.julia/packages/PyCall/zqDXB/src/pyfncall.jl:43 [inlined]
 [6] disable_sigint at ./c.jl:446 [inlined]
 [7] __pycall! at /home/francja5/.julia/packages/PyCall/zqDXB/src/pyfncall.jl:42 [inlined]
 [8] _pycall!(::PyObject, ::PyObject, ::Tuple{Array{AbstractArray,1},Tuple{}}, ::Int64, ::Ptr{Nothing}) at /home/francja5/.julia/packages/PyCall/zqDXB/src/pyfncall.jl:29
 [9] _pycall!(::PyObject, ::PyObject, ::Tuple{Array{AbstractArray,1},Tuple{}}, ::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at /home/francja5/.julia/packages/PyCal
l/zqDXB/src/pyfncall.jl:11
 [10] (::PyObject)(::Array{AbstractArray,1}, ::Vararg{Any,N} where N; kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at /home/francja5/.julia/packages/PyCall/z
qDXB/src/pyfncall.jl:86
 [11] (::PyObject)(::Array{AbstractArray,1}, ::Vararg{Any,N} where N) at /home/francja5/.julia/packages/PyCall/zqDXB/src/pyfncall.jl:86
 [12] tell(::GenerativeAD.BayesianHyperOpt, ::Array{AbstractArray,1}, ::Array{Any,1}) at /home/francja5/Projects/modules/GenerativeAD.jl/src/bayesian_opt.jl:68
 [13] bayes_params(::NamedTuple{(:firstdense, :batchsize, :ncomp, :nlayers, :activation, :unitary, :sharing),NTuple{7,PyObject}}, ::String, ::typeof(sample_params); add_model_seed::Bool, kwa
rgs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at /home/francja5/Projects/modules/GenerativeAD.jl/src/bayesian_opt.jl:181
 [14] top-level scope at /home/francja5/Projects/modules/GenerativeAD.jl/scripts/experiments_tabular/sptn.jl:99
 [15] include(::Function, ::Module, ::String) at ./Base.jl:380
 [16] include(::Module, ::String) at ./Base.jl:368
 [17] exec_options(::Base.JLOptions) at ./client.jl:296
 [18] _start() at ./client.jl:506
in expression starting at /home/francja5/Projects/modules/GenerativeAD.jl/scripts/experiments_tabular/sptn.jl:97

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions