I have written the following code
import cvxpy
import numpy as np
def missingMat(A, mask):##true and false for known and missing entries
M=np.array(A)
for i in range(0, mask.shape[0]):
for j in range(0, mask.shape[1]):
if mask[i][j]==False:
M[i][j]=float("nan")
return M
def constraintsFun(X, missing_mask, S, error_tolerance):
ok_mask = ~missing_mask
masked_X = cvxpy.mul_elemwise(ok_mask, X)
masked_S = cvxpy.mul_elemwise(ok_mask, S)
abs_diff = cvxpy.abs(masked_S - masked_X)
close_to_data =(abs_diff <= 0.0001)
constraints = [close_to_data]
return constraints
def create_objective(m, n):
S=cvxpy.Semidef(m,n)
norm = cvxpy.norm(S, "nuc")
objective = cvxpy.Minimize(norm)
return S, objective
def solve(X, missing_mask):
m, n = X.shape
S, objective = create_objective(m, n)
constraints = constraintsFun(X=X, missing_mask=missing_mask, S=S, error_tolerance=0.0001)
problem = cvxpy.Problem(objective, constraints)
solverLis=cvxpy.installed_solvers()
for solv in solverLis:
try:
problem.solve(solver=solv)##.................error line
except:
print "did not solve", solv
print S.value
return problem, S
A=np.random.uniform(low=1, high=15, size=(8,8))
mask = np.round(np.random.rand(A.shape[0], A.shape[1]))
mask=(mask >= 1)
M=missingMat(A, mask)
prob, S=solve(M, mask)
I am trying to understand how to use cvxpy for the matrix completion problem.
I have a matrix M, with missing entries corresponding to the mask
matrix. It's minimized to the nuclear norm of S, and the constraints correspond to the matching of mask
-True
entries of S and M, within a certain tolerance.
Can someone help me find the mistakes in my code or logic? I am unable to find the proper use of solve()
. I am trying to get missing values by S.value
.
minS ||S||* s.t. S=M for known entries