使用dask进行参数搜索

时间:2017-07-08 21:50:40

标签: python parallel-processing dask hyperparameters dask-distributed

如何使用Dask优化搜索参数空间? (没有交叉验证)

这是代码(这里没有DASK):

def build(ntries,param,niter,func,score,train,test):
    res=[]
    for i in range(ntries):
        cparam=param.rvs(size=niter,random_state=i)
        res.append( func(cparam, train, test, score) )
    return res

def score(test,correct):
    return np.linalg.norm(test-correct)

def compute_optimal(res):
    from operator import itemgetter
    _sorted=sorted(res,None,itemgetter(1))
    return _sorted

def func(c,train,test,score):
    dt=1.0/len(c)
    for cc in c:
        train=train - cc*dt
    return (c,score(train,test))

以下是我如何使用它:

from dask import delayed
from distributed import LocalCluster, Client
cluster=LocalCluster(n_workers=4, threads_per_worker=1)
cli=Client(cluster)

from scipy.stats import uniform
import numpy as np

niter=500
loc=1.0e-09
scale=1.0
ntries=1000
sched=uniform(loc=loc,scale=scale)
train=np.arange(1000)+0.5
test=np.arange(1000)

# HERE IS THE DASK
graph=build(ntries,sched,niter,delayed(func),score,train,test)

# THE QUESTION SECTION
# I do these steps to bring back all the values so that I could search for the score-wise optimal pair: (parameter, score)
res=[cli.compute(g) for g in graph]
results=[r.result() for r in res]
# Actual search for the optimal pair
optimal=compute_optimal(results)
best,worst=optimal[0],optimal[-1]

问题是:

  1. 我在这里正确使用Dask吗?
  2. 我是否正确地将数据提取回客户端?有更有效的方法吗?
  3. 有没有办法在工人身上寻找最佳配对?
  4. P.S。最近我发布了相关问题,但有不同的问题(thread.lock during custom parameter search class using Dask distributed)。我已经解决了它,并将很快发布一个答案,并将关闭该问题。

0 个答案:

没有答案