Skip to main content

xpflow: nested loops as classes

Did you ever perform experiments by nesting loops like this ?

args = edict({'a':'A'})

for b in [1,2]:
    for lr in [1e-3, 2e-3]:
        args.lr = lr
        args.b = b
        # perform_experiment_and_logging(args)

This get messy when there are many loops. In addition, nested loops are not objects, so you cannot store them or share them. A better alternative is to represent experiments with dictionaries where some values are lists, e.g.:

learning_rate = {
    'a' : 'A',
    'b' : [1, 2],
    'lr' : [1e-3, 2e-3]
}

However, you have to write custom code to take care of the list values.

xpflow does that under the hood. Lists of values are used to denote multiple values to try for a given parameter. All combinations will be generated in the form of EasyDict objects. Nested loops become objects (classes).

from xpflow import Xp

for args in Xp(learning_rate):
    # perform_experiment_and_logging(args)

This allows a concise, readable, shareable, composable, and framework-agnostic formulation of experiments. You can also use classes instead of dictionaries. Classes are a bit less verbose (no commas, no quote on parameter names), they enforce tabulation, they are easier to read, extensible, and inheritence is cleaner.

class learning_rate(Xp):
    a = 'A'
    b = [1, 2]
    lr = [1e-3, 2e-3]
    
for args in learning_rate():
    # perform_experiment_and_logging(args)

Installation

pip install xpflow

or the last version with

pip install git+https://github.com/sileod/xpflow.git 

Usage

Just make sure that your experiment classes inherits the Xp class. Instanciating the class will provide an iterable yielding the possible value combinations. Lists of values will be used to generate the possible combinations. You can use a list of lists to represent values that should actually be lists.

from xpflow import Xp

class base(Xp):
    a='A'
    b=[1,2]

class learning_rate(base):
    lr = [1e-3, 2e-3]
    list_values = [[5, 6]]
    
for args in learning_rate():
    # perform_experiment_and_logging(args)
    print(args.a, args.b, args.lr, args.list_values)

will print the following output:

A 1 0.001 [5, 6]
A 1 0.002 [5, 6]
A 2 0.001 [5, 6]
A 2 0.002 [5, 6]

Other specific use cases:

Sequential experiments

for args in xp1() + xp2()
    # perform_experiment_and_logging(args)

Distributing computations across processes

You can easily distribute the computations across processes by passing argparse arguments to your main script. The argument yielded by xpflow are deterministically hashable into integers (standard dict/edict are not hashable).

for args in xp():
    if hash(args) % argparse_args.number_of_processes != argparse_args.process_index:
        continue
    # perform_experiment_and_logging(args)

Random search

You can perform a random search by using lengthy lists of possible values and then randomly discarding parameter combinations.

class random_search_space(Xp):
    learning_rate=list(np.logspace(-6,-1,100))
    batch_size=[32,64,128,256]
    nb_epochs=[3,4,5]

for args in sorted(random_search_space(), key=hash)[:100]:
    # perform_experiment_and_logging(args)

Metadata

Release files for xpflow 0.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for xpflow 0.8
File Size Uploaded Provenance Actions
xpflow-0.8.tar.gz 4.5 kB Without provenance Details for xpflow-0.8.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for xpflow 0.8
File Interpreter ABI Platform Actions
xpflow-0.8-py3-none-any.whl Python 3 none any Details for xpflow-0.8-py3-none-any.whl

Total release size: 9.0 kB

Release files / xpflow-0.8.tar.gz

Download URL xpflow-0.8.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
1cc63aa4100041808c14f41d77de70accd42dd2abc303b263aff6f9e784a762a
BLAKE2b-256 checksum
How to use checksums
b0812160392efc7fcf5132e3b73ded03f365fe0a6a50c7543bc837af1b5ef796
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / xpflow-0.8-py3-none-any.whl

Download URL xpflow-0.8-py3-none-any.whl
Size 4.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ee0e0f876b1642e2f1e8bb82506f5312855e72b83f8c7678b15f8e200e8a0e37
BLAKE2b-256 checksum
How to use checksums
8339990979bf1d8e951b2d117610d407c1f3a6a03a3cc1a436f89071429bf964
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Security for xpflow 0.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page