我写了以下代码
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)
我试图了解如何使用 cvxpy 来解决矩阵完成问题。
我有一个矩阵 M,缺少对应于mask
矩阵的条目。它被最小化到 S 的核范数,并且约束对应于 S 和 M 的mask
-True
条目的匹配,在一定的容差内。
有人可以帮我找出我的代码或逻辑中的错误吗?我无法找到正确使用solve()
. 我试图通过S.value
.
最小S ||S|| * st S=M 对于已知条目