【问题标题】:RuntimeError: Too many failed attempts to build model. keras tunerRuntimeError:构建模型的尝试失败次数过多。 keras 调谐器
【发布时间】:2020-08-16 08:03:47
【问题描述】:

我想使用 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)

我运行课程,但是当我尝试使用任何调谐器时,例如随机搜索、超频带等。我收到以下错误

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.

谁能告诉我怎么解决

【问题讨论】:

    标签: python python-3.x python-2.7 optimization keras


    【解决方案1】:

    根据 Keras 文档 https://keras.io/getting-started/sequential-model-guide 你不应该把layers. 放在图层类型的前面。因此,例如,Dense() 不是 layers.Dense()

    【讨论】:

    • 我查看了您的链接。我没有看到它说不要将layers 放在图层类型的前面。您能否更准确地了解链接的位置?
    • 我同意我的回答现在没有意义。自从我发表评论以来,我认为文档中发生了很多变化。 OP 问题的原因似乎仍然是 Keras 层(预期)和 Tensorflow Keras 层(提供)之间的不匹配。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-25
    • 2012-02-20
    相关资源
    最近更新 更多