cvxpylayers icon indicating copy to clipboard operation
cvxpylayers copied to clipboard

train can not converge on simple problem

Open wangmn93 opened this issue 2 years ago • 1 comments

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_()

wangmn93 avatar Apr 13 '22 14:04 wangmn93

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

kunshouout avatar Oct 13 '22 09:10 kunshouout