fmin_ncg中不支持的操作数类型错误
我有一个成本函数,我想用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
这句话对你来说似乎很重要。
File "bootstrap.py", line 97, in <module>
main(rat, cluster_parameter.main)
这意味着错误不是出现在你定义的函数里,而是在你的主程序中,当你尝试获取参数的时候。请展示一下主程序和你是怎么调用这个程序的。看起来你传入的参数有问题。这里可以考虑使用 argparse(如果你用的是 Python 2.6.7,可以用 optparse)来帮助你设置参数。