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

时间:2018-09-26 13:08:29

标签: python lasso cvxpy

我正在使用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代码版本时,它显示以下错误消息: image

因此,我想我已经将代码从0.4版本正确迁移到了1.0,但是在0.4版本中使用ECOS求解器解决了一个问题,在1.0版本中显示了错误消息。我在这里做错什么了吗?以防万一,在Windows机器中,我在miniconda python 2.7中运行此代码。

1 个答案:

答案 0 :(得分:1)

您的代码似乎与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的一维。