cvxgrp / cvxpylayers

Differentiable convex optimization layers
Apache License 2.0
1.82k stars 160 forks source link

train can not converge on simple problem #127

Open wangmn93 opened 2 years ago

wangmn93 commented 2 years ago

I have a simple problem trying to optimize exp_ret_tch. The optimal value of exp_ret_tch should be something like [0.1 -0.1, 0.2], the second element should decrease and the third element should increase. But it does not converge and the gradient of exp_ret_tch is very small. cvxpylayers==0.1.5 torch==1.10.1+cu102

import cvxpy as cp
import torch
import numpy as np
from cvxpylayers.torch import CvxpyLayer

n = 3
weight = cp.Variable(n)
exp_ret = cp.Parameter(n)
cons = [cp.sum(weight)==1,
weight>=0]
obj = cp.Maximize( exp_ret @ weight )
prob = cp.Problem(obj, cons)
cvxpylayer = CvxpyLayer(prob, [exp_ret], [weight])

exp_ret_tch = torch.from_numpy(np.aray([0.1, 0.2,-0.1])).requires_grad_(True)
true_ret_tch = torch.from_numpy(np.aray([-0.1, -0.1,0.5]))

for _ in range(1000):
    opt_wt , = cvxpylayer(exp_ret_tch,)
    pnl = - torch.sum(opt_wt * true_ret_tch) 
    pnl.backward()
    with torch.no_grad():
        exp_ret_tch -= exp_ret_tch.grad * 1
        exp_ret_tch.grad.zero_()
kunshouout commented 2 years ago

Encountered the same problem. In cvxpylayers/torch/cvxpylayer.py, line 338 dAs, dbs, dcs = ctx.DT_batch(dxs, dys, dss) dcs are all very small(1e-8)

Any clue why this happens? @SteveDiamond