autopartial

class autopartial(autofunc, /, *args, **kwds)

A functools.partial()-like wrapper that accepts Hyperparameter arguments and automatically builds a ConfigurationSpace covering all of them.

When called with a SMAC3 Configuration object (and an optional prefix string), it resolves every hyperparameter to its sampled value and invokes the wrapped callable.

Parameters:
  • autofunc (callable) – The function or class to wrap. May itself be an autopartial instance, in which case configuration spaces are merged.

  • args – Positional hyperparameters — each must be a Hyperparameter or an autopartial instance.

  • kwds – Keyword hyperparameters — same constraint as args.

Raises:
  • TypeError – If autofunc is not callable.

  • ValueError – If any argument is not a Hyperparameter or autopartial.

Usage pattern

from ConfigSpace.hyperparameters import UniformFloatHyperparameter
from src.core import autopartial

lr_hp     = UniformFloatHyperparameter("lr", 1e-4, 0.1, log=True)
margin_hp = UniformFloatHyperparameter("margin", 0.5, 1.5)

wrapped_optimizer = autopartial(MyOptimizer, lr=lr_hp, margin=margin_hp)

# wrapped_optimizer.cs  →  ConfigurationSpace with lr and margin
# Call with a sampled configuration:
optimizer = wrapped_optimizer(some_smac_config)

Nesting autopartial instances

autopartial instances can be nested. The inner configuration spaces are merged into the outer one using a colon-delimited prefix:

inner = autopartial(dict, lr=lr_hp)
outer = autopartial(MyTrainer, optimizer_kwargs=inner)
# outer.cs contains "optimizer_kwargs:lr"

Attributes

Attribute

Description

autofunc

The underlying callable.

args

Tuple of positional Hyperparameter / autopartial objects.

kwds

Dict of keyword Hyperparameter / autopartial objects.

cs

The assembled ConfigurationSpace.

Methods

__call__(space, prefix='')

Resolve all hyperparameters from space and call autofunc.

Parameters:
  • space – A SMAC3 / ConfigSpace Configuration object.

  • prefix (str) – Namespace prefix used when this instance is nested inside another autopartial.

Returns:

The return value of autofunc(*resolved_args, **resolved_kwds).