Smart Optimiser

Vanguard defines its own optimiser wrapper to enable additional features.

class vanguard.optimise.optimiser.GreedySmartOptimiser(optimiser_class, *initial_modules, early_stop_patience=None, **optimiser_kwargs)[source]

Always choose parameters with the minimum loss value, regardless of the iteration at which they occur.

Note

This is the default smart optimiser for some vanguard.vanilla.GaussianGPController. To disable the greedy loss behaviour and revert to keeping the parameters at the final iteration of training, using vanguard.optimise.optimiser.SmartOptimiser or a different subclass thereof.

Parameters:
__init__(optimiser_class, *initial_modules, early_stop_patience=None, **optimiser_kwargs)[source]

Initialise self.

Parameters:
  • optimiser_class (type[Optimizer]) – An uninstantiated subclass of torch.optim.Optimizer to be used to create the internal optimiser.

  • initial_modules (Module) – Initial modules whose parameters will be added to the internal optimiser.

  • early_stop_patience (Optional[int]) – How many consecutive gradient steps of worsening loss to allow before stopping early. Defaults to None which disables early stopping.

  • optimiser_kwargs (Any) – Additional keyword arguments to be passed to the internal optimiser.

reset()[source]

Reset all parameters to start values and clear record of best parameters.

Return type:

None

set_parameters()[source]

Tidy up after optimisation by setting the parameters to the best.

Return type:

None

step(loss, closure=None)[source]

Step the optimiser and update the record best parameters.

Parameters:
Return type:

None

class vanguard.optimise.optimiser.MaxLengthHeapQ(max_length)[source]

A heapq of fixed maximum length.

Parameters:

max_length (int)

__init__(max_length)[source]

Initialise self.

Parameters:

max_length (int)

best()[source]

Get the top element.

Return type:

Any

nlargest(n)[source]

Get the top elements on the heapq.

Parameters:

n (int)

Return type:

list[Any]

push(item)[source]

Push to the heapq.

Parameters:

item (Any)

Return type:

None

exception vanguard.optimise.optimiser.NoImprovementError[source]

Raised when the loss of the model is consistently increasing.

class vanguard.optimise.optimiser.Parameters(module_state_dicts, value=inf)[source]

Wrapped for module state_dicts and an objective value of their quality.

Parameters:
__init__(module_state_dicts, value=inf)[source]

Initialise self.

Parameters:
class vanguard.optimise.optimiser.SmartOptimiser(optimiser_class, *initial_modules, early_stop_patience=None, **optimiser_kwargs)[source]

A smart wrapper around the standard optimisers found in PyTorch which can enable early stopping.

Warning

When setting the learning rate, using the learning_rate() property, the parameters for each registered module are re-initialised.

Parameters:
__init__(optimiser_class, *initial_modules, early_stop_patience=None, **optimiser_kwargs)[source]

Initialise self.

Parameters:
  • optimiser_class (type[Optimizer]) – An uninstantiated subclass of torch.optim.Optimizer to be used to create the internal optimiser.

  • initial_modules (Module) – Initial modules whose parameters will be added to the internal optimiser.

  • early_stop_patience (Optional[int]) – How many consecutive gradient steps of worsening loss to allow before stopping early. Defaults to None which disables early stopping.

  • optimiser_kwargs (Any) – Additional keyword arguments to be passed to the internal optimiser.

property learning_rate: float

Return the learning rate.

parameters()[source]

Get all parameters known to the optimiser.

Return type:

Generator[Any, None, None]

register_module(module)[source]

Register the parameters for a module.

Parameters:

module (Module)

Return type:

None

reset()[source]

Reset everything.

Return type:

None

set_parameters()[source]

Tidy up after optimisation is completed.

Return type:

None

step(loss, closure=None)[source]

Perform a single optimisation step.

Parameters:
Return type:

Union[float, Tensor, None]

update_registered_module(module)[source]

Update the parameters of a registered module if the module has been modified.

Parameters:

module (Module)

Return type:

None

zero_grad(set_to_none=False)[source]

Set the gradients of all optimized torch.Tensor s to zero.

Parameters:

set_to_none (bool)

Return type:

None