config property of a run to save your training configuration:
- Hyperparameters.
- Input settings such as the dataset name or model type.
- Any other independent variables for your experiments.
wandb.Run.config property helps you analyze your experiments and reproduce your work. You can group by configuration values in the W&B App, compare the configurations of different W&B runs, and evaluate how each training configuration affects the output. The config property is a dictionary-like object that you can compose from multiple dictionary-like objects.
To save output metrics or dependent variables like loss and accuracy, use
wandb.Run.log() instead of wandb.Run.config.Set up an experiment configuration
You typically define configurations at the beginning of a training script. Machine learning workflows vary, so you don’t have to. You must use dashes (-) or underscores (_) instead of periods (.) in your config variable names.
If your script accesses wandb.Run.config keys below the root, you must use the dictionary access syntax ["key"]["value"] instead of the attribute access syntax config.key.value.
The following sections outline common scenarios for how to define your experiment’s configuration.
Set the configuration at initialization
Pass a dictionary at the beginning of your script when you call thewandb.init() API to start a background process that syncs and logs your data as a run.
The following code snippet demonstrates how to define a Python dictionary with configuration values and how to pass that dictionary as an argument when you initialize a run.
config, W&B flattens the names using dots.
Access the values from the dictionary similarly to how you access other dictionaries in Python:
Throughout the Developer Guide and examples, we copy the configuration values into separate variables. This step is optional and improves readability.
Set the configuration with argparse
You can set your configuration with an argparse object. argparse is a standard library module in Python 3.2 and above. Use it to write scripts that take advantage of command-line arguments. This approach is useful for tracking results from scripts that you launch from the command line. The following Python script demonstrates how to define a parser object to define and set your experiment config. The functionstrain_one_epoch and evaluate_one_epoch simulate a training loop for this demonstration:
Set the configuration throughout your script
You can add more parameters to your config object throughout your script. The following code snippet demonstrates how to add new key-value pairs to your config object:Set the configuration after your run has finished
If a run has already completed and you need to add or correct config values, use the W&B Public API to update the stored configuration. You must provide the API with your entity, project name, and the run’s ID. You can find these details in the run object or in the W&B App:Highlight config values
Pin important config keys to the References section at the top of a run’s overview page so that you can find them quickly when reviewing the run. Usewandb.Run.pin_config_keys to pin one or more config keys with the Python SDK.
For example, if you use a Grafana dashboard to monitor training runs, add the dashboard URL to your config and pin the grafana_url key:
Use absl flags
If your training script usesabsl flags for configuration, you can pass them directly into your run’s config.
File-based configs
If you prefer to keep configuration outside your Python code, you can load it from a YAML file. If you place a file namedconfig-defaults.yaml in the same directory as your run script, the run automatically picks up the key-value pairs defined in the file and passes them to wandb.Run.config.
The following code snippet shows a sample config-defaults.yaml YAML file:
config-defaults.yaml by setting updated values in the config argument of wandb.init(). For example:
config-defaults.yaml, use the --configs command-line argument and specify the path to the file:
Example use case for file-based configs
Suppose you have a YAML file with some metadata for the run, and then a dictionary of hyperparameters in your Python script. You can save both in the nestedconfig object:
TensorFlow v1 flags
If you use TensorFlow v1, you can pass TensorFlow flags into thewandb.Run.config object directly.