首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Keras和Hyperas进行参数调整

使用Keras和Hyperas进行参数调整
EN

Stack Overflow用户
提问于 2018-02-06 01:20:36
回答 1查看 2.2K关注 0票数 1

我一直在使用一个名为Hyperas的Python库,它是一个用于调优Keras模型中参数的hyperopt/keras包装器。我的问题是关于Hyperas的输出。

我已经阅读了文档和源代码,但似乎无法理解输出的含义或如何解释。在完成优化后,它会打印以下行:

代码语言:javascript
复制
{'batch_size': 3, 'optimizer': 1, 'l2': 0.7446290506725413, 'output_dim': 3, 'output_dim_1': 0, 'l2_1': 0.12090219120950985}

为什么在我的代码中只有一个output_dim参数,但是output_dim有两个字典值呢?我该如何解释所有的elese?

代码语言:javascript
复制
def model(X_train, X_test, y_train, y_test, max_features, maxlen, class_weight_dict):
        model = Sequential()
        model.add(Embedding(max_features, output_dim = {{choice([32,64,128,256,512])}}, input_length=maxlen))
        model.add(LSTM({{choice([32,64,128,256,512])}},W_regularizer=l2({{uniform(0, 1)}})))
        model.add(Dropout({{uniform(0, 1)}}))
        model.add(Dense(138))
        model.add(Activation('softmax'))

        model.compile(loss='categorical_crossentropy',
                      optimizer={{choice(['rmsprop', 'adam', 'sgd'])}},
                      metrics=['accuracy'])

        early_stopping = EarlyStopping(monitor='val_loss', patience=4)
        checkpointer = ModelCheckpoint(filepath='keras_weights.hdf5',
                                       verbose=1,
                                       save_best_only=True)

        model.fit(X_train, y_train,
                  batch_size={{choice([32,16,64,128,256,512])}},
                  validation_data=(X_test, y_test),
                  nb_epoch=100,
                  class_weight=class_weight_dict,
                  callbacks=[early_stopping, checkpointer])

        score, acc = model.evaluate(X_test, y_test)
        print('Test score:', score)
        print('Test accuracy:', acc)
        return {'loss': -acc, 'status': STATUS_OK, 'model': model}

    if __name__ == '__main__':
        best_run, best_model = optim.minimize(model=model,
                                              data=data,
                                              algo=tpe.suggest,
                                              max_evals=10,
                                              trials=Trials())
        print(best_run)
        print(best_model)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-06 04:37:36

这是因为你的参数没有命名,让我们看一下这一行:

代码语言:javascript
复制
 model.add(LSTM({{choice([32,64,128,256,512])}},W_regularizer=l2({{uniform(0, 1)}})))

由于此choice未命名- hyperas正在扫描函数定义并查找参数名称。因为它没有命名-它指定了之前命名的参数的值,即output_1。为了跳过这个尝试:

代码语言:javascript
复制
model.add(LSTM(units={{choice([32,64,128,256,512])}},...)

对dropout rate做类似的事情:

代码语言:javascript
复制
model.add(Dropout(rate=..))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48628041

复制
相关文章

相似问题

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