如何在Python中获取并打印Wandb Sweep的URL?
获取Wandb Sweep的URL方法
已知在Wandb中可以通过wandb.run.get_url()获取单个Run实例的URL,若已持有sweep_id,可通过以下两种方式在Python中获取并打印对应的Sweep URL:
方法一:通过Sweep对象调用API获取
创建Sweep后,通过wandb.Sweep类实例化得到对应Sweep对象,再调用其get_url()方法即可,逻辑和获取Run URL一致。
方法二:手动拼接URL
根据Wandb的URL规则,直接用已知的entity、project和sweep_id拼接出完整URL,格式为:https://wandb.ai/{entity}/{project}/sweeps/{sweep_id}
完整示例代码
""" 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 matches 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 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']} , 'lr': { "distribution": "log_uniform_values", "min": 1e-6, "max": 0.2} , 'batch_size': { 'distribution': 'q_log_uniform_values', 'q': 8, 'min': 32, 'max': 256, } , 'num_its': {'value': 5} } sweep_config['parameters'] = parameters pprint(sweep_config) # 创建sweep并获取sweep_id sweep_id = wandb.sweep(sweep_config) print(f'{sweep_id=}') # 方法1:通过Sweep对象获取URL sweep = wandb.Sweep(sweep_id, entity=sweep_config["entity"], project=sweep_config["project"]) sweep_url = sweep.get_url() print(f"Sweep URL: {sweep_url}") # 方法2:手动拼接URL manual_sweep_url = f"https://wandb.ai/{sweep_config['entity']}/{sweep_config['project']}/sweeps/{sweep_id}" print(f"手动拼接的Sweep URL: {manual_sweep_url}") def my_train_func(): 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): update_step: float = lr * torch.rand(1).item() wandb.log({"lr": lr, "train_loss": train_loss - update_step}) run.finish() # 启动sweep agent,执行5次训练 wandb.agent(sweep_id, function=my_train_func, count=5)
内容的提问来源于stack exchange,提问作者Charlie Parker
相关产品推荐
相关产品推荐

