简体   繁体   English

使用 Keras 和 Hyperas 进行参数调整

[英]Param Tuning with Keras and Hyperas

I have been working with a Python library called Hyperas which is a hyperopt/keras wrapper for tuning parameters in a Keras model.我一直在使用一个名为 Hyperas 的 Python 库,它是一个 hyperopt/keras 包装器,用于在 Keras 模型中调整参数。 My question is about the output from Hyperas.我的问题是关于 Hyperas 的输出。

I have read the documentation and source code, but cannot seem to figure out what the output means or how to interpret.我已阅读文档和源代码,但似乎无法弄清楚输出的含义或如何解释。 It prints the following line after it is done optimizing:它在完成优化后打印以下行:

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

Why are there two dictionary values for output_dim, even though there is only one output_dim param in my code?为什么 output_dim 有两个字典值,即使我的代码中只有一个 output_dim 参数? How do I interpret everything elese?我如何解释所有其他内容?

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)

So it's because you have your parameters not named, let's have a look at this line:所以这是因为你的参数没有命名,让我们看看这一行:

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

as this choice is unnamed - hyperas is scanning function definition and is looking for the parameter name.由于此choice未命名 - hyperas正在扫描函数定义并查找参数名称。 As it's not named - it assigns the value of the previously named parameter which is output_1 .由于它没有命名 - 它分配了先前命名的参数的值,即output_1 In order to skip that try:为了跳过那个尝试:

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

And do a similar thing with dropout rate:并用辍学率做类似的事情:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM