I have been trying for 2-3 days now to get L2 regularized logistric regression to work in Matlab (CVX) and Python(CVXPY) but no success. I am fairly new to convex optimization so I am quite frustrated. Following is the equation that I am trying to solve using CVX/CVXPY. I have taken this equation from the paper https://intentmedia.github.io/assets/2013-10-09-presenting-at-ieee-big-data/pld_js_ieee_bigdata_2013_admm.pdf
My Matlab (CVX) code is
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 returns an error saying which makes sense but the paper mentions the above equation. How can I solve it ?
Your objective function is not a scalar.
After trying on Matlab, I tried on CVXPY. Here is the python code
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())
I get the error
TypeError: incompatible dimensions
So, my question is: What am I doing wrong in the code for calculation of L2 problem in CVX/CVXPY ?
The objective in your MATLAB code is outputting a vector, not a number (scalar). Change it to:
(1/m * sum( log(1+ exp(-b.* (A * x)) ) ) )
And it will return a single number.