gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:00:10 25 4
gpt4 key购买 nike

我正在使用 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 中运行此代码。

最佳答案

您的代码似乎与 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 代码以使您的变量成为一维。

关于python - ECOS 求解器在 CVXPY 1.0 版本中停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52518534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com