使用CVX / CVXPY解决L2正则化逻辑回归

时间:2015-05-16 16:49:39

标签: python matlab convex-optimization cvxpy cvx

我已经尝试了2-3天才能使L2正则化的logistric回归在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

enter image description here

我的Matlab(CVX)代码

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返回错误说明哪个有意义,但文章提到了上述等式。我该如何解决?

  

您的目标函数不是标量。

在尝试使用Matlab之后,我尝试了CVXPY。这是python代码

using JSON

get_code_cells(j::Dict) = filter(x->x["cell_type"] == "code", j["cells"])

function parse_code_cell(c::Dict)
    buf = IOBuffer()
    write(buf, "begin\n")
    map(x->write(buf, x), c["source"])
    write(buf, "\nend")

    src = bytestring(buf)
    parse(src)
end

extract_code(cells::Vector) = Expr[parse_code_cell(c) for c in cells]
extract_code(j::Dict) = extract_code(get_code_cells(j))
eval_code(j::Dict) = map(eval, extract_code(j))


# get filename, then parse to json, then run all code
const fn = ARGS[1]
eval_code(JSON.parsefile(fn))

我收到错误

  

TypeError:不兼容的尺寸

所以,我的问题是: 我在CVX / CVXPY中计算L2问题的代码中做错了什么?

1 个答案:

答案 0 :(得分:1)

MATLAB代码中的目标是输出向量,而不是数字(标量)。将其更改为:

(1/m * sum( log(1+ exp(-b.* (A * x)) ) ) )

它将返回一个数字。

相关问题