jina.optimizers package

Submodules

Module contents

class jina.optimizers.OptimizerCallback[source]

Bases: jina.jaml.JAMLCompatible

Callback interface for storing and calculating evaluation metric during an optimization. Should be used, whenever a custom evaluation aggregation during an Flow optimization is needed.

get_empty_copy()[source]

Return an empty copy of the OptimizerCallback.

Raises

NotImplementedErrorOptimizerCallback is just an interface. Please use any implemented subclass.

Return type

OptimizerCallback

get_final_evaluation()[source]

Return the aggregation of all evaluation collected via :method:`__call__`

Raises

NotImplementedErrorOptimizerCallback is just an interface. Please use any implemented subclass.

Return type

float

class jina.optimizers.EvaluationCallback(eval_name=None, operation='mean')[source]

Bases: jina.optimizers.OptimizerCallback

Calculates an aggregation of all evaluations during a single FlowRunner execution from the FlowOptimizer with a given operator.

Valid operators are: [‘min’, ‘max’, ‘mean’, ‘median’, ‘sum’, ‘prod’]

Parameters
  • eval_name (Optional[str]) – evaluation name as required by the evaluator. Not needed if only 1 evaluator is used

  • operation (str) – name of the operation to be performed when getting the final evaluation

AGGREGATE_FUNCTIONS = ['min', 'max', 'mean', 'median', 'sum', 'prod']
get_empty_copy()[source]

Return an empty copy of the EvaluationCallback.

Returns

Evaluation values

get_final_evaluation()[source]

Calculates and returns evaluation value on the metric defined in the :method:`__init__`.

Return:

The aggregation of all evaluation collected via :method:`__call__`

class jina.optimizers.ResultProcessor(study)[source]

Bases: jina.jaml.JAMLCompatible

Result processor for the Optimizer.

Parameters

study (optuna.study.Study) – optuna study object

property study
Return:

Raw optuna study as calculated by the FlowOptimizer.

property best_parameters
Return:

The parameter set, which got the best evaluation result during the optimization.

save_parameters(filepath='config/best_config.yml')[source]

Stores the best parameters in the given file.

Parameters

filepath (str) – path where the best parameter config will be saved

class jina.optimizers.FlowOptimizer(flow_runner, parameter_yaml, evaluation_callback, n_trials, workspace_base_dir='.', sampler='TPESampler', direction='maximize', seed=42)[source]

Bases: jina.jaml.JAMLCompatible

Optimizer runs the given flows on multiple parameter configurations in order to find the best performing parameters. Uses optuna behind the scenes. For a detailed information how the parameters are sampled by optuna see https://optuna.readthedocs.io/en/stable/reference/generated/optuna.trial.Trial.html

Parameters
  • flow_runner (FlowRunner) – FlowRunner object which contains the flows to be run.

  • parameter_yaml (str) – yaml container the parameters to be optimized

  • evaluation_callback (OptimizerCallback) – The callback object, which stores the evaluation results

  • n_trials (int) – evaluation trials to be run

  • workspace_base_dir (str) – directory in which all temporary created data should be stored

  • sampler (str) – The optuna sampler. For a list of usable names see: https://optuna.readthedocs.io/en/stable/reference/samplers.html

  • direction (str) – direction of the optimization from either of maximize or minimize

  • seed (int) – random seed for reproducibility

optimize_flow(**kwargs)[source]

Will run the actual optimization.

Parameters

kwargs – extra parameters for optuna sampler

Return type

ResultProcessor

Return:

The aggregated result of the optimization run as a ResultProcessor.

jina.optimizers.run_optimizer_cli(args)[source]

Used to run the FlowOptimizer from command line interface.

Parameters

args (Namespace) – arguments passed via cli