Skip to content

Help needed on a simple example on bi-level optimization #74

Answered by XuehaiPan
xianghang asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @xianghang, after each meta-optimization step (i.e., before the next meta-optimization loop), you need to use torchopt.stop_gradient to detach the previous computational graph. See also our tutorial tutorials/4_Stop_Gradient.ipynb.

Here is an example:

import torch
import torch.nn as nn

import torchopt

class Net(nn.Module):
    def __init__(self):
        super().__init__()
        self.y = nn.Parameter(torch.tensor(1.0))

    def loss_lower(self, x):
        return 0.5 * (x - self.y) ** 2

    def loss_upper(self, x):
        return 0.5 * (x**2 + self.y**2)
net = Net().train()

x = nn.Parameter(torch.tensor(2.0))
optim_upper = torchopt.SGD([x], lr=1e-1)
optim = torchopt.MetaSGD(net, lr=

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@xianghang
Comment options

Answer selected by xianghang
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants