gpflow.monitor

gpflow.monitor.ExecuteCallback

class gpflow.monitor.ExecuteCallback(callback)[source]

Bases: gpflow.monitor.base.MonitorTask

Executes a callback as task

Methods

__call__(step, **kwargs)

It calls the ‘run’ function and sets the current step.

run(**kwargs)

Implements the task to be executed on __call__.

Parameters

callback (Callable[…, None]) –

__init__(callback)[source]
Parameters

callback (Callable[…, None]) – callable to be executed during the task. Arguments can be passed using keyword arguments.

run(**kwargs)[source]

Implements the task to be executed on __call__. The current step is available through self.current_step.

Parameters

kwargs – keyword arguments available to the run method.

gpflow.monitor.ImageToTensorBoard

class gpflow.monitor.ImageToTensorBoard(log_dir, plotting_function, name=None, *, fig_kw=None, subplots_kw=None)[source]

Bases: gpflow.monitor.tensorboard.ToTensorBoard

Methods

__call__(step, **kwargs)

It calls the ‘run’ function and sets the current step.

run(**unused_kwargs)

Implements the task to be executed on __call__.

Parameters
  • log_dir (str) –

  • plotting_function (Callable[[ForwardRef, ForwardRef], ForwardRef]) –

  • name (Optional[str]) –

  • fig_kw (Optional[Dict[str, Any]]) –

  • subplots_kw (Optional[Dict[str, Any]]) –

__init__(log_dir, plotting_function, name=None, *, fig_kw=None, subplots_kw=None)[source]
Parameters
  • log_dir (str) – directory in which to store the tensorboard files. Can be nested: for example, ‘./logs/my_run/’.

  • plotting_function (Callable[[ForwardRef, ForwardRef], ForwardRef]) – function performing the plotting.

  • name (Optional[str]) – name used in TensorBoard.

Params fig_kw

keyword arguments to be passed to Figure constructor, e.g. figsize.

Params subplots_kw

keyword arguments to be passed to figure.subplots constructor, e.g. nrows, ncols, sharex, sharey. By default the default values from matplotlib.pyplot are used.

Parameters
  • fig_kw (Optional[Dict[str, Any]]) –

  • subplots_kw (Optional[Dict[str, Any]]) –

run(**unused_kwargs)[source]

Implements the task to be executed on __call__. The current step is available through self.current_step.

Parameters

kwargs – keyword arguments available to the run method.

gpflow.monitor.ModelToTensorBoard

class gpflow.monitor.ModelToTensorBoard(log_dir, model, *, max_size=3, keywords_to_monitor=['kernel', 'likelihood'], left_strip_character='.')[source]

Bases: gpflow.monitor.tensorboard.ToTensorBoard

Monitoring task that creates a sensible TensorBoard for a model.

Monitors all the model’s parameters for which their name matches with keywords_to_monitor. By default, “kernel” and “likelihood” are elements of keywords_to_monitor. Example:

keyword = “kernel”, parameter = “kernel.lengthscale” => match keyword = “variational”, parameter = “kernel.lengthscale” => no match

Methods

__call__(step, **kwargs)

It calls the ‘run’ function and sets the current step.

run(**unused_kwargs)

Implements the task to be executed on __call__.

Parameters
  • log_dir (str) –

  • model (BayesianModel) –

  • max_size (int) –

  • keywords_to_monitor (List[str]) –

  • left_strip_character (str) –

__init__(log_dir, model, *, max_size=3, keywords_to_monitor=['kernel', 'likelihood'], left_strip_character='.')[source]
Parameters
  • log_dir (str) – directory in which to store the tensorboard files. Can be a nested: for example, ‘./logs/my_run/’.

  • model (BayesianModel) – model to be monitord.

  • max_size (int) – maximum size of arrays (incl.) to store each element of the array independently as a scalar in the TensorBoard. Setting max_size to -1 will write all values. Use with care.

  • keywords_to_monitor (List[str]) – specifies keywords to be monitored. If the parameter’s name includes any of the keywords specified it will be monitored. By default, parameters that match the kernel or likelihood keyword are monitored. Adding a “*” to the list will match with all parameters, i.e. no parameters or variables will be filtered out.

  • left_strip_character (str) – certain frameworks prepend their variables with a character. GPflow adds a ‘.’ and Keras add a ‘_’, for example. When a left_strip_character is specified it will be stripped from the parameter’s name. By default the ‘.’ is left stripped, for example: “.likelihood.variance” becomes “likelihood.variance”.

_summarize_parameter(name, param)[source]
Parameters
  • name (str) – identifier used in tensorboard

  • param (Union[Parameter, Variable]) – parameter to be stored in tensorboard

run(**unused_kwargs)[source]

Implements the task to be executed on __call__. The current step is available through self.current_step.

Parameters

kwargs – keyword arguments available to the run method.

