Skip to content

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

Open
@wadx2019

Description

@wadx2019

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions