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

KeyError, when deepcopy a nn.Module object contains cvxpylayer #150

Open
wadx2019 opened this issue Oct 18, 2023 · 1 comment
Open

KeyError, when deepcopy a nn.Module object contains cvxpylayer #150

wadx2019 opened this issue Oct 18, 2023 · 1 comment

Comments

@wadx2019
Copy link

When I applied cvxpylayer in my project, I found a problem in cvxpylayer. This leads to KeyError, when we deepcopy a existing module that contains cvxpylayer. For example, you can try the code below.

import cvxpy as cp
from cvxpylayers.torch import CvxpyLayer
import torch
import copy
n = 6
m = 3
x = cp.Variable(n)
x_prime = cp.Parameter(n)
constraints = [cp.pnorm(x, 2) <= m]
objective = cp.Minimize(0.5 * cp.pnorm(x - x_prime, 2))
problem = cp.Problem(objective, constraints)
assert problem.is_dpp()

projection = CvxpyLayer(problem, parameters=[x_prime], variables=[x])

s=torch.nn.Sequential(projection)
s=s.cuda()
sc=copy.deepcopy(s)
x=torch.randn(100,n, requires_grad=True)
sc(x)
@testMonkey02
Copy link

确实,keyerror:221
别用deepcopy了就

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