Skip to content
This repository was archived by the owner on Feb 28, 2024. It is now read-only.
This repository was archived by the owner on Feb 28, 2024. It is now read-only.

Using Integer search space dimension with Tensorflow #790

@marian-lambert

Description

@marian-lambert

Hello,

consider the following minimal example, where I am optimizing one hyperparameter of a dummy neural network:

space = [Integer(2, 4, name='kernel_size')]

@use_named_args(space)
def objective_function(kernel_size):
    model = keras.Sequential()
    model.add(Convolution1D(filters=64, kernel_size=kernel_size))
    model.compile(optimizer=Adam(), loss='binary_crossentropy',
                  metrics=['acc'])
    return random.randint(0, 100)

res = gp_minimize(objective_function, space, n_calls=10, verbose=True)

Running this with scikit-optimize==0.5.2, I get the following error:

Traceback (most recent call last):
  File ".\venv\lib\site-packages\keras\utils\conv_utils.py", line 33, in normalize_tuple
    value_tuple = tuple(value)
TypeError: 'numpy.int32' object is not iterable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./recreate.py", line 22, in <module>
    res = gp_minimize(objective_function, space, n_calls=10, verbose=True)
  File ".\venv\lib\site-packages\skopt\optimizer\gp.py", line 228, in gp_minimize
    callback=callback, n_jobs=n_jobs)
  File ".\venv\lib\site-packages\skopt\optimizer\base.py", line 248, in base_minimize
    next_y = func(next_x)
  File ".\venv\lib\site-packages\skopt\utils.py", line 636, in wrapper
    objective_value = func(**arg_dict)
  File "./recreate.py", line 16, in objective_function
    model.add(Convolution1D(filters=64, kernel_size=first_convolution_layer_kernel_size))
  File ".\venv\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File ".\venv\lib\site-packages\keras\layers\convolutional.py", line 353, in __init__
    **kwargs)
  File ".\venv\lib\site-packages\keras\layers\convolutional.py", line 109, in __init__
    'kernel_size')
  File ".\venv\lib\site-packages\keras\utils\conv_utils.py", line 36, in normalize_tuple
    str(n) + ' integers. Received: ' + str(value))
ValueError: The `kernel_size` argument must be a tuple of 1 integers. Received: 3

This is due to this line value_tuple = tuple(value) in Keras which fails for numpy.int32 types.
One can resolve this by using model.add(Convolution1D(filters=64, kernel_size=kernel_size.item())) instead of model.add(Convolution1D(filters=64, kernel_size=kernel_size)). However this is not a clean solution as it restricts reusing the objective function with other types.

Is there a proper way to convert the numpy.int32 parameter to a native python type?

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