Skip to content
This repository has been archived by the owner on Feb 23, 2023. It is now read-only.

numpy truth value error #96

Closed
himat opened this issue Jun 7, 2017 · 5 comments
Closed

numpy truth value error #96

himat opened this issue Jun 7, 2017 · 5 comments

Comments

@himat
Copy link

himat commented Jun 7, 2017

I followed the example on the gpyopt website, and immediately got an error.

$ python
Python 2.7.10 (default, Feb  6 2017, 23:53:20) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> # --- Load GPyOpt
... from GPyOpt.methods import BayesianOptimization
>>> import numpy as np
>>> 
>>> def f(x): return (6*x-2)**2*np.sin(12*x-4)
...
>>> bounds = [(0,1)]
>>> myBopt = BayesianOptimization(f=f, bounds=bounds)
>>> myBopt.run_optimization(max_iter=15)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/himakee/Library/Python/2.7/lib/python/site-packages/GPyOpt/methods/bayesian_optimization.py", line 458, in run_optimization
    super(BayesianOptimization, self).run_optimization(max_iter = max_iter, max_time = max_time,  eps = eps, verbosity=verbosity, save_models_parameters = save_models_parameters, report_file = report_file, evaluations_file= evaluations_file, models_file=models_file)
  File "/Users/himakee/Library/Python/2.7/lib/python/site-packages/GPyOpt/core/bo.py", line 103, in run_optimization
    self._update_model()
  File "/Users/himakee/Library/Python/2.7/lib/python/site-packages/GPyOpt/core/bo.py", line 198, in _update_model
    self._save_model_parameter_values()
  File "/Users/himakee/Library/Python/2.7/lib/python/site-packages/GPyOpt/core/bo.py", line 209, in _save_model_parameter_values
    if self.model_parameters_iterations == None:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
>>>
@arutaku
Copy link

arutaku commented Jun 13, 2017

@himat I got the same error, and it was because I was using numpy 1.13
It looks like GPyOpt does not get well with the new version of numpy.

Just downgrade your numpy version ;-)

@javiergonzalezh
Copy link
Member

javiergonzalezh commented Jun 13, 2017 via email

@ghost
Copy link

ghost commented Jun 14, 2017

Hi,

There are numerous places in the GPyOpt code where it tests for the presence of an array argument like this:
if X_batch != None:

With the new numpy his will cause the error described above for arrays, and I encountered the same error in acquisitions.LP (see below). The solution is to replace these tests with the following form:
if X_batch is not None:

A similar fix applies to tests of the form "== None"

I hope this helps. It should be fairly simple to find and repair all occurrences. Most of the existing tests already have the correct form.

Traceback (most recent call last):
  File "optimiser.py", line 102, in <module>
    process()
  File "optimiser.py", line 79, in process
    evaluator_type=evaluator_type)
  File "/usr/local/lib/python2.7/site-packages/GPyOpt/methods/bayesian_optimization.py", line 244, in __init__
    self.run_optimization(max_iter=0,verbosity=self.verbosity)
  File "/usr/local/lib/python2.7/site-packages/GPyOpt/methods/bayesian_optimization.py", line 458, in run_optimization
    super(BayesianOptimization, self).run_optimization(max_iter = max_iter, max_time = max_time,  eps = eps, verbosity=verbosity, save_models_parameters = save_models_parameters, report_file = report_file, evaluations_file= evaluations_file, models_file=models_file)
  File "/usr/local/lib/python2.7/site-packages/GPyOpt/core/bo.py", line 108, in run_optimization
    self.suggested_sample = self._compute_next_evaluations()
  File "/usr/local/lib/python2.7/site-packages/GPyOpt/core/bo.py", line 186, in _compute_next_evaluations
    return self.evaluator.compute_batch() 
  File "/usr/local/lib/python2.7/site-packages/GPyOpt/core/evaluators/batch_local_penalization.py", line 45, in compute_batch
    new_sample = self.acquisition.optimize()
  File "/usr/local/lib/python2.7/site-packages/GPyOpt/acquisitions/base.py", line 59, in optimize
    out = self.optimizer.optimize(f=self.acquisition_function, f_df=self.acquisition_function_withGradients)[0]
  File "/usr/local/lib/python2.7/site-packages/GPyOpt/optimization/acquisition_optimizer.py", line 227, in optimize
    pref_f = f(arms)
  File "/usr/local/lib/python2.7/site-packages/GPyOpt/acquisitions/LP.py", line 110, in acquisition_function
    return self._penalized_acquisition(x, self.model, self.X_batch, self.r_x0, self.s_x0)
  File "/usr/local/lib/python2.7/site-packages/GPyOpt/acquisitions/LP.py", line 86, in _penalized_acquisition
    if X_batch!=None:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

@jzpgit
Copy link

jzpgit commented Jun 23, 2017

Hi,
@javiergonzalezh @himat
I suggest to use

def _save_model_parameter_values(self):
    if self.model_parameters_iterations is None:

in GPyOpt/core/bo.py line 209

It this way, the error may go.

Simple test:

a= np.array([1,2,3])
if a==None:
... print('Equal')
...
Traceback (most recent call last):
File "", line 1, in
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
if a is not None:
... print('Equal')
...
Equal

If you tried this method and it works, please let me know. I need to inform our IT team to update the package.

@javiergonzalezh
Copy link
Member

Closing as this issue was fixed in the release 1.2.0.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants