ECOS 求解器在 CVXPY 1.0 版本中停止工作

ECOS solver stops working in CVXPY 1.0 version

我当时使用的是 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 不一致。在 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 成为一维的。