使用Keras和Hyperas进行参数调优

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

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

{'batch_size': 3, 'optimizer': 1, 'l2': 0.7446290506725413, 'output_dim': 3, 'output_dim_1': 0, 'l2_1': 0.12090219120950985}

为什么会有两个output_dim的字典值,尽管我的代码中只有一个output_dim参数?我应该如何解释其他所有内容?

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)

回答:

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

 model.add(LSTM({{choice([32,64,128,256,512])}},W_regularizer=l2({{uniform(0, 1)}})))

因为这个choice没有命名,hyperas会扫描函数定义并寻找参数名称。由于它没有被命名,它会分配之前命名的参数的值,即output_1。为了避免这种情况,请尝试:

model.add(LSTM(units={{choice([32,64,128,256,512])}},...)

对dropout率也做类似的处理:

model.add(Dropout(rate=..))

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注