trixi.experiment

Experiment

class trixi.experiment.experiment.Experiment(n_epochs=0)[source]

Bases: object

An abstract Experiment which can be run for a number of epochs.

The basic life cycle of an experiment is:

setup()
prepare()

while epoch < n_epochs:
    train()
    validate()
    epoch += 1

end()

If you want to use another criterion than number of epochs, e.g. stopping based on validation loss, you can implement that in your validation method and just call .stop() at some point to break the loop. Just set your n_epochs to a high number or np.inf.

The reason there is both setup() and prepare() is that internally there is also a _setup_internal() method for hidden magic in classes that inherit from this. For example, the trixi.experiment.pytorchexperiment.PytorchExperiment uses this to restore checkpoints. Think of setup() as an __init__() that is only called when the Experiment is actually asked to do anything. Then use prepare() to modify the fully instantiated Experiment if you need to.

To write a new Experiment simply inherit the Experiment class and overwrite the methods. You can then start your Experiment calling run()

In Addition the Experiment also has a test function. If you call the run_test() method it will call the test() and end_test() method internally (and if you give the parameter setup = True in run_test is will again call setup() and prepare() ).

Each Experiment also has its current state in _exp_state, its start time in _time_start, its end time in _time_end and the current epoch index in _epoch_idx

Parameters:n_epochs (int) – The number of epochs in the Experiment (how often the train and validate method will be called)
end()[source]

Is called at the end of each experiment

end_test()[source]

Is called at the end of each experiment test

epoch

Convenience access property for self._epoch_idx

prepare()[source]

This method is called directly before the experiment training starts

process_err(e)[source]

This method is called if an error occurs during the execution of an experiment. Will just raise by default.

Parameters:e (Exception) – The exception which was raised during the experiment life cycle
run(setup=True)[source]

This method runs the Experiment. It runs through the basic lifecycle of an Experiment:

setup()
prepare()

while epoch < n_epochs:
    train()
    validate()
    epoch += 1

end()
run_test(setup=True)[source]

This method runs the Experiment.

The test consist of an optional setup and then calls the test() and end_test().

Parameters:setup – If True it will execute the setup() and prepare() function similar to the run method before calling test().
setup()[source]

Is called at the beginning of each Experiment run to setup the basic components needed for a run.

stop()[source]

If called the Experiment will stop after that epoch and not continue training

test()[source]

The testing part of the Experiment

train(epoch)[source]

The training part of the Experiment, it is called once for each epoch

Parameters:epoch (int) – The current epoch the train method is called in
validate(epoch)[source]

The evaluation/validation part of the Experiment, it is called once for each epoch (after the training part)

Parameters:epoch (int) – The current epoch the validate method is called in

PytorchExperiment