gpflow.monitor.Monitor

class gpflow.monitor.Monitor(*task_groups)[source]

Bases: object

Accepts any number of of MonitorTaskGroup instances, and runs them according to their specified periodicity.

Example use-case:

``` # Create some monitor tasks log_dir = “logs” model_task = ModelToTensorBoard(log_dir, model) image_task = ImageToTensorBoard(log_dir, plot_prediction, “image_samples”) lml_task = ScalarToTensorBoard(log_dir, lambda: model.log_marginal_likelihood(), “lml”)

# Plotting tasks can be quite slow, so we want to run them less frequently. # We group them in a MonitorTaskGroup and set the period to 5. slow_tasks = MonitorTaskGroup(image_task, period=5)

# The other tasks are fast. We run them at each iteration of the optimisation. fast_tasks = MonitorTaskGroup([model_task, lml_task], period=1)

# We pass both groups to the Monitor monitor = Monitor(fast_tasks, slow_tasks) ```

Methods

__call__(step, **kwargs)

Call self as a function.

Parameters

task_groups (MonitorTaskGroup) –

__init__(*task_groups)[source]
Parameters

task_groups (MonitorTaskGroup) – a list of `MonitorTaskGroup`s to be executed.

gpflow.monitor.MonitorTask

class gpflow.monitor.MonitorTask[source]

Bases: abc.ABC

A base class for a monitoring task.

All monitoring tasks are callable objects. A descendant class must implement the run method, which is the body of the monitoring task.

Methods

__call__(step, **kwargs)

It calls the ‘run’ function and sets the current step.

run(**kwargs)

Implements the task to be executed on __call__.

__call__(step, **kwargs)[source]

It calls the ‘run’ function and sets the current step.

Parameters
  • step (int) – current step in the optimisation.

  • kwargs – additional keyword arguments that can be passed to the run method of the task. This is in particular handy for passing keyword argument to the callback of ScalarToTensorBoard.

abstract run(**kwargs)[source]

Implements the task to be executed on __call__. The current step is available through self.current_step.

Parameters

kwargs – keyword arguments available to the run method.

gpflow.monitor.MonitorTaskGroup

class gpflow.monitor.MonitorTaskGroup(task_or_tasks, period=1)[source]

Bases: object

Class for grouping MonitorTask instances. A group defines all the tasks that are run at the same frequency, given by period.

A MonitorTaskGroup can exist of a single instance or a list of MonitorTask instances.

Attributes
tasks

Methods

__call__(step, **kwargs)

Call each task in the group.

Parameters
  • task_or_tasks (Union[List[MonitorTask], MonitorTask]) –

  • period (int) –

__call__(step, **kwargs)[source]

Call each task in the group.

__init__(task_or_tasks, period=1)[source]
Parameters
  • task_or_tasks (Union[List[MonitorTask], MonitorTask]) – a single instance or a list of MonitorTask instances. Each MonitorTask in the list will be run with the given period.

  • period (int) – defines how often to run the tasks; they will execute every period`th step. For large values of `period the tasks will be less frequently run. Defaults to running at every step (period = 1).

gpflow.monitor.ScalarToTensorBoard

class gpflow.monitor.ScalarToTensorBoard(log_dir, callback, name)[source]

Bases: gpflow.monitor.tensorboard.ToTensorBoard

Stores the return value of a callback in a TensorBoard.

Methods

__call__(step, **kwargs)

It calls the ‘run’ function and sets the current step.

run(**kwargs)

Implements the task to be executed on __call__.

Parameters
  • log_dir (str) –

  • callback (Callable[[], float]) –

  • name (str) –

__init__(log_dir, callback, name)[source]
Parameters
  • log_dir (str) – directory in which to store the tensorboard files. For example, ‘./logs/my_run/’.

  • callback (Callable[[], float]) –

    callback to be executed and result written to TensorBoard. A callback can have arguments (e.g. data) passed to the function using keyword arguments. For example: ``` lambda cb(x=None): 2 * x task = ScalarToTensorBoard(logdir, cb, “callback”)

    # specify the argument of the function using kwargs, the names need to match. task(step, x=1) ```

  • name (str) – name used in TensorBoard.

run(**kwargs)[source]

Implements the task to be executed on __call__. The current step is available through self.current_step.

Parameters

kwargs – keyword arguments available to the run method.

gpflow.monitor.ToTensorBoard

class gpflow.monitor.ToTensorBoard(log_dir)[source]

Bases: gpflow.monitor.base.MonitorTask

Methods

__call__(step, **kwargs)

It calls the ‘run’ function and sets the current step.

run(**kwargs)

Implements the task to be executed on __call__.

Parameters

log_dir (str) –

__init__(log_dir)[source]
Parameters

log_dir (str) – directory in which to store the tensorboard files. Can be nested, e.g. ./logs/my_run/