0

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

trincot
  • 317,000
  • 35
  • 244
  • 286
Manish Bhanu
  • 53
  • 10
  • 1
    Why don't you show the error or any *verbose* output of those solvers? – sascha Feb 21 '18 at 14:41
  • Look into that masking-stuff. You should never have any non-finite value (inf/nan) in any cvxpy-expression! It might help to work on the COO-form of data: ```observation: col, row, value```, like [here](https://github.com/sschnug/pyMMMF/blob/master/maxNorm_binHinge_primal_cvxpy.py) (warning: it's not the most efficient code and it's outdated). – sascha Feb 21 '18 at 14:57
  • Thanks @sascha , replacing nan with 0, does return a value. Though the value is not what i expect (doesnot seems to approximated matrix) but this removes the bug out of the code. – Manish Bhanu Feb 21 '18 at 15:16

0 Answers0