This page documents W&B (Weights and Biases) hyperparameter sweeps in Protify: sweep config (sweep.yaml), HyperoptModule, apply_config and train_model, the run_wandb_hyperopt flow, and the CLI arguments. For probe and trainer options that can be swept, see Probes and training and Configuration.
When --use_wandb_hyperopt is set, the pipeline runs a W&B sweep over the parameters defined in the sweep YAML. For each (model, dataset) combination (skipping random/onehot), it runs multiple trials, selects the best config by the chosen metric, writes a CSV of results, and then runs one final training with the best config and logs that run to W&B.
- Sweep config is loaded from
full_args.sweep_config_path(defaultyamls/sweep.yaml). Required:parameters(W&B format). Optional:early_terminate(e.g. hyperband). - Parameters are filtered by probe type and LoRA: only keys in
linear_probe_paramsortransformer_probe_params(and optionallylora_params) are passed to the sweep; the rest are ignored for that run. - For each (model_name, data_name), embeddings are loaded,
num_labelsandtask_typeare set, and a HyperoptModule is created with the sweep config and a sharedresults_list. - W&B sweep is created:
wandb.sweep(sweep, project, entity)thenwandb.agent(sweep_id, function=hyperopt_module.objective, count=sweep_count). - HyperoptModule.objective() is the W&B trial entry:
wandb.init(project, entity, config=sweep_config, reinit=True);apply_config(wandb.config); optionally reload embeddings ifembedding_pooling_typesorembedding_hidden_state_indexchanged;train_model(sweep_mode=True); select metric viasweep_metric_clsorsweep_metric_regby task_type; log metrics to W&B; append to results_list; return metric value. - After the agent finishes, results_list is sorted by the chosen metric (max or min per
sweep_goal), and a CSV is written to log_dir:{random_id}_sweep_{data_name}_{model_name}.csvwith columns such as rank, wandb_run_id, metric, config, valid_metrics, test_metrics. - Best config is applied via
apply_config(best_config);make_plots=True; one final training run with the best config is executed, logged to W&B as a new run, then finished.
Defined in hyperopt_utils.py.
- Constructor:
HyperoptModule(main_process, model_name, data_name, dataset, emb_dict, sweep_config, results_list, swept_param_keys=None). Keeps deep copies of baseprobe_argsandtrainer_argsand defines which keys are probe/trainer/embedding and which are int-cast. - apply_config(cfg): Restores base probe and trainer args, then applies
cfg: legacyn_heads(deprecated) is migrated tohead_size = hidden_size // n_headswith aDeprecationWarning; assertshidden_size % head_size == 0; ints for int_keys;transformer_dropoutfromdropout;pooling_typesfromprobe_pooling_types; sets attributes onmp.probe_args,mp.trainer_args, and embedding options such asmp.embedding_args.pooling_typesandmp.embedding_args.hidden_state_indexwhere applicable. - train_model(sweep_mode=True): Dispatches to
_run_full_finetuning,_run_hybrid_probe, or_run_nn_probedepending onfull_args; returns (model/probe, valid_metrics, test_metrics). - select_metric(valid_metrics, test_metrics, sweep_metric): Returns the float value for
sweep_metricfrom valid or test; raises with available keys if missing. - objective(): W&B entry: init, apply_config, train_model, log metrics, append to results_list, return metric for the goal.
File: src/protify/yamls/sweep.yaml.
- Top-level:
parameters(required); optionalearly_terminate(e.g.{type: hyperband, min_iter: 10}). - parameters: W&B sweep format. Examples:
lr,weight_decay:distribution: log_uniform_values,min,max.hidden_size,n_layers,dropout,pre_ln: uniform or int_uniform.- Transformer-specific:
transformer_hidden_size,classifier_dropout,classifier_size,probe_pooling_types,embedding_pooling_types,embedding_hidden_state_index. - LoRA:
lora_r,lora_alpha,lora_dropout. - Trainer:
num_epochs,probe_batch_size,base_batch_size,probe_grad_accum,base_grad_accum,patience,seed.
Only parameters that are in the allowed sets (linear_probe_params, transformer_probe_params, lora_params, trainer_keys, embedding_keys) are applied; the rest are ignored for that probe type.
Example hidden-state sweep entry:
parameters:
embedding_hidden_state_index:
values: [-1, 3, 6]| Argument | Type | Default | Description |
|---|---|---|---|
--use_wandb_hyperopt |
flag | False | Run W&B hyperparameter sweep. |
--wandb_project |
str | (env/args) | W&B project name. |
--wandb_entity |
str | (env/args) | W&B entity. |
--sweep_config_path |
str | yamls/sweep.yaml | Path to sweep YAML. |
--sweep_count |
int | 10 | Number of trials per (model, dataset). |
--sweep_method |
choice | bayes | bayes, grid, random. |
--sweep_metric_cls |
str | eval_loss | Classification metric to optimize. |
--sweep_metric_reg |
str | eval_loss | Regression metric to optimize. |
--sweep_goal |
choice | minimize | maximize, minimize. |
Set WANDB_API_KEY or --wandb_api_key for authentication.
py -m src.protify.main --yaml_path src/protify/yamls/base.yaml --use_wandb_hyperopt --sweep_count 5 --wandb_project my_project --wandb_entity my_entitypy -m src.protify.main --data_names DeepLoc-2 --model_names ESM2-8 --use_wandb_hyperopt --sweep_metric_reg eval_spearman_rho --sweep_goal maximizepy -m src.protify.main --yaml_path src/protify/yamls/base.yaml --use_wandb_hyperopt --sweep_config_path my_sweep.yaml --sweep_count 20- Configuration for all W&B and sweep flags
- Probes and training for probe and trainer options that are swept
- Models and embeddings for embedding options (e.g. embedding_pooling_types, embedding_hidden_state_index)
- Logging and replay for where sweep CSV and results are written