Robust PCA

Solve the robust PCA problem taken from Xinyang Yi, et al. “Fast algorithms for robust PCA via gradient descent.” Advances in neural information processing systems. 2016.

Problem Description

\[\min_{M,S}||M||_{\text{nuc}}+\lambda||S||_1,\]
\[\text{s.t. }Y=M+S,\]

where \(M,S\in R^{d_1,d_2}\) are matrix form optimization variables, \(Y\in R^{d_1,d_2}\) is a given matrix, and \(||\cdot||_{\text{nuc}}\) denotes the nuclear norm.

Modules Importing

Import all necessary modules and add PyGRANSO src folder to system path.

[1]:
import time
import torch
from pygranso.pygranso import pygranso
from pygranso.pygransoStruct import pygransoStruct

Data Initialization

Specify torch device, and generate data.

Use GPU for this problem. If no cuda device available, please set device = torch.device(‘cpu’)

[2]:
device = torch.device('cuda')
d1 = 3
d2 = 4
torch.manual_seed(1)
eta = .05
# All the user-provided data (vector/matrix/tensor) must be in torch tensor format.
# As PyTorch tensor is single precision by default, one must explicitly set `dtype=torch.double`.
# Also, please make sure the device of provided torch tensor is the same as opts.torch_device.
Y = torch.randn(d1,d2).to(device=device, dtype=torch.double)

Function Set-Up

Encode the optimization variables, and objective and constraint functions.

Note: please strictly follow the format of comb_fn, which will be used in the PyGRANSO main algortihm.

[3]:
# variables and corresponding dimensions.
var_in = {"M": [d1,d2],"S": [d1,d2]}


def user_fn(X_struct,Y):
    M = X_struct.M
    S = X_struct.S

    # objective function
    f = torch.norm(M, p = 'nuc') + eta * torch.norm(S, p = 1)

    # inequality constraint, matrix form
    ci = None

    # equality constraint
    ce = pygransoStruct()
    ce.c1 = M + S - Y

    return [f,ci,ce]

comb_fn = lambda X_struct : user_fn(X_struct,Y)

User Options

Specify user-defined options for PyGRANSO

[4]:
opts = pygransoStruct()
opts.torch_device = device
opts.print_frequency = 10
opts.x0 = .2 * torch.ones((2*d1*d2,1)).to(device=device, dtype=torch.double)
opts.opt_tol = 1e-6

Main Algorithm

[5]:
start = time.time()
soln = pygranso(var_spec = var_in,combined_fn = comb_fn,user_opts = opts)
end = time.time()
print("Total Wall Time: {}s".format(end - start))


╔═════ QP SOLVER NOTICE ════════════════════════════════════════════════════════════════════════╗
║  PyGRANSO requires a quadratic program (QP) solver that has a quadprog-compatible interface,  ║
║  the default is osqp. Users may provide their own wrapper for the QP solver.                  ║
║  To disable this notice, set opts.quadprog_info_msg = False                                   ║
╚═══════════════════════════════════════════════════════════════════════════════════════════════╝
═════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗
PyGRANSO: A PyTorch-enabled port of GRANSO with auto-differentiation                                             ║
Version 1.0.0                                                                                                    ║
Licensed under the AGPLv3, Copyright (C) 2021 Tim Mitchell and Buyun Liang                                       ║
═════════════════════════════════════════════════════════════════════════════════════════════════════════════════╣
Problem specifications:                                                                                          ║
 # of variables                     :   24                                                                       ║
 # of inequality constraints        :    0                                                                       ║
 # of equality constraints          :   12                                                                       ║
═════╦═══════════════════════════╦════════════════╦═════════════════╦═══════════════════════╦════════════════════╣
     ║ <--- Penalty Function --> ║                ║ Total Violation ║ <--- Line Search ---> ║ <- Stationarity -> ║
Iter ║    Mu    │      Value     ║    Objective   ║ Ineq │    Eq    ║ SD │ Evals │     t    ║ Grads │    Value   ║
═════╬═══════════════════════════╬════════════════╬═════════════════╬═══════════════════════╬════════════════════╣
   0 ║ 1.000000 │  9.26721133015 ║  0.81282032303 ║   -  │ 1.922768 ║ -  │     1 │ 0.000000 ║     1 │ 2.502220   ║
  10 ║ 0.228768 │  0.10944851437 ║  0.47842596196 ║   -  │ 8.88e-16 ║ S  │     1 │ 1.000000 ║     1 │ 4.649133   ║
  20 ║ 0.088629 │  0.02987332406 ║  0.33705892627 ║   -  │ 5.28e-13 ║ S  │     4 │ 0.125000 ║     1 │ 0.013927   ║
  30 ║ 0.088629 │  0.02961763578 ║  0.33417401065 ║   -  │ 1.15e-13 ║ S  │     3 │ 0.250000 ║     1 │ 0.005520   ║
  40 ║ 0.088629 │  0.02958952106 ║  0.33385679403 ║   -  │ 5.66e-15 ║ S  │     3 │ 0.250000 ║     1 │ 8.10e-04   ║
  50 ║ 0.030903 │  0.01031425037 ║  0.33376043889 ║   -  │ 6.94e-16 ║ S  │     4 │ 0.125000 ║     6 │ 3.90e-05   ║
  60 ║ 0.004638 │  0.00154806127 ║  0.33374903361 ║   -  │ 1.14e-10 ║ S  │     1 │ 1.000000 ║    14 │ 9.31e-07   ║
═════╩═══════════════════════════╩════════════════╩═════════════════╩═══════════════════════╩════════════════════╣
Optimization results:                                                                                            ║
F = final iterate, B = Best (to tolerance), MF = Most Feasible                                                   ║
═════╦═══════════════════════════╦════════════════╦═════════════════╦═══════════════════════╦════════════════════╣
   F ║          │                ║  0.33374903361 ║   -  │ 1.14e-10 ║    │       │          ║       │            ║
   B ║          │                ║  0.33374903361 ║   -  │ 1.14e-10 ║    │       │          ║       │            ║
  MF ║          │                ║  0.33389618278 ║   -  │ 0.000000 ║    │       │          ║       │            ║
═════╩═══════════════════════════╩════════════════╩═════════════════╩═══════════════════════╩════════════════════╣
Iterations:              60                                                                                      ║
Function evaluations:    163                                                                                     ║
PyGRANSO termination code: 0 --- converged to stationarity and feasibility tolerances.                           ║
═════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝
Total Wall Time: 1.6486585140228271s