How can I grid search different values for my keras model in python?

自闭症网瘾萝莉.ら 提交于 2019-12-25 02:15:33

问题


I've implemented a LSTM in keras.

In that I am using the following three values:

  • embedding_size
  • hidden_layer_size
  • learning_rate

I want now to find the values which fit best into my model. So for example I have 3 values I can assign to each property (like [embedding_size: [100, 150, 200], hidden_layer_size: [50, 100, 150], learning_rate: [0.015,0.01,0.005]])

What I would love now to know is which combination works best in my function. I thought I can build my function like this:

def lstm(embedding_size, hidden_layer_size, learning_rate):
    return score

and the highest score has the best values.

I know scikit learn offers function for this but I do not know how to use them with a custom function (if it is even possible). This is a source I found: https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.GridSearchCV.html

Can someone help me how I could use a library for my problem or a create a custom function to compare all values?


回答1:


Use hyperopt. Here's an example, for Random Forests:

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)


来源:https://stackoverflow.com/questions/54933103/how-can-i-grid-search-different-values-for-my-keras-model-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!