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

Python 使用Keras和Hyperas进行参数调整,python,machine-learning,keras,neural-network,deep-learning,Python,Machine Learning,Keras,Neural Network,Deep Learning,我一直在使用一个名为Hyperas的Python库,它是一个hyperopt/keras包装器,用于调整keras模型中的参数。我的问题是关于Hyperas的输出 我已经阅读了文档和源代码,但似乎无法理解输出的含义或解释方式。完成优化后,它将打印以下行: {'batch_size': 3, 'optimizer': 1, 'l2': 0.7446290506725413, 'output_dim': 3, 'output_dim_1': 0, 'l2_1': 0.1209021912095098

我一直在使用一个名为Hyperas的Python库,它是一个hyperopt/keras包装器,用于调整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)}})))
由于此
选项
未命名-
hyperas
正在扫描函数定义并查找参数名称。由于未命名-它为先前命名的参数赋值,该参数为
output\u 1
。要跳过该步骤,请尝试:

model.add(LSTM(units={{choice([32,64,128,256,512])}},...)
对辍学率也做类似的事情:

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

令人惊叹的。例如,对于输出({'batch_size':3,'optimizer':1}),数字是否表示每个参数的最佳参数的索引?例如,对于批量大小,我将使用第3位的大小?右:)该值是最佳参数的索引。我发现
hyperas
无法标记某些参数。例如,我从
x=Bidirectional(LSTM(50,return_sequences=True,dropout={uniform(0,0.2)}中得到
{dropout:0.19540010347590975,'dropout_1':0.1673333369423164}