opto.trainer.algorithms.beamsearch_algorithm¶
BeamsearchAlgorithm ¶
Bases: MinibatchAlgorithm
BeamsearchAlgorithm performs beam search over parameter space.
It starts with an initial prompt, generates multiple candidates, selects top beam_width candidates, and repeats this process up to max_depth. At each step, it evaluates candidates on a validation set to select the best ones. Finally output the best candidate based on validation scores.
train ¶
train(
guide,
train_dataset,
*,
validate_dataset=None,
validate_guide=None,
validation_dataset_size=5,
beam_width=3,
num_proposals=4,
max_depth=2,
num_epochs=1,
batch_size=1,
test_dataset=None,
log_frequency=None,
save_frequency=None,
save_path="checkpoints/agent.pkl",
min_score=None,
num_threads=10,
test_frequency=4,
**kwargs
)
Performs beam search to find optimal parameters.
Args: beam_width: Number of candidates to keep at each level of the beam search num_proposals: Number of proposals to generate per beam candidate max_depth: Maximum depth of the beam search validate_dataset: Dataset used to select the best candidates validate_guide: Guide used for validation validation_dataset_size: Size of validation minibatch for each evaluation (if None, uses all) test_frequency: How often to evaluate on test set (every N steps) Other parameters are the same as MinibatchAlgorithm.train()
expand ¶
expand(
beam_params: Dict,
beam_idx: int,
guide,
train_dataset,
batch_size: int,
num_proposals: int,
num_threads: int = None,
) -> List[Dict]
Expands a single candidate into multiple proposals without evaluation.
Args: beam_params: Parameters of the current beam beam_idx: Index of the current beam guide: Guide for generating feedback train_dataset: Training dataset batch_size: Training batch size num_proposals: Number of proposals to generate num_threads: Number of threads to use
Returns: List of parameter dictionaries for each candidate
select ¶
select(
candidates: List[Dict],
validate_guide,
validation_mini_dataset,
beam_width: int,
num_threads: int = None,
min_score: float = None,
return_scores: bool = False,
) -> Union[List[Dict], Tuple[List[Dict], List[float]]]
Evaluates all candidates and selects the top beam_width candidates based on validation scores.
Args: candidates: List of parameter dictionaries for each candidate validate_guide: Guide for validation validation_mini_dataset: Validation dataset for evaluation beam_width: Maximum number of candidates to select num_threads: Number of threads to use min_score: Minimum score when errors occur return_scores: Whether to return scores along with parameters
Returns: If return_scores is False: List of selected candidates' parameters If return_scores is True: Tuple of (list of parameters, list of scores)
BeamsearchHistoryAlgorithm ¶
BeamsearchHistoryAlgorithm(
agent,
optimizer,
num_threads: int = None,
logger=None,
*args,
**kwargs
)
Bases: BeamsearchAlgorithm
BeamsearchHistoryAlgorithm enhances BeamsearchAlgorithm by incorporating historical parameter-score information into the proposal generation process.
It maintains a log of previously selected parameter sets and their validation scores.
This history is then formatted and provided as additional context (feedback)
during the expand
phase, aiming to guide the optimizer towards generating
more informed proposals based on past performance.
train ¶
train(
guide,
train_dataset,
*,
validate_dataset=None,
validate_guide=None,
validation_dataset_size=5,
beam_width=3,
batch_size=1,
num_proposals=1,
max_depth=2,
num_threads=10,
max_history_size=10,
test_frequency=5,
**kwargs
)
Performs beam search enhanced with parameter history.
Args: max_history_size: Maximum number of (parameter, score) pairs to store in the history log. Defaults to 20. top_k: Size of the top-k candidates buffer that persists across depths. Default is 1, which keeps only the best candidate. Other args are the same as BeamsearchAlgorithm.train()
expand ¶
expand(
beam_params: Dict,
beam_idx: int,
guide,
train_dataset,
batch_size: int,
num_proposals: int,
num_threads: int = None,
) -> List[Dict]
Expands a single candidate into multiple proposals, incorporating history.
Overrides the parent expand method to augment the feedback provided to the optimizer with a summary of historical parameter-score pairs.
Args: Same as parent expand method.
Returns: Same as parent expand method.