fmin_ncg不支持的操作数TypeError

时间:2014-03-06 17:58:19

标签: python optimization scipy

所以我有一个成本函数,我试图用scipy.optimize中的fmin_ncg来最小化。如果我只运行一次它似乎工作正常,但我还编写了一个重新采样数据的引导脚本,并重复最小化计算置信区间的成本函数。在引导脚本的几次迭代之后,我得到以下错误。它的信息量不大。有谁知道它可能来自哪里?

Traceback (most recent call last):
  File "bootstrap.py", line 97, in <module>
    main(rat, cluster_parameter.main)
  File "bootstrap.py", line 83, in main
    t, i = estimator(resampled_data, RAT_CLUSTERS[rat])
  File "/home/matthew/Dropbox/Work/vocalization_analysis/cluster_parameter.py", line 176, in main
    args=(included_clusters,jumps), avextol=1e-5)
  File "/usr/lib/python2.7/dist-packages/scipy/optimize/optimize.py", line 1252, in fmin_ncg
    callback=callback, **opts)
  File "/usr/lib/python2.7/dist-packages/scipy/optimize/optimize.py", line 1365, in _minimize_newtoncg
    update = alphak * pk
TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'

我不确定发布此问题的相关代码。这是成本函数

def cost(x, *args):
theta = x[0]
b = x[1]
included_clusters = args[0]
jumps = args[1]

h = [(n,SLOPES[n](theta)) for n in included_clusters]
h = OrderedDict(h)
bisecting_slopes = find_bisector(h)
vertices = polygon_vertices(bisecting_slopes, included_clusters)
cluster = poly_cluster(jumps[:,0:2], vertices)
cost = 0 
for c in h.keys(): 
    idx = find(cluster==c)
    l = len(jumps[idx,0:2])
    print(l)
    for j in jumps[idx,0:2]:
        x = (j[0] / h[c] + j[1] - b) / (h[c] + 1 / h[c])
        y = h[c] * x + b 
        cost += (((j[0] - x)**2 + (j[1]-y)**2)) / l 

#print('b='+str(b))
#print('theta='+str(theta))
#print('cost='+str(cost))
return cost

以及它的衍生

def dcost(x, *args):
theta = x[0]
b = x[1]
included_clusters = args[0]
jumps = args[1]

h = [(n, SLOPES[n](theta)) for n in included_clusters]
h = OrderedDict(h)
dh = [(n, DSLOPES[n](theta)) for n in included_clusters]
dh = OrderedDict(dh)
bisecting_slopes= find_bisector(h)
vertices = polygon_vertices(bisecting_slopes, included_clusters)
cluster = poly_cluster(jumps[:,0:2], vertices)

dcost_theta = 0
dcost_b = 0
for c in h.keys(): 
    idx = find(cluster==c)
    l = len(jumps[idx,0:2])
    for j in jumps[idx,0:2]:
        x = (j[0] / h[c] + j[1] - b) / (h[c] + 1 / h[c])
        y = h[c] * x + b 
        dx_theta = ((h[c]**2 + 1) * (j[1] - b) * dh[c] - 
            2 * (j[0] + (j[1] - b) * h[c]) * h[c] * dh[c]) / (h[c]**2 + 1)**2
        dy_theta = ((h[c]**2 + 1) * (j[0] * dh[c] + 2 * (j[1] - b) * h[c] * dh[c]) 
            - 2 * (j[0] * h[c] + (j[1] - b) * h[c]**2) * h[c] * dh[c]) / (h[c]**2 + 1)**2
        dx_b = -h[c] / (h[c]**2 + 1)
        dy_b = -h[c]**2 / (h[c]**2 + 1)

        dcost_theta += (2 * (j[0] - x) * dx_theta 
            + 2 * ((j[1] - b) - y) * dy_theta) / l

        dcost_b += (2 * (j[0] - x) * dx_b 
            + 2 * ((j[1] - b) - y) * dy_b) / l

return np.array([dcost_theta, dcost_b])

这也是我的主要功能。它只是调用fmin_ncg。

def main(jumps, included_clusters = range(NUM_CLUSTERS)):
theta_min, b_min = fmin_ncg(f=cost, x0=[0,0], fprime=dcost,
 args=(jumps, included_clusters), avextol=1e-5, disp=0)

return theta_min, b_min

如果您想查看该计划的其余部分,可以在此处https://github.com/mdornfe1/vocalization_analysis/blob/error_analysis/cluster_parameter.py和此处https://github.com/mdornfe1/vocalization_analysis/blob/error_analysis/bootstrap.py

1 个答案:

答案 0 :(得分:0)

就你而言,这似乎是一个重要的陈述。

File "bootstrap.py", line 97, in <module>
    main(rat, cluster_parameter.main)

这意味着您在定义的函数中出现 错误,但是当您尝试获取参数时,错误发生在您的main中。请显示主要内容以及如何调用该程序。看起来好像你有无效的参数。 argparse(或者如果你有Python 2.6.7则是optparse)在这里作为设置的一部分可能很有用。