【发布时间】:2017-07-07 06:40:42
【问题描述】:
我需要限制我的损失,以便预测总是积极的。 所以我有:
x = [1.0,0.64,0.36,0.3,0.2]
y = [1.0,0.5,0.4,-0.1,-0.2]
alpha = 0
def loss(w, x, y, alpha):
loss = 0.0
for y_i,x_i in zip(y,x):
loss += ((y_i - np.dot(w,x_i)) ** 2)
return loss + alpha * math.sqrt(np.dot(w,w))
res = minimize(loss_new_scipy, 0.0, args=(x, y, alpha))
现在我想添加约束,但我发现大多数约束 x 在边界之间,而不是 np.dot(w,x)>= 0
这样的约束是什么样子的?
编辑: 我想在 scipy.optimize.minimize 函数中使用约束参数,所以我认为它应该看起来像这样:
def con(w,x):
loss = 0.0
for i_x in x:
loss += (np.dot(w, i_x))
return loss
cons = ({'type': 'ineq', 'fun': con})
res = minimize(loss_new_scipy, 0.0, args=(x, y, alpha), constraints=cons)
为了简单起见,我还删除了第二个约束
编辑2: 我将问题更改为以下内容:约束是 w*x 必须大于 1,并且还将目标更改为所有负数。我还更改了参数,所以它现在运行:
x = np.array([1.0,0.64,0.36,0.3,0.2])
y = [-1.0,-0.5,-0.4,-0.1,-0.2]
alpha = 0
def con(w,x,y,alpha):
print np.array(w*x)
return np.array((w*x)-1).sum()
cons = ({'type': 'ineq', 'fun': con,'args':(x,y,alpha)})
def loss_new_scipy(w, x, y, alpha):
loss = 0.0
for y_i,x_i in zip(y,x):
loss += ((y_i - np.dot(w,x_i)) ** 2)
return loss + alpha * math.sqrt(np.dot(w,w))
res = minimize(loss_new_scipy, np.array([1.0]), args=(x, y, alpha),constraints=cons)
print res
但不幸的是,w 的结果是 2.0,这确实是正数,看起来约束有所帮助,因为它距离将函数拟合到目标还很远,但预测 w*x 并非都高于 1.0
编辑3: 我刚刚意识到我的预测之和 - 1 现在等于 0,但我希望每个预测都大于 1.0 所以 w = 2.0,
w*x = [ 2.00000001 1.28000001 0.72 0.6 0.4 ]
和
(w*x) - 1 = [ 1.00000001 0.28000001 -0.28 -0.4 -0.6 ]
总和等于 0.0,但我希望所有预测 w*x 大于 1.0,因此 w*x 中的所有 5 个值至少应为 1.0
【问题讨论】:
-
为了可能使这个问题变得更有帮助(即,我发现它与答案一起很有用)可能是删除编辑部分并以更简洁的版本重新编写 - 否则,它是尽管这是一个好问题,但有点难以理解
标签: python optimization scipy constraints