我已经在keras中实现了LSTM。
因为我使用了以下三个值:
现在我想找到最适合我的模型的值。例如,我可以为每个属性分配3个值(如[embedding_size: [100, 150, 200], hidden_layer_size: [50, 100, 150], learning_rate: [0.015,0.01,0.005]]
)
我现在想知道的是哪种组合在我的函数中效果最好。我想我可以这样构建我的函数:
def lstm(embedding_size, hidden_layer_size, learning_rate):
return score
得分最高的人拥有最好的价值。
我知道scikit learn提供了这方面的功能,但我不知道如何在自定义功能中使用它们(如果可能的话)。这是我找到的源码:https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.GridSearchCV.html
有人能帮我解决我的问题吗?或者创建一个自定义函数来比较所有的值?
发布于 2019-03-01 04:00:50
使用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)
https://stackoverflow.com/questions/54933103
复制相似问题