如何在python中为我的keras模型网格搜索不同的值?

时间:2019-02-28 19:41:19

标签: python python-3.x keras lstm

我已经在喀拉拉邦实施了LSTM。

我正在使用以下三个值:

  • emdding_size
  • hidden_​​layer_size
  • learning_rate

我现在想找到最适合我的模型的值。例如,我有3个值可以分配给每个属性(例如Sub PartNumber() Dim xCell As Range For Each xCell In Range("firstDigit") If xCell Like WorksheetFunction.Rept("[a-zA-Z]", Len(xCell)) Then xCell.Offset(0, 2) = "Alpha Only" Else xCell.Offset(0, 2) = "Contains non Alpha Char" xCell.Offset(0, 2).Interior.Color = vbYellow 'To color the cell that has string End If Next xCell End Sub

我现在想知道的是哪种组合最能发挥我的功能。我以为可以建立这样的功能:

[embedding_size: [100, 150, 200], hidden_layer_size: [50, 100, 150], learning_rate: [0.015,0.01,0.005]]

得分最高的是最好的

我知道scikit learning为此提供了功能,但我不知道如何将其与自定义功能一起使用(如果可能的话)。这是我发现的来源:https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.GridSearchCV.html

有人可以帮助我如何使用库解决问题或创建自定义函数以比较所有值吗?

1 个答案:

答案 0 :(得分:1)

使用hyperopt。这是随机森林的示例:

from sklearn.ensemble import RandomForestClassifier

from hyperopt import fmin, tpe, hp, STATUS_OK, Trials
from sklearn.model_selection import cross_val_score
from sklearn.metrics import accuracy_score,precision_score,confusion_matrix,f1_score,recall_score

def accuracy(params):
    clf = RandomForestClassifier(**params)
    clf.fit(x_train,y_train)
    return clf.score(x_test, y_test)


parameters = {
    'max_depth': hp.choice('max_depth', range(80,120)),
    'max_features': hp.choice('max_features', range(30,x_train.shape[1])),
    'n_estimators': hp.choice('n_estimators', range(30,100)),
    "max_leaf_nodes":hp.choice("max_leaf_nodes",range(2,8)),
    "min_samples_leaf":hp.choice("min_samples_leaf",range(1,30)),
    "min_samples_split":hp.choice("min_samples_split",range(2,100)),
    'criterion': hp.choice('criterion', ["gini", "entropy"])}


best = 0
def f(params):
    global best
    acc = accuracy(params)
    if acc > best:
        best = acc
    print ('Improving:', best, params)
    return {'loss': -acc, 'status': STATUS_OK}

trials = Trials()

best = fmin(f, parameters, algo=tpe.suggest, max_evals=100, trials=trials)
print ('best:',best)