2

我正在尝试使用 hyperas 库对这个 keras 模型进行超参数优化,我以前从未这样做过,所以我基本上遵循了这里的分步完整示例,但我收到了提到的错误。提前致谢。

model = Sequential()

model.add(Conv2D({{choice(32, 64, 128, 256, 512, 1024)}}, 3, 3, border_mode='same',
                 input_shape=input_shape, activation={{choice('relu', 'sigmoid', 'softmax', 'tanh')}}))
model.add(Conv2D({{choice(32, 64, 128, 256, 512, 1024)}}, 3, 3, border_mode='same',
                 activation={{choice('relu', 'sigmoid', 'softmax', 'tanh')}}))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D({{choice(32, 64, 128, 256, 512, 1024)}}, 3, 3, border_mode='same',
                 input_shape=input_shape, activation={{choice('relu', 'sigmoid', 'softmax', 'tanh')}}))
model.add(Conv2D({{choice(32, 64, 128, 256, 512, 1024)}}, 3, 3, border_mode='same',
                 activation={{choice('relu', 'sigmoid', 'softmax', 'tanh')}}))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D({{choice(32, 64, 128, 256, 512, 1024)}}, 3, 3, border_mode='same',
                 input_shape=input_shape, activation={{choice('relu', 'sigmoid', 'softmax', 'tanh')}}))
model.add(Conv2D({{choice(32, 64, 128, 256, 512, 1024)}}, 3, 3, border_mode='same',
                 activation={{choice('relu', 'sigmoid', 'softmax', 'tanh')}}))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D({{choice(32, 64, 128, 256, 512, 1024)}}, 3, 3, border_mode='same',
                 input_shape=input_shape, activation={{choice('relu', 'sigmoid', 'softmax', 'tanh')}}))
model.add(Conv2D({{choice(32, 64, 128, 256, 512, 1024)}}, 3, 3, border_mode='same',
                 activation={{choice('relu', 'sigmoid', 'softmax', 'tanh')}}))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())
model.add(Dense({{choice(32, 64, 128, 256, 512, 1024)}},
                activation={{choice('relu', 'sigmoid', 'softmax', 'tanh')}}))
model.add(Dropout({{uniform(0, 0.75)}}))

model.add(Dense({{choice(32, 64, 128, 256, 512, 1024)}},
                activation={{choice('relu', 'sigmoid', 'softmax', 'tanh')}}))
model.add(Dropout({{uniform(0, 0.75)}}))

model.add(Dense(1))
model.add(Activation({{choice('relu', 'sigmoid', 'softmax', 'tanh')}}))

model.compile(loss='binary_crossentropy',
              optimizer={{choice(RMSprop, Adam, SGD)}},
              metrics=['accuracy'])
"/home/bjorn/PycharmProjects/Test/HyperoptModel.py", line 113, in
<module>
    trials=Trials())   File "/home/bjorn/PycharmProjects/Test/venv/lib/python3.5/site-packages/hyperas/optim.py",
line 69, in minimize
    keep_temp=keep_temp)   File "/home/bjorn/PycharmProjects/Test/venv/lib/python3.5/site-packages/hyperas/optim.py",
line 134, in base_minimizer
    space=get_space(),   File "/home/bjorn/PycharmProjects/Test/temp_model.py", line 149, in
get_space   File
"/home/bjorn/PycharmProjects/Test/venv/lib/python3.5/site-packages/hyperopt/pyll_utils.py",
line 22, in wrapper
    return f(label, *args, **kwargs) TypeError: hp_choice() takes 2 positional arguments but 7 were given ```
4

1 回答 1

3

您需要将选项choice作为 alist而不是多个参数。

改变

choice(32, 64, 128, 256, 512, 1024)

choice([32, 64, 128, 256, 512, 1024])
于 2019-07-11T14:45:36.397 回答