首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >RuntimeError:太多的尝试都失败了

RuntimeError:太多的尝试都失败了
EN

Stack Overflow用户
提问于 2020-07-16 06:29:44
回答 1查看 2.3K关注 0票数 0

我想使用Keras来调优模型的超参数,使用下面的代码,这些代码首先创建类来进行优化,如下面的所示

代码语言:javascript
运行
复制
    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') 

**我运行这个类,但是当我尝试使用任意搜索、超带等调谐器时,我得到了以下错误**

代码语言:javascript
运行
复制
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.

**有人能帮我解决这个问题吗?**

EN

回答 1

Stack Overflow用户

发布于 2020-07-16 18:00:38

所以您已经完成了大多数正确的事情,您只是在您的代码中输入了错误。

您必须使用filters,也可以使用filter

为了更清楚地说明,它应该以以下方式进行。

代码语言:javascript
运行
复制
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)
                                                  ))

改变这一点应该能解决这个问题。我希望你的问题能解决。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62928797

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档