Search Space and Decorator

This tutorial explains the supported search spaces and how to use them, including simple search spaces (Int, Real, and Categorical) and nested search spaces (Categorical, List, Dict). Each search space describes the set of possible values for a hyperparameter, from which the searcher will try particular values during hyperparameter optimization. AutoGluon also enables search spaces in user-defined objects using the decorator ag.obj and user-defined functions using the decorator ag.func.

Search Space

Simple Search Space

import autogluon.core as ag

Integer Space

An integer is chosen between lower and upper value during the searcher sampling.

a =, upper=10)
Int: lower=0, upper=10

Get default value:


Change default value, which is the first configuration that a random searcher autogluon.core.searcher.RandomSearcher will try:

a =, upper=10, default=2)

Pick a random value.


Real Space

A real number is chosen between lower and upper value during the searcher sampling.

b =, upper=1e-2)
Real: lower=0.0001, upper=0.01

Real space in log scale:

c =, upper=1e-2, log=True)
Real: lower=0.0001, upper=0.01

Categorical Space

Categorical Space chooses one value from all the possible values during the searcher sampling.

d ='Monday', 'Tuesday', 'Wednesday')
Categorical['Monday', 'Tuesday', 'Wednesday']

Nested Search Space

Categorical Space

Categorical Space can also be used as a nested search space. For an example, see NestedExampleObj_.

List Space

List Space returns a list of sampled results.

In this example, the first element of the list is a Int Space sampled from 0 to 3, and the second element is a Categorical Space sampled from the choices of 'alpha' and 'beta'.

f =, 3),'alpha', 'beta'),
List[Int: lower=0, upper=3, Categorical['alpha', 'beta']]

Get one example configuration:

[2, 'beta']

Dict Space

Dict Space returns a dict of sampled results.

Similar to List Space, the resulting configuraton of Dict is a dict. In this example, the value of 'key1' is sampled from a Categorical Space with the choices of 'alpha' and 'beta', and the value of 'key2' is sampled from an Int Space between 0 and 3.

g ='alpha', 'beta'),, 3),
Dict{'key1': Categorical['alpha', 'beta'], 'key2': Int: lower=0, upper=3, 'key3': 'constant'}

Get one example configuration:

{'key1': 'beta', 'key2': 1, 'key3': 'constant'}

Decorators for Searchbale Object and Customized Training Scripts

In this section, we show how to insert search space into customized objects and training functions.

Searchable Space in Customized Class autogluon.obj()

In AutoGluon searchable object can be returned by a user defined class with a decorator.

@ag.obj('auto', 'gluon'),
    static_value=10,, 5),
class MyObj:
    def __init__(self, name, rank, static_value): = name
        self.rank = rank
        self.static_value = static_value
    def __repr__(self):
        repr = 'MyObj -- name: {}, rank: {}, static_value: {}'.format(
      , self.rank, self.static_value)
        return repr
h = MyObj()
AutoGluonObject -- MyObj

Get one example random object:

MyObj -- name: gluon, rank: 2, static_value: 10

We can also use it within a Nested Space such as In this example, the resulting nested space will be sampled from:

nested =
      'a', 'b'),

Categorical[Dict{'obj1': '1', 'obj2': Categorical['a', 'b']}, AutoGluonObject -- MyObj]

Get an example output:

for _ in range(5):
    result = nested.rand
    assert (isinstance(result, dict) and result['obj2'] in ['a', 'b']) or hasattr(result, 'name')
MyObj -- name: gluon, rank: 3, static_value: 10
MyObj -- name: auto, rank: 5, static_value: 10
{'obj1': '1', 'obj2': 'a'}
{'obj1': '1', 'obj2': 'a'}
{'obj1': '1', 'obj2': 'a'}

Searchable Space in Customized Function autogluon.obj()

We can also insert a searchable space in a customized function:

@ag.func('mxnet', 'pytorch'),
def myfunc(framework):
    return framework
i = myfunc()

We can also put a searchable space inside a nested space:

j =
        a=ag.Real(0, 10),
Dict{'a': Real: lower=0, upper=10, 'obj1': AutoGluonObject -- MyObj, 'obj2': AutoGluonObject}

Customized Train Script Using autogluon.args()

train_func is where to put your model training script, which takes in various keyword args as its hyperparameters and reports the performance of the trained model using the provided reporter. Here, we show a dummy train_func that simply prints these objects.

@ag.args(, 10),, 1e-2),, 1e-2, log=True),'a', 'b', 'c', 'd'),,
  , 2),
  , 5),
            a=ag.Real(0, 10),
        ),'test', MyObj()),
    i = myfunc(),
def train_fn(args, reporter):
    print('args: {}'.format(args))

Create Searcher and Sample a Configuration

In this section, we create a Searcher object, which orchestrates a particular hyperparameter-tuning strategy.

searcher = ag.searcher.RandomSearcher(train_fn.cs)
config = searcher.get_config()
{'a': 6, 'b': 0.0055, 'c': 0.0031622777, 'd▁choice': 0, 'e': 0, 'f▁0': 2, 'f▁1▁choice': 0, 'g▁a': 5.0, 'g▁obj▁name▁choice': 0, 'g▁obj▁rank': 4, 'h▁1▁name▁choice': 0, 'h▁1▁rank': 4, 'h▁choice': 0, 'i▁framework▁choice': 0}
train_fn(train_fn.args, config)
args: {'a': 6, 'b': 0.0055, 'c': 0.0031622777, 'd': 'a', 'e': 0, 'f': [2, 4], 'g': {'a': 5.0, 'obj': MyObj -- name: auto, rank: 4, static_value: 10}, 'h': 'test', 'i': 'mxnet', '_default_config': {}}