Reputation: 1
I am writing a convex optimization problem with cvxpy, but it returns that the problem is not DPP even though it is.
import cvxpy as cp
import numpy as np
from cvxpylayers.tensorflow import CvxpyLayer
# Simplified proxy functions for debugging
def score(x, w, b):
return x @ w + b
def sign_proxy(x, w, b, temperature=1):
return 0.5 * cp.norm(cp.hstack([1, (temperature * score(x, w, b) + 1)]), 2) + \
0.5 * cp.norm(cp.hstack([1, (temperature * score(x, w, b) - 1)]), 2)
def c_Quad(x, xt, scale=1):
return scale * cp.sum_squares(x - xt)
class DELTA:
def __init__(self, x_dim, N, funcs):
self.score_fn = funcs["score_fn"]
self.cost_fn = funcs["cost_fn"]
self.sign_proxy_fn = funcs["sign_proxy_fn"]
self.xt = cp.Variable(x_dim)
self.x = cp.Parameter(x_dim)
self.w = cp.Parameter((x_dim, N))
self.b = cp.Parameter(N)
sign_proxy = self.sign_proxy_fn(self.xt, self.w, self.b)
target = cp.sum(sign_proxy) - self.cost_fn(self.xt, self.x)
X_LOWER_BOUND = -10 # Example value, replace with your actual lower bound
X_UPPER_BOUND = 10 # Example value, replace with your actual upper bound
constraints = [
self.xt >= X_LOWER_BOUND,
self.xt <= X_UPPER_BOUND
]
objective = cp.Maximize(target)
problem = cp.Problem(objective, constraints)
print(problem.is_dcp(dpp=True))
self.layer = CvxpyLayer(problem, parameters=[self.x, self.w, self.b], variables=[self.xt])
def optimize_X(self, x, w, b):
return self.layer(x, w, b)[0]
# Example usage
x_dim = 5 # Example dimension
N = 10 # Example number of columns
funcs = {
"score_fn": score,
"cost_fn": c_Quad,
"sign_proxy_fn": sign_proxy
}
delta = DELTA(x_dim, N, funcs)
x = np.random.randn(x_dim)
w = np.random.randn(x_dim, N)
b = np.random.randn(N)
# Optimize X
optimized_xt = delta.optimize_X(x, w, b)
print("Optimized xt:", optimized_xt)
The line:
self.layer = CvxpyLayer(problem, parameters=[self.x, self.w, self.b], variables=[self.xt])
fails on "Problem must be DPP": ValueError: Problem must be DPP. even though all the elements in the problem are convex. What do I need to do?
Upvotes: 0
Views: 102
Reputation: 23
I think that your problem is not DCP, and therefore not DPP.
My guess is that in this line:
target = cp.sum(sign_proxy) - self.cost_fn(self.xt, self.x)
you are negating a concave function and therefore making it convex and then you are trying to maximize it. Please tell me if I'm wrong.
Maybe you should take a look at the signs?
Upvotes: 0