0

I want to use Keras tuner to tune model hyperparameter using the following code that first creates the class to make the optimization as following

    model = keras.Sequential()
    model.add(layers.Conv2D(filter=hp.Int('conv_1_filter',min_value=32,max_value=128,step=16),
                                                  kernel_size=hp.Choice('conv_1_kernel',values = [3,5]),
                                                  activation='relu',
                                                  input_shape=(28,28,1)
                                                  ))
    model.add(layers.Conv2D(filter=hp.Int('conv_2_filter',min_value=32,max_value=128,step=16),
                                                  kernel_size=hp.Choice('conv_2_kernel',values = [3,5]),
                                                  activation='relu'))
    model.add(layers.Flatten())
    model.add(layers.Dense(
                                  units=hp.Int('dense_1_units',min_value=32,max_value=128, step=16),
                                  activation='relu'
                              ))
    model.add(layers.Dense(10,activation='softmax'))
    model.compile(
        optimizer=keras.optimizers.Adam(
            hp.Choice('learning_rate',
                      values=[1e-2, 1e-3])),
        loss='sparse_categorical_crossentropy',
        metrics=['accuracy'])
    return model

from kerastuner import RandomSearch
from tensorflow.keras import layers
from kerastuner.engine.hyperparameters import HyperParameters

tuner_search=RandomSearch(build_model,
                          objective='val_accuracy',
                          max_trials=5,
                          executions_per_trial=3,
                          directory='output',project_name='MNIST') 

** i run the class , but when i try to use any tuners such as random search, hyperband,etc. I got the following error **

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hypermodel.py", line 105, in build
    model = self.hypermodel.build(hp)
  File "<ipython-input-37-8db271052e01>", line 6, in build_model
    input_shape=(28,28,1)
TypeError: __init__() missing 1 required positional argument: 'filters'
[Warning] Invalid model 0/5
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hypermodel.py", line 105, in build
    model = self.hypermodel.build(hp)
  File "<ipython-input-37-8db271052e01>", line 6, in build_model
    input_shape=(28,28,1)
TypeError: __init__() missing 1 required positional argument: 'filters'
[Warning] Invalid model 1/5
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hypermodel.py", line 105, in build
    model = self.hypermodel.build(hp)
  File "<ipython-input-37-8db271052e01>", line 6, in build_model
    input_shape=(28,28,1)
TypeError: __init__() missing 1 required positional argument: 'filters'
[Warning] Invalid model 2/5
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hypermodel.py", line 105, in build
    model = self.hypermodel.build(hp)
  File "<ipython-input-37-8db271052e01>", line 6, in build_model
    input_shape=(28,28,1)
TypeError: __init__() missing 1 required positional argument: 'filters'
[Warning] Invalid model 3/5
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hypermodel.py", line 105, in build
    model = self.hypermodel.build(hp)
  File "<ipython-input-37-8db271052e01>", line 6, in build_model
    input_shape=(28,28,1)
TypeError: __init__() missing 1 required positional argument: 'filters'
[Warning] Invalid model 4/5
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hypermodel.py", line 105, in build
    model = self.hypermodel.build(hp)
  File "<ipython-input-37-8db271052e01>", line 6, in build_model
    input_shape=(28,28,1)
TypeError: __init__() missing 1 required positional argument: 'filters'
[Warning] Invalid model 5/5
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hypermodel.py in build(self, hp)
    104                 with maybe_distribute(self.distribution_strategy):
--> 105                     model = self.hypermodel.build(hp)
    106             except:

8 frames
TypeError: __init__() missing 1 required positional argument: 'filters'

During handling of the above exception, another exception occurred:

RuntimeError                              Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/kerastuner/engine/hypermodel.py in build(self, hp)
    113                 if i == self._max_fail_streak:
    114                     raise RuntimeError(
--> 115                         'Too many failed attempts to build model.')
    116                 continue
    117 

RuntimeError: Too many failed attempts to build model.

** Can anyone plese help me to solve this. **

Adi Yadav
  • 1
  • 1
  • 1
    please post more of the code. what is ```build_model```? – ewokx Jul 16 '20 at 06:35
  • 1
    assuming the code above the imports builds the build_model, you're missing the ```filters``` parameter in your ```conv2D``` calls. – ewokx Jul 16 '20 at 06:37

1 Answers1

1

So you have done most of the things correct, you have just made typo in your code.

You have to use filters and you have used filter.

Just to make it more clear it should be in the following way.

model.add(layers.Conv2D(filters=hp.Int('conv_1_filter',min_value=32,max_value=128,step=16),
                                                  kernel_size=hp.Choice('conv_1_kernel',values = [3,5]),
                                                  activation='relu',
                                                  input_shape=(28,28,1)
                                                  ))

Changing that should resolve the issue. I hope your issue gets resolved.

pratsbhatt
  • 1,498
  • 10
  • 20