RuntimeError: слишком много неудачных попыток построить модель. керас тюнер - PullRequest
0 голосов

Я хочу использовать keras tuner для настройки гиперпараметра модели, используя следующий код, который сначала создает класс для оптимизации следующим образом:

class RegressionHyperModel (HyperModel):    
 def __init__(self, input_shape): 
      self.input_shape = input_shape 
 def build(self, hp):
     model = Sequential()
     model.add(
     layers.Dense(
     units=hp.Int('units', 8, 64, 4, default=8),
     activation=hp.Choice(
     'dense_activation',
     values=['relu', 'tanh', 'sigmoid'],
     default='relu'),
     input_shape=input_shape
     )
     )

     model.add(
     layers.Dense(
     units=hp.Int('units', 16, 64, 4, default=16),
     activation=hp.Choice(
     'dense_activation',
     values=['relu', 'tanh', 'sigmoid'],
     default='relu')
     )
     )

     model.add(
     layers.Dropout(
     hp.Float(
     'dropout',
     min_value=0.0,
     max_value=0.1,
     default=0.005,
     step=0.01)
     )
     )

     model.add(layers.Dense(1))

     model.compile(
     optimizer='rmsprop',loss='mse',metrics=['mse']
     )
     return model


hp.Int('units',8,64,4,default=8)
hp.Choice('dense_activation', values=['relu', 'tanh', 'sigmoid'], default='relu')

#Let’s instantiate a hypermodel object. Input shape varies per dataset and the problem you are trying to solve.

input_shape = (x_train.shape[1],)
hypermodel = RegressionHyperModel(input_shape)


#tunnig random search

tuner_rs = RandomSearch(
 hypermodel,
 objective='mse',
 seed=42,
 max_trials=10,
 executions_per_trial=2)

#Run the random search tuner using the search method.

tuner_rs.search(x_train_scaled, y_train, epochs=10, validation_split=0.2, verbose=0)
#Select the best combination of hyperparameters the tuner had tried and evaluate.

best_model = tuner_rs.get_best_models(num_models=1)[0]
loss, mse = best_model.evaluate(x_test_scaled, y_test)

я запускаю класс, но когда я пытаюсь использовать любой тюнеры, такие как случайный поиск, гиперполоса и т. д. c. Я получил следующую ошибку

Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\kerastuner\engine\hypermodel.py", line 105, in build
    model = self.hypermodel.build(hp)
  File "<ipython-input-93-d8d5d966c04f>", line 13, in build
    input_shape=input_shape
  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\sequential.py", line 133, in add
    'Found: ' + str(layer))
TypeError: The added layer must be an instance of class Layer. Found: <tensorflow.python.keras.layers.core.Dense object at 0x0000015D19051B38>
<IPython.core.display.HTML object>
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\kerastuner\engine\hypermodel.py", line 105, in build
    model = self.hypermodel.build(hp)
  File "<ipython-input-93-d8d5d966c04f>", line 13, in build
    input_shape=input_shape
  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\sequential.py", line 133, in add
    'Found: ' + str(layer))
TypeError: The added layer must be an instance of class Layer. Found: <tensorflow.python.keras.layers.core.Dense object at 0x0000015D196B0908>
<IPython.core.display.HTML object>
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\kerastuner\engine\hypermodel.py", line 105, in build
    model = self.hypermodel.build(hp)
  File "<ipython-input-93-d8d5d966c04f>", line 13, in build
    input_shape=input_shape
  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\sequential.py", line 133, in add
    'Found: ' + str(layer))
TypeError: The added layer must be an instance of class Layer. Found: <tensorflow.python.keras.layers.core.Dense object at 0x0000015D19668B38>
<IPython.core.display.HTML object>
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\kerastuner\engine\hypermodel.py", line 105, in build
    model = self.hypermodel.build(hp)
  File "<ipython-input-93-d8d5d966c04f>", line 13, in build
    input_shape=input_shape
  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\sequential.py", line 133, in add
    'Found: ' + str(layer))
TypeError: The added layer must be an instance of class Layer. Found: <tensorflow.python.keras.layers.core.Dense object at 0x0000015D19681940>
<IPython.core.display.HTML object>
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\kerastuner\engine\hypermodel.py", line 105, in build
    model = self.hypermodel.build(hp)
  File "<ipython-input-93-d8d5d966c04f>", line 13, in build
    input_shape=input_shape
  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\sequential.py", line 133, in add
    'Found: ' + str(layer))
TypeError: The added layer must be an instance of class Layer. Found: <tensorflow.python.keras.layers.core.Dense object at 0x0000015D1109E908>
<IPython.core.display.HTML object>
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\kerastuner\engine\hypermodel.py", line 105, in build
    model = self.hypermodel.build(hp)
  File "<ipython-input-93-d8d5d966c04f>", line 13, in build
    input_shape=input_shape
  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\sequential.py", line 133, in add
    'Found: ' + str(layer))
TypeError: The added layer must be an instance of class Layer. Found: <tensorflow.python.keras.layers.core.Dense object at 0x0000015D10E86AC8>
<IPython.core.display.HTML object>
Traceback (most recent call last):

  File "<ipython-input-100-d0d2704255f3>", line 7, in <module>
    project_name='helloworld')

  File "C:\ProgramData\Anaconda3\lib\site-packages\kerastuner\tuners\randomsearch.py", line 175, in __init__
    **kwargs)

  File "C:\ProgramData\Anaconda3\lib\site-packages\kerastuner\engine\multi_execution_tuner.py", line 58, in __init__
    oracle, hypermodel, **kwargs)

  File "C:\ProgramData\Anaconda3\lib\site-packages\kerastuner\engine\tuner.py", line 103, in __init__
    overwrite=overwrite)

  File "C:\ProgramData\Anaconda3\lib\site-packages\kerastuner\engine\base_tuner.py", line 91, in __init__
    self._populate_initial_space()

  File "C:\ProgramData\Anaconda3\lib\site-packages\kerastuner\engine\base_tuner.py", line 106, in _populate_initial_space
    self.hypermodel.build(hp)

  File "C:\ProgramData\Anaconda3\lib\site-packages\kerastuner\engine\hypermodel.py", line 65, in _build_wrapper
    return self._build(hp, *args, **kwargs)

  File "C:\ProgramData\Anaconda3\lib\site-packages\kerastuner\engine\hypermodel.py", line 115, in build
    'Too many failed attempts to build model.')

RuntimeError: Too many failed attempts to build model.

Пожалуйста, кто-нибудь может сказать мне, как ее решить

1 Ответ

0 голосов
/ 01 мая 2020

В соответствии с документами Keras https://keras.io/getting-started/sequential-model-guide вы не должны ставить layers. перед типом слоя. Так, например, Dense() не layers.Dense().

...