Skip to content

Optimization in a dual encoder LitModel #9807

Discussion options

You must be logged in to vote

I see two ways. I think your example is quite simple so it does not matter which way you choose in the end:

1) Automatic Optimization:

    def training_step(self, batch, batch_idx, optimizer_idx):
        x1, x2 = batch["x1"], batch["x2"]
        if optimizer_idx == 0;
            x1_repr = self.x1_encoder(x1)
            x1_loss=self.x1_loss(x1_repr, x2_repr)
            return x1_loss
       if optimizer_idx == 1:
           x2_repr = ...
           return x2_loss


    def configure_optimizers(self):
        return [
        {"optimizer": torch.optim.AdamW(self.x1_encoder.parameters(), ...), "frequency": 1}, 
        {"optimizer": torch.optim.AdamW(self.x2_encoder.parameters(), ...), "…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@celsofranssa
Comment options

@awaelchli
Comment options

@celsofranssa
Comment options

Answer selected by celsofranssa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #9806 on October 03, 2021 17:10.