lagom.utils: Utils

lagom.utils.set_global_seeds(seed)[source]

Set the seed for generating random numbers.

It sets the following dependencies with the given random seed:

  1. PyTorch
  2. Numpy
  3. Python random
Parameters:seed (int) – a given seed.
class lagom.utils.Seeder(init_seed=0)[source]

A random seed generator.

Given an initial seed, the seeder can be called continuously to sample a single or a batch of random seeds.

Note

The seeder creates an independent RandomState to generate random numbers. It does not affect the RandomState in np.random.

Example:

>>> seeder = Seeder(init_seed=0)
>>> seeder(size=5)
[209652396, 398764591, 924231285, 1478610112, 441365315]
__call__(size=1)[source]

Return the sampled random seeds according to the given size.

Parameters:size (int or list) – The size of random seeds to sample.
Returns:seeds – a list of sampled random seeds.
Return type:list
__init__(init_seed=0)[source]

Initialize the seeder.

Parameters:init_seed (int, optional) – Initial seed for generating random seeds. Default: 0.
lagom.utils.tensorify(x, device)[source]
lagom.utils.numpify(x, dtype)[source]
lagom.utils.pickle_dump(obj, f, ext='.pkl')[source]

Serialize an object using pickling and save in a file.

Note

It uses cloudpickle instead of pickle to support lambda function and multiprocessing. By default, the highest protocol is used.

Note

Except for pure array object, it is not recommended to use np.save because it is often much slower.

Parameters:
  • obj (object) – a serializable object
  • f (str/Path) – file path
  • ext (str, optional) – file extension. Default: .pkl
lagom.utils.pickle_load(f)[source]

Read a pickled data from a file.

Parameters:f (str/Path) – file path
lagom.utils.yaml_dump(obj, f, ext='.yml')[source]

Serialize a Python object using YAML and save in a file.

Note

YAML is recommended to use for a small dictionary and it is super human-readable. e.g. configuration settings. For saving experiment metrics, it is better to use pickle_dump().

Note

Except for pure array object, it is not recommended to use np.load because it is often much slower.

Parameters:
  • obj (object) – a serializable object
  • f (str/Path) – file path
  • ext (str, optional) – file extension. Default: .yml
lagom.utils.yaml_load(f)[source]

Read the data from a YAML file.

Parameters:f (str/Path) – file path
lagom.utils.color_str(string, color, attribute=None)[source]

Returns stylized string with color and attribute for printing.

Example:

>>> print(color_str('lagom', 'green', attribute='bold'))

See colored documentation for more details.

Parameters:
  • string (str) – input string
  • color (str) – color name
  • attribute (str, optional) – attribute. Default: None
Returns:

out – stylized string

Return type:

str

lagom.utils.timed(color='green', attribute='bold')[source]

A decorator to print the total time of executing a body function.

Parameters:
  • color (str, optional) – color name. Default: ‘green’
  • attribute (str, optional) – attribute. Default: ‘bold’
lagom.utils.timeit(_func=None, *, color='green', attribute='bold')[source]
lagom.utils.ask_yes_or_no(msg)[source]

Ask user to enter yes or no to a given message.

Parameters:msg (str) – a message
class lagom.utils.CloudpickleWrapper(x)[source]

Uses cloudpickle to serialize contents (multiprocessing uses pickle by default)

This is useful when passing lambda definition through Process arguments.