Skip to main content

Hypergrid enables concise declaration of parameter grids for hyperparameter optimization and batch jobs.

Project description

hypergrid

Hypergrid enables concise declaration and manipulation of parameter grid spaces, with an aim towards use cases such as hyperparameter tuning or defining large batch jobs.

Use the following features to lazily declare a parameter grid:

  • Dimension and Grid direct instantiation
  • + and | for "sum" or "union" types (concatenation)
  • * for "product" types
  • & for coiteration (zip)
  • select to project dimensions by name

There are also a few transformations that can be lazily applied element-wise, which take a GridElement (a namedtuple of dimension<->value) as input.

  • filter to apply boolean predicate
  • map for lambda transformation
  • map_to for map + concat

Once a parameter grid is declared, there are two ways to "materialize" your grid, which return GridElements.

  • __iter__: a grid is directly iterable
  • sample: allows you to sample from the grid according to a sampling strategy

Usage Examples

from hypergrid.dsl import *
from dataclasses import dataclass

# First, we need to create a Dimension, which is essentially a named, finite, 1-d collection
d = Dimension(custom_name=[1, 2, 3])        # any python Collection will work - set, dict, range(), etc.
assert d.name == "custom_name"              # the argument name is used as the dimension's name.
d.with_name("ints")                         # which you can reset
Uniform(low=1, high=5).take(5)              
ExponentialStep(start=1, step=1.1).take(6)  # You can also take a dimension from a Distribution or HIterable

# You can `len(d)` or `[i for i in d]`, but grids are more interesting
g = d.to_grid()
i2d = Dimension(ints=[4, 5, 6])
cd = Dimension(chars=["a", "b", "c", "d"])
union_ints = g + i2d     # result is length 6: Concatenate two grids that have the same underlying dimensions
product_g = g * cd       # result is length 12: tuples (1, "a") - take the cartesian product of the underlying grids
zip_g = g & cd           # result is length 3: tuples (1, "a") - zip two grids together, up to the shorter grid

ml = [i for i in zip_g]  # You can iterate through a grid
tl = zip_g.take(5)       # or you can just take up to a certain number of grid elements from it
print(tl[0].ints), print(tl[0].chars)  # The iterator elements are python NamedTuples taken from the dimension names.

# These gridelements can be referenced and used in the grid higher-order functions
zip_g.filter(lambda ge: ge.chars in ["a", "b"])    # result is length 2: keep the tuples (1, "a") and (2, "b")
zip_g.map(doubled=lambda ge: ge.ints * 2)          # result is length 3, with single attribute (drops `ints` and `chars`)
mt = zip_g.map_to(doubled=lambda ge: ge.ints * 2)  # result is length 3, appends `doubled` and keeps `ints` and `chars`
print(mt.select("doubled", "ints").take(1)[0])     # resulting gridelement no longer has `chars`

# There are some other utility methods on a grid:
zip_g.sample()                                     # Randomly samples a single grid element from a grid
zip_g.to_sklearn()                                 # The Grid.to_* methods convert HyperGrids to other grid formats

# The general idea is to allow for fairly extensive grid construction routines
@dataclass
class FakeModel:
    idx: int
    param1: float

g = HyperGrid(  # A grid with 4 x 10 combinations
    ExponentialStep(start=1.0, step=1.5).take(4).with_name("param1"),
    idx=range(10)
).instantiate(model=FakeModel).select("model") + \
    HyperGrid(  # A different grid with 15 combinations
        Uniform(low=-1, high=1).take(5).with_name("param1"),
        idx=[10, 20, 30]        
    ).instantiate(model=FakeModel).select("model")
assert len(g) == 55
g.sample()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hypergrid-0.0.1a2.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hypergrid-0.0.1a2-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file hypergrid-0.0.1a2.tar.gz.

File metadata

  • Download URL: hypergrid-0.0.1a2.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for hypergrid-0.0.1a2.tar.gz
Algorithm Hash digest
SHA256 4b02d347dc9d4962db938431c6c368c1ca556f2e7b22459c6c807682808ca34b
MD5 92e0b01fdb2f856931f2ab9b979d9ee9
BLAKE2b-256 aac9946a122302dd1ad6b2d59baa8ba498db4c43d76ab38759818943b881379f

See more details on using hashes here.

File details

Details for the file hypergrid-0.0.1a2-py3-none-any.whl.

File metadata

  • Download URL: hypergrid-0.0.1a2-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for hypergrid-0.0.1a2-py3-none-any.whl
Algorithm Hash digest
SHA256 b134a7626428b884ca489ef507950cddec0ae578056d672aae090b6b880bfedf
MD5 60b89e3d9e4ae5ca833c9485396587dd
BLAKE2b-256 14f6d98d3337a83e689bf8acabb7ee7c11bf82a725ddbd808d4e65722542db22

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page