Param调整与Keras和Hyperas

时间:2018-02-05 17:20:36

标签: python machine-learning keras deep-learning

我一直在使用一个名为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)

1 个答案:

答案 0 :(得分:1)

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

 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])}},...)

用辍学率做类似的事情:

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