[英]How to pass objects into function which is optimized with hyperopt?
I'm new to hyperopt package. 我是hyperopt包的新手。 Now, I wanna optimize my LDA model which is implemented in gensim.
现在,我想优化在gensim中实现的LDA模型。 The LDA model is optimized to maximize silhouette score over training data.
优化了LDA模型,以最大化训练数据上的轮廓分数。
Now, my question is "How do I pass training-data(numpy.ndarray) to objective-function which is called from hyperopt?" 现在,我的问题是“如何将训练数据(numpy.ndarray)传递给从hyperopt调用的目标函数?” I looked tutorials and some example codes .
我看了教程和一些示例代码 。 They set training-data as global variable.
他们将训练数据设置为全局变量。 But in my situation, it's difficult to set training-data as global variable as they do.
但是在我的情况下,很难像它们那样将训练数据设置为全局变量。
I wrote following code to optimize LDA with hyoeropt. 我编写了以下代码,以使用hyoeropt优化LDA。 I'm stacked with the way to pass training-data to
gensim_objective_function
function because I'm gonna put gensim_lda_optimaze
in system which calls gensim_lda_optimaze
function. 我将传递训练数据到
gensim_objective_function
函数的方式堆叠在一起,因为我gensim_lda_optimaze
放在调用gensim_lda_optimaze
函数的系统中。
How to realize that? 如何实现呢?
# I want to pass training data to this function!
# gensim_lda_tuning_training_corpus, gensim_lda_tuning_num_topic, gensim_lda_tuning_word2id is what I wanna pass
def gensim_objective_function(arg_dict):
from .gensim_lda import evaluate_clustering
from .gensim_lda import call_lda_single
from .gensim_lda import get_topics_ids
alpha = arg_dict['alpha']
eta = arg_dict['eta']
iteration= arg_dict['iteration']
gamma_threshold= arg_dict['gamma_threshold']
minimum_probability= arg_dict['minimum_probability']
passes= arg_dict['passes']
# train LDA model
lda_model, gensim_corpus = call_lda_single(matrix=gensim_lda_tuning_training_corpus,
num_topics=gensim_lda_tuning_num_topic,
word2id_dict=gensim_lda_tuning_word2id,
alpha=alpha, eta=eta,
iteration=iteration,
gamma_threshold=gamma_threshold,
minimum_probability=minimum_probability,
passes=passes)
topic_ids = get_topics_ids(trained_lda_model=lda_model, gensim_corpus=gensim_corpus)
labels = [t[0] for t in topic_ids]
# get silhouette score with extracted label
evaluation_score = evaluate_clustering(feature_matrix=gensim_lda_tuning_training_corpus, labels=numpy.array(labels))
return -1 * evaluation_score
def gensim_lda_optimaze(feature_matrix, num_topics, word2id_dict):
assert isinstance(feature_matrix, (ndarray, csr_matrix))
assert isinstance(num_topics, int)
assert isinstance(word2id_dict, dict)
parameter_space = {
'alpha': hp.loguniform("alpha", numpy.log(0.1), numpy.log(1)),
'eta': hp.loguniform("eta", numpy.log(0.1), numpy.log(1)),
'iteration': 100,
'gamma_threshold': 0.001,
'minimum_probability': 0.01,
'passes': 10
}
trials = Trials()
best = fmin(
gensim_objective_function,
parameter_space,
algo=tpe.suggest,
max_evals=100,
trials=trials
)
return best
You can always use partial
in python. 您始终可以在python中使用
partial
。
from functools import partial
def foo(params, data):
return params, data
goo = partial(foo, data=[1,2,3])
print goo('ala')
gives 给
ala [1, 2, 3]
In other words, you make a proxy function, which has data loaded as a given parameter and you ask hyperopt to optimize this new function, with data already set. 换句话说,您将创建一个代理功能,该代理功能已将数据作为给定参数加载,并要求hyperopt使用已设置的数据来优化此新功能。
thus in your case you change gensim_objective_function to be something accepting all your params: 因此,在您的情况下,您将gensim_objective_function更改为接受所有参数的内容:
def RAW_gensim_objective_function(arg_dict, gensim_lda_tuning_training_corpus,
gensim_lda_tuning_num_topic,
gensim_lda_tuning_word2id):
and create actual function to optimize by passing your data in different part of code 并创建实际功能以通过在代码的不同部分传递数据来进行优化
gensim_objective_function = partial(RAW_gensim_objective_function,
gensim_lda_tuning_training_corpus = YOUR_CORPUS,
gensim_lda_tuning_num_topic = YOUR_NUM_TOPICS,
gensim_lda_tuning_word2id = YOUR_IDs)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.