Does your experiment spark joy?
Project description
Kondo
The name is inspired by Marie Kondo's tidying adventures.
Throw away experiments that don't spark joy with this tiny framework agnostic module.
Installation
PyPI
pip install kondo
Source
pip install git+https://github.com/activatedgeek/kondo.git@master
NOTE: Prefer pinning to a reference than the master branch for unintended updates.
Usage
-
Create new
Experiment
classfrom kondo import Experiment, RandIntType, ChoiceType class MyExp(Experiment): def __init__(self, foo=100, bar='c', **kwargs): super().__init__(**kwargs) self.foo = foo self.bar = bar def run(self): print('Running experiment with foo={}, bar={}!'.format(self.foo, self.bar)) @property def spec_list(self): return [ ('def', dict(foo=RandIntType(low=10, high=100), bar=ChoiceType(['a', 'b', 'c']))) ]
Make sure to capture all keyword arguments to the super class using
**kwargs
as above. -
Create
Hyperparameter
specfrom kondo import HParams hparams = HParams(MyExp)
HParams
class automagically recognizes all the possible parameters to the experiment specified as arguments to the constructor with default values. Thespec
can be any key value pairs (and can include constant values which will remain common across all trials).Other types available can be seen in param_types.py.
-
Generate trials and create a new experiment each time
for trial in hparams.trials(num=3): exp = MyExp(**trial) exp.run()
A sample output for these three trials with randomly selected values for
foo
andbar
is shown below. Each line represents the dictionary sent in to the constructor of theMyExp
class.Running experiment with foo=93, bar="b". Running experiment with foo=30, bar="c". Running experiment with foo=75, bar="c".
-
We can alternatively save this configurations for later use and load the experiment later. We extend the above example by making the following calls
# Save trials instead of running trials_dir = os.path.join(os.path.dirname(__file__), '.trials') hparams.save_trials(trials_dir, num=3)
We then load all of the saved trials from the
YAML
files.for fname in os.listdir(trials_dir): fname = os.path.join(trials_dir, fname) trial = MyExp.load(fname) trial.run()
Now, you can keep tuning the spec during your hyperparameter search and throw away the ones that don't spark joy!.
The full example file is available at basic.py.
License
Apache 2.0
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.