Shortcuts

Loop

class pytorch_lightning.loops.base.Loop[source]

Bases: abc.ABC, Generic[pytorch_lightning.loops.base.T]

Basic Loops interface. All classes derived from this must implement the following properties and methods:

  • done (property): Condition to break the loop

  • reset (method): Resets the internal state between multiple calls of run

  • advance (method): Implements one step of the loop

This class implements the following loop structure:

on_run_start()

while not done:
    on_advance_start()
    advance()
    on_advance_end()

on_run_end()
abstract advance(*args, **kwargs)[source]

Performs a single step.

Accepts all arguments passed to run.

Example:

def advance(self, iterator):
    batch = next(iterator)
    loss = self.trainer.lightning_module.training_step(batch, batch_idx)
    ...
Return type

None

connect(**kwargs)[source]

Optionally connect one or multiple loops to this one.

Linked loops should form a tree.

Return type

None

load_state_dict(state_dict, prefix='', metrics=None)[source]

Loads the state of this loop and all its children.

Return type

None

on_advance_end()[source]

Hook to be called each time after advance is called.

Return type

None

on_advance_start(*args, **kwargs)[source]

Hook to be called each time before advance is called.

Accepts all arguments passed to :attr`run`.

Return type

None

on_load_checkpoint(state_dict)[source]

Called when loading a model checkpoint, use to reload loop state.

Return type

None

on_run_end()[source]

Hook to be called at the end of the run.

Its return argument is returned from run.

Return type

~T

on_run_start(*args, **kwargs)[source]

Hook to be called as the first thing after entering run (except the state reset).

Accepts all arguments passed to run.

Return type

None

on_save_checkpoint()[source]

Called when saving a model checkpoint, use to persist loop state.

Return type

Dict

Returns

The current loop state.

on_skip()[source]

The function to run when run() should be skipped, determined by the condition in skip.

Return type

~T

Returns

the default output value of on_run_end()

abstract reset()[source]

Resets the internal state of the loop at the beginning of each call to run.

Example:

def reset(self):
    # reset your internal state or add custom logic
    # if you expect run() to be called multiple times
    self.current_iteration = 0
    self.outputs = []
Return type

None

run(*args, **kwargs)[source]

The main entry point to the loop.

Will frequently check the done condition and calls advance until done evaluates to True.

Override this if you wish to change the default behavior. The default implementation is:

Example:

def run(self, *args, **kwargs):
    if self.skip:
        return self.on_skip()

    self.reset()
    self.on_run_start(*args, **kwargs)

    while not self.done:
        self.advance(*args, **kwargs)

    output = self.on_run_end()
    return output
Return type

~T

Returns

The output of on_run_end (often outputs collected from each step of the loop)

state_dict(destination=None, prefix='')[source]

The state dict is determined by the state and progress of this loop and all its children.

Parameters
  • destination (Optional[Dict]) – An existing dictionary to update with this loop’s state. By default a new dictionary is returned.

  • prefix (str) – A prefix for each key in the state dictionary

Return type

Dict

teardown()[source]

Use to release memory etc.

Return type

None

abstract property done: bool

Property indicating when the loop is finished.

Example:

@property
def done(self):
    return self.trainer.global_step >= self.trainer.max_steps
Return type

bool

property skip: bool

Determine whether to return immediately from the call to run().

Example:

@property
def skip(self):
    return len(self.trainer.train_dataloader) == 0
Return type

bool

Read the Docs v: latest
Versions
latest
stable
1.5.4
1.5.3
1.5.2
1.5.1
1.5.0
1.4.9
1.4.8
1.4.7
1.4.6
1.4.5
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.3.8
1.3.7
1.3.6
1.3.5
1.3.4
1.3.3
1.3.2
1.3.1
1.3.0
1.2.10
1.2.8
1.2.7
1.2.6
1.2.5
1.2.4
1.2.3
1.2.2
1.2.1
1.2.0
1.1.8
1.1.7
1.1.6
1.1.5
1.1.4
1.1.3
1.1.2
1.1.1
1.1.0
1.0.8
1.0.7
1.0.6
1.0.5
1.0.4
1.0.3
1.0.2
1.0.1
1.0.0
0.10.0
0.9.0
0.8.5
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.6
0.7.5
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0
0.6.0
0.5.3
0.4.9
ipynb-update
docs-search
Downloads
html
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.