garage.experiment.experiment

Tools for running experiments with Garage.

exp_count = 0
now
timestamp
EIGHT_MEBIBYTES
class ExperimentContext(*, snapshot_dir, snapshot_mode, snapshot_gap)

Context in which an experiment is being run.

Currently, this class implements the same interface as SnapshotConfig, but it will be extended in the future.

Parameters
  • snapshot_dir (str) – The full directory to put snapshots in.

  • snapshot_mode (str) – Policy for which snapshots to keep (or make at all). Can be either “all” (all iterations will be saved), “last” (only the last iteration will be saved), “gap” (every snapshot_gap iterations are saved), or “none” (do not save snapshots).

  • snapshot_gap (int) – Gap between snapshot iterations. Waits this number of iterations before taking another snapshot.

class ExperimentTemplate(*, function, log_dir, name, prefix, snapshot_mode, snapshot_gap, archive_launch_repo, name_parameters, use_existing_dir, x_axis)

Creates experiment log directories and runs an experiment.

This class should only be created by calling garage.wrap_experiment. Generally, it’s used as a decorator like this:

@wrap_experiment(snapshot_mode=’all’) def my_experiment(ctxt, seed, lr=0.5):

my_experiment(seed=1)

Even though this class could be implemented as a closure in wrap_experiment(), it’s more readable (and easier to pickle) implemented as a class.

Note that the full path that will be created is f’{data}/local/{prefix}/{name}’.

Parameters
  • function (callable or None) – The experiment function to wrap.

  • log_dir (str or None) – The full log directory to log to. Will be computed from name if omitted.

  • name (str or None) – The name of this experiment template. Will be filled from the wrapped function’s name if omitted.

  • prefix (str) – Directory under data/local in which to place the experiment directory.

  • snapshot_mode (str) – Policy for which snapshots to keep (or make at all). Can be either “all” (all iterations will be saved), “last” (only the last iteration will be saved), “gap” (every snapshot_gap iterations are saved), or “none” (do not save snapshots).

  • snapshot_gap (int) – Gap between snapshot iterations. Waits this number of iterations before taking another snapshot.

  • archive_launch_repo (bool) – Whether to save an archive of the repository containing the launcher script. This is a potentially expensive operation which is useful for ensuring reproducibility.

  • name_parameters (str or None) – Parameters to insert into the experiment name. Should be either None (the default), ‘all’ (all parameters will be used), or ‘passed’ (only passed parameters will be used). The used parameters will be inserted in the order they appear in the function definition.

  • use_existing_dir (bool) – If true, (re)use the directory for this experiment, even if it already contains data.

  • x_axis (str) – Key to use for x axis of plots.

wrap_experiment(function=None, *, log_dir=None, prefix='experiment', name=None, snapshot_mode='last', snapshot_gap=1, archive_launch_repo=True, name_parameters=None, use_existing_dir=False, x_axis='TotalEnvSteps')

Decorate a function to turn it into an ExperimentTemplate.

When invoked, the wrapped function will receive an ExperimentContext, which will contain the log directory into which the experiment should log information.

This decorator can be invoked in two differed ways.

Without arguments, like this:

@wrap_experiment def my_experiment(ctxt, seed, lr=0.5):

Or with arguments:

@wrap_experiment(snapshot_mode=’all’) def my_experiment(ctxt, seed, lr=0.5):

All arguments must be keyword arguments.

Parameters
  • function (callable or None) – The experiment function to wrap.

  • log_dir (str or None) – The full log directory to log to. Will be computed from name if omitted.

  • name (str or None) – The name of this experiment template. Will be filled from the wrapped function’s name if omitted.

  • prefix (str) – Directory under data/local in which to place the experiment directory.

  • snapshot_mode (str) – Policy for which snapshots to keep (or make at all). Can be either “all” (all iterations will be saved), “last” (only the last iteration will be saved), “gap” (every snapshot_gap iterations are saved), or “none” (do not save snapshots).

  • snapshot_gap (int) – Gap between snapshot iterations. Waits this number of iterations before taking another snapshot.

  • archive_launch_repo (bool) – Whether to save an archive of the repository containing the launcher script. This is a potentially expensive operation which is useful for ensuring reproducibility.

  • name_parameters (str or None) – Parameters to insert into the experiment name. Should be either None (the default), ‘all’ (all parameters will be used), or ‘passed’ (only passed parameters will be used). The used parameters will be inserted in the order they appear in the function definition.

  • use_existing_dir (bool) – If true, (re)use the directory for this experiment, even if it already contains data.

  • x_axis (str) – Key to use for x axis of plots.

Returns

The wrapped function.

Return type

callable

dump_json(filename, data)

Dump a dictionary to a file in JSON format.

Parameters
  • filename (str) – Filename for the file.

  • data (dict) – Data to save to file.

get_metadata()

Get metadata about the main script.

The goal of this function is to capture the additional information needed to re-run an experiment, assuming that the launcher script that started the experiment is located in a clean git repository.

Returns

  • Absolute path to root directory of launcher’s git repo.

  • Directory containing: * githash (str): Hash of the git revision of the repo the

    experiment was started from. “-dirty” will be appended to this string if the repo has uncommitted changes. May not be present if the main script is not in a git repo.

    • launcher (str): Relative path to the main script from the base of

      the repo the experiment was started from. If the main script was not started from a git repo, this will instead be an absolute path to the main script.

Return type

tuple[str, dict[str, str]]

make_launcher_archive(git_root_path, log_dir)

Saves an archive of the launcher’s git repo to the log directory.

Parameters
  • git_root_path (str) – Absolute path to git repo to archive.

  • log_dir (str) – Absolute path to the log directory.

class LogEncoder(*args, **kwargs)

Bases: json.JSONEncoder

Inheritance diagram of garage.experiment.experiment.LogEncoder

Encoder to be used as cls in json.dump.

Parameters
  • args (object) – Passed to super class.

  • kwargs (dict) – Passed to super class.

BLOCKED_MODULES
item_separator = ,
key_separator = :
default(self, o)

Perform JSON encoding.

Parameters

o (object) – Object to encode.

Raises

TypeError – If o cannot be turned into JSON even using repr(o).

Returns

Object encoded in JSON.

Return type

dict or str or float or bool

encode(self, o)

Return a JSON string representation of a Python data structure.

>>> from json.encoder import JSONEncoder
>>> JSONEncoder().encode({"foo": ["bar", "baz"]})
'{"foo": ["bar", "baz"]}'
iterencode(self, o, _one_shot=False)

Encode the given object and yield each string representation as available.

For example:

for chunk in JSONEncoder().iterencode(bigobject):
    mysocket.write(chunk)