gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 10:06:47 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com