Skip to content

Optimizer

The base Optimizer class, defines the API we require optimizers to implement.

  • ask() - Ask the optimizer for a new Trial to evaluate.
  • tell() - Tell the optimizer the result of the sampled config. This comes in the form of a Trial.Report.

Additionally, to aid users from switching between optimizers, the preferred_parser() method should return either a parser function or a string that can be used with node.search_space(parser=..._) to extract the search space for the optimizer.

class Optimizer(metrics, bucket=None) #

Bases: Generic[I]

An optimizer protocol.

An optimizer is an object that can be asked for a trail using ask and a tell to inform the optimizer of the report from that trial.

PARAMETER DESCRIPTION
metrics

The metrics to optimize.

TYPE: Sequence[Metric]

bucket

The bucket to store results of individual trials from this optimizer.

TYPE: PathBucket | None DEFAULT: None

Source code in src/amltk/optimization/optimizer.py
def __init__(
    self,
    metrics: Sequence[Metric],
    bucket: PathBucket | None = None,
) -> None:
    """Initialize the optimizer.

    Args:
        metrics: The metrics to optimize.
        bucket: The bucket to store results of individual trials from this
            optimizer.
    """
    super().__init__()
    self.metrics = metrics
    self.bucket = (
        bucket
        if bucket is not None
        else PathBucket(f"{self.__class__.__name__}-{datetime.now().isoformat()}")
    )

metrics: Sequence[Metric]
attr
#

The metrics to optimize.

bucket: PathBucket
attr
#

The bucket to give to trials generated by this optimizer.

def tell(report)
abstractmethod
#

Tell the optimizer the report for an asked trial.

PARAMETER DESCRIPTION
report

The report for a trial

TYPE: Report[I]

Source code in src/amltk/optimization/optimizer.py
@abstractmethod
def tell(self, report: Trial.Report[I]) -> None:
    """Tell the optimizer the report for an asked trial.

    Args:
        report: The report for a trial
    """

def ask()
abstractmethod
#

Ask the optimizer for a trial to evaluate.

RETURNS DESCRIPTION
Trial[I]

A config to sample.

Source code in src/amltk/optimization/optimizer.py
@abstractmethod
def ask(self) -> Trial[I]:
    """Ask the optimizer for a trial to evaluate.

    Returns:
        A config to sample.
    """
    ...

def preferred_parser()
classmethod
#

The preferred parser for this optimizer.

Note

Subclasses should override this as required.

Source code in src/amltk/optimization/optimizer.py
@classmethod
def preferred_parser(
    cls,
) -> str | Callable[Concatenate[Node, ...], Any] | Callable[[Node], Any] | None:
    """The preferred parser for this optimizer.

    !!! note

        Subclasses should override this as required.

    """
    return None