hyperopt中文文档:Interfacing-With-Other-Languages(在其他语言中使用hyperopt)
其他語言接口
Font Tian translated this article on 23 December 2017
兩種接口策略
基本上有兩種方法將 hyperopt 與其他語言進(jìn)行連接:
包裝對非Python代碼的調(diào)用
使用hyperopt優(yōu)化非python函數(shù)的參數(shù)(例如外部可執(zhí)行文件)的最簡單方法是在外部可執(zhí)行文件周圍編寫一個Python函數(shù)包裝器。假設(shè)你有一個可執(zhí)行文件 foo 需要一個整數(shù)的命令行參數(shù) --n 并打印出一個分?jǐn)?shù),你可以像這樣包裝它:
import subprocessdef foo_wrapper(n):# Optional: write out a script for the external executable# (we just call foo with the argument proposed by hyperopt)proc = subprocess.Popen(['foo', '--n', n], stdout=subprocess.PIPE)proc_out, proc_err = proc.communicate()# <you might have to do some more elaborate parsing of foo's output here>score = float(proc_out)return score當(dāng)然,要優(yōu)化 n 參數(shù)給 foo 你也需要調(diào)用 hyperopt.fmin ,并且定義搜索空間。我覺得你會想在Python中做這個部分。
from hyperopt import fmin, hp, randombest_n = fmin(foo_wrapper, hp.quniform('n', 1, 100, 1), algo=random.suggest)print best_n當(dāng)這里的搜索空間大于簡單的搜索空間時,您可能需要或者必須包裝函數(shù)來將其參數(shù)轉(zhuǎn)換為外部可執(zhí)行文件的某種 配置文件/腳本。
這種方法與MongoTrials完全兼容。
直接與MongoDB進(jìn)行通信
通過直接與MongoDB進(jìn)行通信,可以更直接地與搜索過程(使用 MongoTrials 時)進(jìn)行交互,就像 hyperopt-mongo-worker 一樣。該內(nèi)容已經(jīng)超過了本教程的范圍,但Hannes Schultz(@ Contemporaryer)的hyperopt與他的MDBQ項目可以作為有不錯的參考,這是一個獨(dú)立的基于MongoDB的任務(wù)隊列:
https://github.com/temporaer/MDBQ/blob/master/src/example/hyperopt_client.cpp
查看代碼以及 hyperopt / mongoexp.py 的內(nèi)容,了解工作進(jìn)程如何在工作隊列中保留作業(yè),并將結(jié)果存儲回MongoDB。
總結(jié)
以上是生活随笔為你收集整理的hyperopt中文文档:Interfacing-With-Other-Languages(在其他语言中使用hyperopt)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hyperopt中文文档:Install
- 下一篇: hyperopt中文文档:Paralle