首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在python中为我的keras模型搜索不同的值?

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

Stack Overflow用户
提问于 2019-03-01 03:41:19
回答 1查看 143关注 0票数 0

我已经在keras中实现了LSTM。

因为我使用了以下三个值:

  • embedding_size
  • hidden_layer_size
  • learning_rate

现在我想找到最适合我的模型的值。例如,我可以为每个属性分配3个值(如[embedding_size: [100, 150, 200], hidden_layer_size: [50, 100, 150], learning_rate: [0.015,0.01,0.005]])

我现在想知道的是哪种组合在我的函数中效果最好。我想我可以这样构建我的函数:

代码语言:javascript
复制
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

有人能帮我解决我的问题吗?或者创建一个自定义函数来比较所有的值?

EN

回答 1

Stack Overflow用户

发布于 2019-03-01 04:00:50

使用hyperopt。下面是一个随机森林的例子:

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54933103

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档