How do I print the wandb sweep url in python?

For runs I do:

wandb.run.get_url()

how do I do the same but for sweeps given the sweep_id?


fulls sample run:

"""
Main Idea:
- create sweep with a sweep config & get sweep_id for the agents (note, this creates a sweep in wandb's website)
- create agent to run a setting of hps by giving it the sweep_id (that mataches the sweep in the wandb website)
- keep running agents with sweep_id until you're done

note:
    - Each individual training session with a specific set of hyperparameters in a sweep is considered a wandb run.

ref:
    - read: https://docs.wandb.ai/guides/sweeps
"""

import wandb
from pprint import pprint
import math
import torch

sweep_config: dict = {
    "project": "playground",
    "entity": "your_wanbd_username",
    "name": "my-ultimate-sweep",
    "metric":
        {"name": "train_loss",
         "goal": "minimize"}
    ,
    "method": "random",
    "parameters": None,  # not set yet
}

parameters = {
    'optimizer': {
        'values': ['adam', 'adafactor']}
    ,
    'scheduler': {
        'values': ['cosine', 'none']}  # todo, think how to do
    ,
    'lr': {
        "distribution": "log_uniform_values",
        "min": 1e-6,
        "max": 0.2}
    ,
    'batch_size': {
        # integers between 32 and 256
        # with evenly-distributed logarithms
        'distribution': 'q_log_uniform_values',
        'q': 8,
        'min': 32,
        'max': 256,
    }
    ,
    # it's often the case that some hps we don't want to vary in the run e.g. num_its
    'num_its': {'value': 5}
}
sweep_config['parameters'] = parameters
pprint(sweep_config)

# create sweep in wandb's website & get sweep_id to create agents that run a single agent with a set of hps
sweep_id = wandb.sweep(sweep_config)
print(f'{sweep_id=}')


def my_train_func():
    # read the current value of parameter "a" from wandb.config
    # I don't think we need the group since the sweep name is already the group
    run = wandb.init(config=sweep_config)
    print(f'{run=}')
    pprint(f'{wandb.config=}')
    lr = wandb.config.lr
    num_its = wandb.config.num_its

    train_loss: float = 8.0 + torch.rand(1).item()
    for i in range(num_its):
        # get a random update step from the range [0.0, 1.0] using torch
        update_step: float = lr * torch.rand(1).item()
        wandb.log({"lr": lr, "train_loss": train_loss - update_step})
    run.finish()


# run the sweep, The cell below will launch an agent that runs train 5 times, usingly the randomly-generated hyperparameter values returned by the Sweep Controller.
wandb.agent(sweep_id, function=my_train_func, count=5)

cross: machine learning - How do I print the wandb sweep url in python? - Stack Overflow

Hi @brando thank you for your question. Here is how you can get_sweep_url:

get_sweep_url() -> Optional[str]

You can find more in our docs. I hope this helps!

1 Like

like wandb.get_sweep_url()? Thanks!

Run | Weights & Biases Documentation#get_sweep_url

1 Like

@corey-strausman

Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/pydevd.py", line 1496, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/brandomiranda/ultimate-utils/tutorials_for_myself/my_wandb_uu/my_wandb_sweeps_uu/sweep_everything_in_python_even_config/sweep_everything_in_python.py", line 64, in <module>
    wandb.get_sweep_url()
AttributeError: module 'wandb' has no attribute 'get_sweep_url'

didn’t work even after pip install --upgrade wandb

(pycoq-ejgallego) brandomiranda~/pycoq-ejgallego ❯ pip install --upgrade wandb

Requirement already satisfied: wandb in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (0.13.10)
Collecting wandb
  Using cached wandb-0.14.0-py3-none-any.whl (2.0 MB)
Requirement already satisfied: typing-extensions in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from wandb) (4.5.0)
Requirement already satisfied: protobuf!=4.21.0,<5,>=3.19.0 in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from wandb) (4.22.0)
Requirement already satisfied: docker-pycreds>=0.4.0 in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from wandb) (0.4.0)
Requirement already satisfied: GitPython!=3.1.29,>=1.0.0 in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from wandb) (3.1.31)
Requirement already satisfied: sentry-sdk>=1.0.0 in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from wandb) (1.15.0)
Requirement already satisfied: Click!=8.0.0,>=7.0 in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from wandb) (8.1.3)
Requirement already satisfied: setuptools in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from wandb) (65.6.3)
Requirement already satisfied: PyYAML in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from wandb) (6.0)
Requirement already satisfied: pathtools in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from wandb) (0.1.2)
Requirement already satisfied: setproctitle in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from wandb) (1.3.2)
Requirement already satisfied: requests<3,>=2.0.0 in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from wandb) (2.28.2)
Requirement already satisfied: psutil>=5.0.0 in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from wandb) (5.9.4)
Requirement already satisfied: appdirs>=1.4.3 in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from wandb) (1.4.4)
Requirement already satisfied: six>=1.4.0 in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from docker-pycreds>=0.4.0->wandb) (1.16.0)
Requirement already satisfied: gitdb<5,>=4.0.1 in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from GitPython!=3.1.29,>=1.0.0->wandb) (4.0.10)
Requirement already satisfied: idna<4,>=2.5 in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from requests<3,>=2.0.0->wandb) (3.4)
Requirement already satisfied: charset-normalizer<4,>=2 in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from requests<3,>=2.0.0->wandb) (3.0.1)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from requests<3,>=2.0.0->wandb) (1.26.14)
Requirement already satisfied: certifi>=2017.4.17 in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from requests<3,>=2.0.0->wandb) (2022.12.7)
Requirement already satisfied: smmap<6,>=3.0.1 in /Users/brandomiranda/opt/anaconda3/envs/pycoq-ejgallego/lib/python3.9/site-packages (from gitdb<5,>=4.0.1->GitPython!=3.1.29,>=1.0.0->wandb) (5.0.0)
Installing collected packages: wandb
  Attempting uninstall: wandb
    Found existing installation: wandb 0.13.10
    Uninstalling wandb-0.13.10:
      Successfully uninstalled wandb-0.13.10
Successfully installed wandb-0.14.0

Hi @brando, you can get the sweep id by using our public API like:

import wandb
api = wandb.Api()
sweep = api.sweep('entity/project/sweep_id')
sweep.url

Please let me know if this works!

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.