def sweep(sweep, entity=None, project=None): from wandb.sweeps.config import SweepConfig import types if isinstance(sweep, types.FunctionType): sweep = sweep() if isinstance(sweep, SweepConfig): sweep = dict(sweep) """Sweep create for controller api and jupyter (eventually for cli).""" in_jupyter = wandb._get_python_type() != "python" if in_jupyter: os.environ[env.JUPYTER] = "true" _api0 = InternalApi() if not _api0.api_key: wandb._jupyter_login(api=_api0) if entity: env.set_entity(entity) if project: env.set_project(project) api = InternalApi() sweep_id = api.upsert_sweep(sweep) print('Create sweep with ID:', sweep_id) sweep_url = _get_sweep_url(api, sweep_id) if sweep_url: print('Sweep URL:', sweep_url) return sweep_id
def run_agent(sweep_id, function=None, in_jupyter=None, entity=None, project=None, count=None): if not isinstance(sweep_id, str): wandb.termerror('Expected string sweep_id') return sweep_split = sweep_id.split('/') if len(sweep_split) == 1: pass elif len(sweep_split) == 2: split_project, sweep_id = sweep_split if project and split_project: wandb.termwarn('Ignoring project commandline parameter') project = split_project or project elif len(sweep_split) == 3: split_entity, split_project, sweep_id = sweep_split if entity and split_entity: wandb.termwarn('Ignoring entity commandline parameter') if project and split_project: wandb.termwarn('Ignoring project commandline parameter') project = split_project or project entity = split_entity or entity else: wandb.termerror( 'Expected sweep_id in form of sweep, project/sweep, or entity/project/sweep' ) return if entity: env.set_entity(entity) if project: env.set_project(project) logger.setLevel(logging.DEBUG) ch = logging.StreamHandler() log_level = logging.DEBUG if in_jupyter: log_level = logging.ERROR ch.setLevel(log_level) formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') ch.setFormatter(formatter) try: logger.addHandler(ch) api = InternalApi() queue = multiprocessing.Queue() agent = Agent(api, queue, sweep_id=sweep_id, function=function, in_jupyter=in_jupyter, count=count) agent.run() finally: # make sure we remove the logging handler (important for jupyter notebooks) logger.removeHandler(ch)
def set_setting(self, key, value): self.settings() # make sure we do initial load self._settings[key] = value if key == 'entity': env.set_entity(value, env=self._environ) elif key == 'project': env.set_project(value, env=self._environ)
def run_agent(sweep_id, function=None, in_jupyter=None, entity=None, project=None, count=None): parts = dict(entity=entity, project=project, name=sweep_id) err = util.parse_sweep_id(parts) if err: wandb.termerror(err) return entity = parts.get("entity") or entity project = parts.get("project") or project sweep_id = parts.get("name") or sweep_id if entity: env.set_entity(entity) if project: env.set_project(project) logger.setLevel(logging.DEBUG) ch = logging.StreamHandler() log_level = logging.DEBUG if in_jupyter: log_level = logging.ERROR ch.setLevel(log_level) formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') ch.setFormatter(formatter) try: logger.addHandler(ch) api = InternalApi() queue = multiprocessing.Queue() agent = Agent(api, queue, sweep_id=sweep_id, function=function, in_jupyter=in_jupyter, count=count) agent.run() finally: # make sure we remove the logging handler (important for jupyter notebooks) logger.removeHandler(ch)
def set_setting(self, key, value, globally=False, persist=False): self._settings.set(Settings.DEFAULT_SECTION, key, value, globally=globally, persist=persist) if key == 'entity': env.set_entity(value, env=self._environ) elif key == 'project': env.set_project(value, env=self._environ) elif key == 'base_url': self.relocate()
def set_setting(self, key, value, globally=False): self._settings.set(Settings.DEFAULT_SECTION, key, value, globally=globally) if key == 'entity': env.set_entity(value, env=self._environ) elif key == 'project': env.set_project(value, env=self._environ)
def sweep(sweep, entity=None, project=None): """Sweep create for controller api and jupyter (eventually for cli).""" in_jupyter = wandb._get_python_type() != "python" if in_jupyter: os.environ[env.JUPYTER] = "true" _api0 = InternalApi() if not _api0.api_key: wandb.jupyter_login(api=_api0) if entity: env.set_entity(entity) if project: env.set_project(project) api = InternalApi() sweep_id = api.upsert_sweep(sweep) print('Create sweep with ID:', sweep_id) sweep_url = _get_sweep_url(api, sweep_id) if sweep_url: print('Sweep URL:', sweep_url) return sweep_id
def agent(sweep_id, function=None, entity=None, project=None): """Generic agent entrypoint, used for CLI or jupyter. Args: sweep_id (dict): Sweep ID generated by CLI or sweep API entity (str, optional): W&B Entity project (str, optional): W&B Project function (dict, optional): Configure sweep function """ in_jupyter = wandb._get_python_type() != "python" if in_jupyter: os.environ[env.JUPYTER] = "true" _api0 = InternalApi() if not _api0.api_key: wandb._jupyter_login(api=_api0) if entity: env.set_entity(entity) if project: env.set_project(project) return run_agent(sweep_id, function=function, in_jupyter=in_jupyter)
def sweep(sweep, entity=None, project=None): from wandb.sweeps.config import SweepConfig import types if isinstance(sweep, types.FunctionType): sweep = sweep() if isinstance(sweep, SweepConfig): sweep = dict(sweep) """Sweep create for controller api and jupyter (eventually for cli).""" if entity: env.set_entity(entity) if project: env.set_project(project) # Make sure we are logged in wandb_sdk.wandb_login._login(_silent=True) api = InternalApi() sweep_id = api.upsert_sweep(sweep) print("Create sweep with ID:", sweep_id) if sweep_url := _get_sweep_url(api, sweep_id): print("Sweep URL:", sweep_url)
def __init__(self, sweep_id_or_config=None, entity=None, project=None): global wandb_sweeps try: from wandb.sweeps import sweeps as wandb_sweeps except ImportError as e: raise wandb.Error("Module load error: " + str(e)) # sweep id configured in constuctor self._sweep_id = None # configured parameters # Configuration to be created self._create = {} # Custom search self._custom_search = None # Custom stopping self._custom_stopping = None # Program function (used for future jupyter support) self._program_function = None # The following are updated every sweep step # raw sweep object (dict of strings) self._sweep_obj = None # parsed sweep config (dict) self._sweep_config = None # sweep metric used to optimize (str or None) self._sweep_metric = None # list of _Run objects self._sweep_runs = None # dictionary mapping name of run to run object self._sweep_runs_map = None # scheduler dict (read only from controller) - used as feedback from the server self._scheduler = None # controller dict (write only from controller) - used to send commands to server self._controller = None # keep track of controller dict from previous step self._controller_prev_step = None # Internal # Keep track of whether the sweep has been started self._started = False # indicate whether there is more to schedule self._done_scheduling = False # indicate whether the sweep needs to be created self._defer_sweep_creation = False # count of logged lines since last status self._logged = 0 # last status line printed self._laststatus = "" # keep track of logged actions for print_actions() self._log_actions = [] # keep track of logged debug for print_debug() self._log_debug = [] # all backend commands use internal api environ = os.environ if entity: env.set_entity(entity, env=environ) if project: env.set_project(project, env=environ) self._api = InternalApi(environ=environ) if isinstance(sweep_id_or_config, str): self._sweep_id = sweep_id_or_config elif isinstance(sweep_id_or_config, dict): self.configure(sweep_id_or_config) self._sweep_id = self.create() elif sweep_id_or_config is None: self._defer_sweep_creation = True return else: raise ControllerError("Unhandled sweep controller type") sweep_obj = self._sweep_object_read_from_backend() if sweep_obj is None: raise ControllerError("Can not find sweep") self._sweep_obj = sweep_obj
def sweep(sweep, entity=None, project=None): """Initialize a hyperparameter sweep. To generate hyperparameter suggestions from the sweep and use them to train a model, call `wandb.agent` with the sweep_id returned by this command. For command line functionality, see the command line tool `wandb sweep` (https://docs.wandb.ai/ref/cli/wandb-sweep). Args: sweep: dict, SweepConfig, or callable. The sweep configuration (or configuration generator). If a dict or SweepConfig, should conform to the W&B sweep config specification (https://docs.wandb.ai/guides/sweeps/configuration). If a callable, should take no arguments and return a dict that conforms to the W&B sweep config spec. entity: str (optional). An entity is a username or team name where you're sending runs. This entity must exist before you can send runs there, so make sure to create your account or team in the UI before starting to log runs. If you don't specify an entity, the run will be sent to your default entity, which is usually your username. Change your default entity in [Settings](wandb.ai/settings) under "default location to create new projects". project: str (optional). The name of the project where you're sending the new run. If the project is not specified, the run is put in an "Uncategorized" project. Returns: sweep_id: str. A unique identifier for the sweep. Examples: Basic usage ```python # this line initializes the sweep sweep_id = wandb.sweep({'name': 'my-awesome-sweep', 'metric': 'accuracy', 'method': 'grid', 'parameters': {'a': {'values': [1, 2, 3, 4]}}}) # this line actually runs it -- parameters are available to # my_train_func via wandb.config wandb.agent(sweep_id, function=my_train_func) ``` """ from wandb.sweeps.config import SweepConfig import types if isinstance(sweep, types.FunctionType): sweep = sweep() if isinstance(sweep, SweepConfig): sweep = dict(sweep) """Sweep create for controller api and jupyter (eventually for cli).""" if entity: env.set_entity(entity) if project: env.set_project(project) # Make sure we are logged in wandb_sdk.wandb_login._login(_silent=True) api = InternalApi() sweep_id = api.upsert_sweep(sweep) print("Create sweep with ID:", sweep_id) sweep_url = _get_sweep_url(api, sweep_id) if sweep_url: print("Sweep URL:", sweep_url) return sweep_id
def __init__(self, sweep_id_or_config=None, entity=None, project=None): # sweep id configured in constuctor self._sweep_id: Optional[str] = None # configured parameters # Configuration to be created self._create: Dict = {} # Custom search self._custom_search: Optional[ Callable[[Union[dict, sweeps.SweepConfig], List[sweeps.SweepRun]], Optional[sweeps.SweepRun], ]] = None # Custom stopping self._custom_stopping: Optional[ Callable[[Union[dict, sweeps.SweepConfig], List[sweeps.SweepRun]], List[sweeps.SweepRun], ]] = None # Program function (used for future jupyter support) self._program_function = None # The following are updated every sweep step # raw sweep object (dict of strings) self._sweep_obj = None # parsed sweep config (dict) self._sweep_config: Optional[Union[dict, sweeps.SweepConfig]] = None # sweep metric used to optimize (str or None) self._sweep_metric: Optional[str] = None # list of _Run objects self._sweep_runs: Optional[List[sweeps.SweepRun]] = None # dictionary mapping name of run to run object self._sweep_runs_map: Optional[Dict[str, sweeps.SweepRun]] = None # scheduler dict (read only from controller) - used as feedback from the server self._scheduler: Optional[Dict] = None # controller dict (write only from controller) - used to send commands to server self._controller: Optional[Dict] = None # keep track of controller dict from previous step self._controller_prev_step: Optional[Dict] = None # Internal # Keep track of whether the sweep has been started self._started: bool = False # indicate whether there is more to schedule self._done_scheduling: bool = False # indicate whether the sweep needs to be created self._defer_sweep_creation: bool = False # count of logged lines since last status self._logged: int = 0 # last status line printed self._laststatus: str = "" # keep track of logged actions for print_actions() self._log_actions: List[Tuple[str, str]] = [] # keep track of logged debug for print_debug() self._log_debug: List[str] = [] # all backend commands use internal api environ = os.environ if entity: env.set_entity(entity, env=environ) if project: env.set_project(project, env=environ) self._api = InternalApi(environ=environ) if isinstance(sweep_id_or_config, str): self._sweep_id = sweep_id_or_config elif isinstance(sweep_id_or_config, dict) or isinstance( sweep_id_or_config, sweeps.SweepConfig): self._create = sweeps.SweepConfig(sweep_id_or_config) # check for custom search and or stopping functions for config_key, controller_attr in zip( ["method", "early_terminate"], ["_custom_search", "_custom_stopping"]): if callable(config_key in self._create and self._create[config_key]): setattr(self, controller_attr, self._create[config_key]) self._create[config_key] = "custom" self._sweep_id = self.create(from_dict=True) elif sweep_id_or_config is None: self._defer_sweep_creation = True return else: raise ControllerError("Unhandled sweep controller type") sweep_obj = self._sweep_object_read_from_backend() if sweep_obj is None: raise ControllerError("Can not find sweep") self._sweep_obj = sweep_obj
def sweep( sweep: Union[dict, Callable], entity: str = None, project: str = None, ) -> str: """Initialize a hyperparameter sweep. To generate hyperparameter suggestions from the sweep and use them to train a model, call `wandb.agent` with the sweep_id returned by this command. For command line functionality, see the command line tool `wandb sweep` (https://docs.wandb.ai/ref/cli/wandb-sweep). Args: sweep: dict, SweepConfig, or callable. The sweep configuration (or configuration generator). If a dict or SweepConfig, should conform to the W&B sweep config specification (https://docs.wandb.ai/guides/sweeps/configuration). If a callable, should take no arguments and return a dict that conforms to the W&B sweep config spec. entity: str (optional). An entity is a username or team name where you're sending runs. This entity must exist before you can send runs there, so make sure to create your account or team in the UI before starting to log runs. If you don't specify an entity, the run will be sent to your default entity, which is usually your username. Change your default entity in [Settings](wandb.ai/settings) under "default location to create new projects". project: str (optional). The name of the project where you're sending the new run. If the project is not specified, the run is put in an "Uncategorized" project. Returns: sweep_id: str. A unique identifier for the sweep. Examples: Basic usage <!--yeadoc-test:one-parameter-sweep--> ```python import wandb sweep_configuration = { "name": "my-awesome-sweep", "metric": {"name": "accuracy", "goal": "maximize"}, "method": "grid", "parameters": { "a": { "values": [1, 2, 3, 4] } } } def my_train_func(): # read the current value of parameter "a" from wandb.config wandb.init() a = wandb.config.a wandb.log({"a": a, "accuracy": a + 1}) sweep_id = wandb.sweep(sweep_configuration) # run the sweep wandb.agent(sweep_id, function=my_train_func) ``` """ if callable(sweep): sweep = sweep() """Sweep create for controller api and jupyter (eventually for cli).""" if entity: env.set_entity(entity) if project: env.set_project(project) # Make sure we are logged in if wandb.run is None: wandb_login._login(_silent=True) api = InternalApi() sweep_id, warnings = api.upsert_sweep(sweep) handle_sweep_config_violations(warnings) print("Create sweep with ID:", sweep_id) sweep_url = _get_sweep_url(api, sweep_id) if sweep_url: print("Sweep URL:", sweep_url) return sweep_id