python - 运行时错误 : Too many failed attempts to build model. keras 调谐器

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

我想使用 keras 调谐器来调整模型超参数 使用首先创建类的以下代码进行如下优化

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.

谁能告诉我怎么解决

最佳答案

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

关于python - 运行时错误 : Too many failed attempts to build model. keras 调谐器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61549141/

相关文章:

python - 取消 Python 脚本时做一些事情

python - 尝试使用经度和纬度获取距离,但一直运行到错误 : 'Series' object has no attribute 'radians'

python - 如何设置 Plone 在注销后重定向到 came_from

python - Flask Cors 不工作

python - 将数据帧保存到多个保留数据帧名称的 CSV

python - 无法在 virtualenv 中使用 pip 安装软件包

python - Tkinter 文本小部件。换行符破坏单词

python - 十六进制文件头,魔数(Magic Number),python

python - 以与 Python 2.7 和 Python 3.5 兼容的方式使用 abc.ABCMeta

python - SDL: "working natively in C++"是什么意思?