python - ECOS 求解器在 CVXPY 1.0 版本中停止工作
问题描述
我正在使用 cvxpy 0.4 版本,在这个版本中,我编写了组套索惩罚线性模型,如下所示:
from cvxpy import *
from sklearn.datasets import load_boston
import numpy as np
boston = load_boston()
x = boston.data
y = boston.target
index = np.array([1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5])
lambda_val = 1
0.4版本
n = x.shape[0]
lambda_param = Parameter(sign="positive")
index = np.append(0, index)
x = np.c_[np.ones(n), x]
group_sizes = []
beta_var = []
unique_index = np.unique(index)
for idx in unique_index:
group_sizes.append(len(np.where(index == idx)[0]))
beta_var.append(Variable(len(np.where(index == idx)[0])))
num_groups = len(group_sizes)
group_lasso_penalization = 0
model_prediction = x[:, np.where(index == unique_index[0])[0]] * beta_var[0]
for i in range(1, num_groups):
model_prediction += x[:, np.where(index == unique_index[i])[0]] * beta_var[i]
group_lasso_penalization += sqrt(group_sizes[i]) * norm(beta_var[i], 2)
lm_penalization = (1.0 / n) * sum_squares(y - model_prediction)
objective = Minimize(lm_penalization + (lambda_param * group_lasso_penalization))
problem = Problem(objective)
lambda_param.value = lambda_val
problem.solve(solver=ECOS)
beta_sol = [b.value for b in beta_var]
1.0版本
n = x.shape[0]
lambda_param = Parameter(nonneg=True)
index = np.append(0, index)
x = np.c_[np.ones(n), x]
group_sizes = []
beta_var = []
unique_index = np.unique(index)
for idx in unique_index:
group_sizes.append(len(np.where(index == idx)[0]))
beta_var.append(Variable(shape=(len(np.where(index == idx)[0]), 1)))
num_groups = len(group_sizes)
model_prediction = 0
group_lasso_penalization = 0
model_prediction = x[:, np.where(index == unique_index[0])[0]] * beta_var[0]
for i in range(1, num_groups):
model_prediction += x[:, np.where(index == unique_index[i])[0]] * beta_var[i]
group_lasso_penalization += sqrt(group_sizes[i]) * norm(beta_var[i], 2)
lm_penalization = (1.0 / n) * sum_squares(y.reshape((n, 1)) - model_prediction)
objective = Minimize(lm_penalization + (lambda_param * group_lasso_penalization))
problem = Problem(objective)
lambda_param.value = lambda_val
problem.solve(solver=ECOS)
beta_sol = [b.value for b in beta_var]
使用 1.0 代码版本时,它显示以下错误消息:
所以,我认为我已经将代码从 0.4 版正确迁移到 1.0 版,但是在 0.4 版中使用 ECOS 求解器解决了一个问题,在 1.0 版中显示错误消息。我在这里做错了吗?以防万一,我在 Windows 机器上的 miniconda python 2.7 中运行此代码。
解决方案
beta_var
看来您的代码与s不一致。在 0.4 示例中它们是一维的,但在 1.0 示例中它们是二维的。这是一个简单的程序,显示 cvxpy 是一致的:
import cvxpy
print(cvxpy.__version__)
from cvxpy import *
try:
x = Variable(shape=(3, 3))
except TypeError:
x = Variable(3, 3)
obj = Minimize(norm(x, 2))
Problem(obj).solve(solver=ECOS)
0.4 的输出:
0.4.11
Traceback (most recent call last):
File "./cvxtest.py", line 11, in <module>
Problem(obj).solve(solver=ECOS)
File "lib/python3.6/site-packages/cvxpy/problems/problem.py", line 209, in solve
return self._solve(*args, **kwargs)
File "lib/python3.6/site-packages/cvxpy/problems/problem.py", line 321, in _solve
solver.validate_solver(constraints)
File "lib/python3.6/site-packages/cvxpy/problems/solvers/solver.py", line 131, in validate_solver
self._reject_problem("it cannot solve semidefinite problems")
File "lib/python3.6/site-packages/cvxpy/problems/solvers/solver.py", line 156, in _reject_problem
raise SolverError(message)
cvxpy.error.SolverError: The solver ECOS cannot solve the problem because it cannot solve semidefinite problems.
1.0 的输出:
1.0.8
Traceback (most recent call last):
File "./cvxtest.py", line 11, in <module>
Problem(obj).solve(solver=ECOS)
File "lib/python3.6/site-packages/cvxpy/problems/problem.py", line 247, in solve
return solve_func(self, *args, **kwargs)
File "lib/python3.6/site-packages/cvxpy/problems/problem.py", line 357, in _solve
raise e
File "lib/python3.6/site-packages/cvxpy/problems/problem.py", line 355, in _solve
solver=solver)
File "lib/python3.6/site-packages/cvxpy/reductions/solvers/solving_chain.py", line 143, in construct_solving_chain
", ".join([cone.__name__ for cone in cones])))
cvxpy.error.SolverError: Either candidate conic solvers (['ECOS']) do not support the cones output by the problem (PSD), or there are not enough constraints in the problem.
总之,您应该修复您的 1.0 代码以使您Variable
的 s 一维。
推荐阅读
- java - Splash-Screen 在 IDE 中工作,但不能作为 .jar - ANT 有问题吗?
- r - 如何在r中保持具有相同符号的值
- redis - Redis georadius函数可以查询hash数据类型吗
- java - 重定向用户和 POST 参数
- python - 在函数中创建的变量是否具有全局范围?
- javascript - 如何重置每个容器元素的自动编号标题的计数?
- javascript - 中止包含 setInterval 和事件调用的异步函数
- python - 使用条件将浮点数循环到数据框中的 %
- python - 使用另一列作为索引的 Pandas 子字符串
- android - 如何修复“org.apache.http.message.BasicHeader”未实现接口“org.apache.http.NameValuePair”