Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

train can not converge on simple problem #127

Open
wangmn93 opened this issue Apr 13, 2022 · 1 comment
Open

train can not converge on simple problem #127

wangmn93 opened this issue Apr 13, 2022 · 1 comment

Comments

@wangmn93
Copy link

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
Copy link

kunshouout commented Oct 13, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants