我已经尝试了 2-3 天来让 L2 正则化逻辑回归在 Matlab(CVX)和 Python(CVXPY)中工作,但没有成功。我对凸优化还很陌生,所以我很沮丧。以下是我尝试使用 CVX/CVXPY 求解的方程。我从论文 https://intentmedia.github.io/assets/2013-10-09-presenting-at-ieee-big-data/pld_js_ieee_bigdata_2013_admm.pdf中获取了这个等式
我的 Matlab (CVX) 代码是
function L2
m = 800; N = 5;
lambda =0.000001;
A = load('/path/to/training/file');
b= A(:,6); //Label Matrix (800x1)
A = A(:,1:5); //Feature matrix (800x5)
cvx_begin
variable x(N)
minimize( (1/m * sum( log(1+ exp(-1* A' * (b * x')) ) ) ) + lambda*(norm(x,2)))
cvx_end
CVX 返回一个错误,说这是有道理的,但论文提到了上述等式。我该如何解决?
您的目标函数不是标量。
在尝试了 Matlab 之后,我尝试了 CVXPY。这是python代码
from cvxopt import solvers, matrix,log, exp,mul
from cvxopt.modeling import op,variable
import numpy as np
n = 5
m=800
data = np.ndarray(shape=(m,n), dtype=float,)
bArray = []
file = open('/path/to/training/file')
i = 0;
j=0;
for line in file:
for num in line.split():
if(j==5):
bArray.append(float(num))
else:
data[i][j] = num
j = j + 1
j=0
i = i + 1
A = matrix(data)
b_mat= matrix(bArray)
m, n = A.size
lamb_default = 0.000001
x=variable(n)
b = -1*b_mat
w = exp(A.T*b*x)
f = (1/m) + sum(log(1+w)) + lamb_default*mul(x,x)
lp1 = op(f)
lp1.solve()
lp1.status
print(lp1.objective.value())
我得到错误
类型错误:尺寸不兼容
所以,我的问题是:我在 CVX/CVXPY 中计算 L2 问题的代码中做错了什么?