Skip to content

Tutorial "step optimizer at arbitrary intervals" does not call train_step every iteration #7337

@lizhitwo

Description

@lizhitwo

🐛 Bug

The tutorial step optimizer at arbitrary intervals has a bug/feature(*) that the closure is evaluated (with optimizer_idx=1) only every other batch, not every batch.

(*bug/feature) Note: this behavior is intended for e.g. GAN training where a part of the model is only trained every n batches, and the tutorial has the expected behavior. But the language looks like the tutorial is also applicable to people who are trying to implement accumulate_grad_batches here (Pytorch-lightning forbids us using accumulate_grad_batches if we override optimizer_step). For this use case, using the tutorial leads to the unexpected behavior.

Please reproduce using the BoringModel

To Reproduce

Colab

Difference: overrode optimizer_step, added printing in training_step.

Expected behavior

The training step is called every batch.

training_step(batch_idx=0)
training_step(batch_idx=1)
training_step(batch_idx=2)
training_step(batch_idx=3)
training_step(batch_idx=4)
training_step(batch_idx=5)
training_step(batch_idx=6)
...

Actual behavior

training_step(batch_idx=1)
training_step(batch_idx=3)
training_step(batch_idx=5)
training_step(batch_idx=7)
training_step(batch_idx=9)
training_step(batch_idx=11)
training_step(batch_idx=13)
...

Metadata

Metadata

Labels

bugSomething isn't workingdocsDocumentation relatedhelp wantedOpen to be worked on

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions