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:
- PyTorch
- Numpy
- 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]
-
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’