Main loop

class blocks.main_loop.MainLoop(algorithm, data_stream, model=None, log=None, log_backend=None, extensions=None)[source]

Bases: object

The standard main loop of Blocks.

In the MainLoop a model is trained by a training algorithm using data extracted from a data stream. This process is scrupulously documented in a log object.

The MainLoop itself does very little: only fetching the data from the data stream and feeding it to the algorithm. It expects the extensions to do most of the job. A respective callback of every extension is called at every stage of training. The extensions should communicate between themselves and with the main loop object by means of making records in the log. For instance in order to stop the training procedure an extension can make a record training_finish_requested=True in the log. The main loop checks for such a record after every batch and every epoch and terminates when finds it.

The MainLoop also handles interruption signal SIGINT for you (e.g. the one program receives when you press Ctrl + C). It notes this event in the log and at the next iteration or epoch end the main loop will be gracefully finished, with calling all necessary extension callbacks and waiting until they finish.

  • algorithm (instance of TrainingAlgorithm) – The training algorithm.
  • data_stream (instance of DataStream.) – The data stream. Should support AbstractDataStream interface from Fuel.
  • model (instance of ComputationGraph, optional) – An annotated computation graph, typically represented by ComputationGraph or Model object. The main loop object uses the model only for optional sanity checks, it is here mainly for the main loop extensions.
  • log (instance of TrainingLog, optional) – The log. When not given, a TrainingLog is created.
  • log_backend (str) – The backend to use for the log. Currently python and sqlite are available. If not given, config.log_backend will be used. Ignored if log is passed.
  • extensions (list of TrainingExtension instances) – The training extensions. Will be called in the same order as given here.

Find an extension with a given name.

Parameters:name (str) – The name of the extension looked for.


Will crash if there no or several extension found.


Quick access to the (data stream, epoch iterator) pair.


Starts the main loop.

The main loop ends when a training extension makes a training_finish_requested record in the log.


A shortcut for self.log.status.

exception blocks.main_loop.TrainingFinish[source]

Bases: exceptions.Exception

An exception raised when a finish request is found in the log.