Cvxpy: DCPError : expr.is_dcp() returns False for expressions of the form w.T@w

Created on 10 Mar 2020  路  3Comments  路  Source: cvxgrp/cvxpy

Describe the bug
Any expression of the form x.T@P@x in cvxpy gives DCPError. When checked if the expression is DCP or not, expr.is_dcp() return False.

To Reproduce

import cvxpy as cp
import numpy as np

alphas = cp.Variable((2*NUM,1))
Cd = cp.Parameter()
Cd.value = 0.95

alphas_constraint_1 = [alphas[i] >=0 for i in range(2*NUM)]
alphas_constraint_2 = [alphas[i] <= Cd for i in range(2*NUM)]
alpha_y_constraint = [alphas.T@y == 0]

constraints = alphas_constraint_1 + alphas_constraint_2 + alpha_y_constraint

K = y[:,None]*x
K = np.dot(K,K.T)

objective_dual = cp.Minimize(-cp.sum(alphas)+alphas.T@K@alphas)

prob_dual = cp.Problem(objective_dual,constraints)

Expected behavior
As shown in various examples on the cvxpy tutorials, the above problem is expected to run without failure.

Version

  • OS: Windows 10
  • CVXPY Version: 1.1.0a3

Additional context
The problem I am trying to solve here is the dual form of SVM
Link to question posted on Stackoverflow

All 3 comments

You need to use the quad_form atom, as in quad_form(x, P).

@sadimanna , please read the documentation on DCP here: https://www.cvxpy.org/tutorial/dcp/index.html

DCP is a grammar for constructing convex expressions, using a simple composition rule. Once you learn this grammar (it's just one rule), you should be able to easily express essentially any convex expression in CVXPY.

You need to use the quad_form atom, as in quad_form(x, P).
@SteveDiamond @akshayka
I have tried using
-cp.sum(alphas)+0.5*cp.quad_form(alphas,K)
But I still get the same error

DCPError: Problem does not follow DCP rules. Specifically:
The objective is not DCP. Its following subexpressions are not:
QuadForm(var14052, [[ 1.83491803  1.75045974  2.28622267 ... -4.39732493 -5.00052443
  -3.77439719]
 [ 1.75045974  2.34203535  2.34179792 ... -5.44176538 -5.89979043
  -4.35890149]
 [ 2.28622267  2.34179792  2.8869991  ... -5.77716101 -6.50063002
  -4.88412578]
 ...
 [-4.39732493 -5.44176538 -5.77716101 ... 12.85096644 14.07871791
  10.45176273]
 [-5.00052443 -5.89979043 -6.50063002 ... 14.07871791 15.52527056
  11.56008668]
 [-3.77439719 -4.35890149 -4.88412578 ... 10.45176273 11.56008668
   8.61922173]])

I tried running this piece of code
expr = cp.quad_form(alphas,K) print(expr.curvature)

I get output UNKNOWN
Capture

Was this page helpful?
0 / 5 - 0 ratings