# permanent address, but here it will be started for the local machine # with a random port NS = hpns.NameServer(run_id=run_id, host='localhost', port=0) ns_host, ns_port = NS.start() # Start a bunch of workers in some threads, just to show how it works. # On the cluster, each worker would run in a separate job and the nameserver # credentials have to be distributed. num_workers = 1 workers = [] for i in range(num_workers): w = MyWorker( nameserver=ns_host, nameserver_port=ns_port, run_id=run_id, # unique Hyperband run id id=i # unique ID as all workers belong to the same process ) w.run(background=True) workers.append(w) HB = opt( configspace=config_space, run_id=run_id, eta=3, min_budget=27, max_budget=243, # HB parameters nameserver=ns_host, nameserver_port=ns_port, ping_interval=3600, # here, master pings for workers every hour )
# Step 1: # Every run needs a nameserver. It could be a 'static' server with a # permanent address, but here it will be started for the local machine # with a random port. # The nameserver manages the concurrent running workers across all possible threads or clusternodes. NS = hpns.NameServer(run_id=run_id, host='localhost', port=0) ns_host, ns_port = NS.start() # Step 2: # The worker implements the connection to the model to be evaluated. # Its 'compute'-method will be called later by the BOHB-optimizer repeatedly # with the sampled configurations and return for example the computed loss. # Further usages of the worker will be covered in a later example. worker = MyWorker(nameserver=ns_host, nameserver_port=ns_port, run_id=run_id) worker.run(background=True) # We will start the first run with a smaller budget, which we define here. # In the second run, we'll use three times as much. min_budget = 9 max_budget = 243 # Step 3: # The number of sampled configurations is determined by the # parameters eta, min_budget and max_budget. # After evaluating each configuration, starting with the minimum budget # on the same subset size, only a fraction of 1 / eta of them # 'advances' to the next round. At the same time the current budget will be doubled.
args=parser.parse_args() if args.array_id == 1: # start nameserver NS = hpns.NameServer(run_id=args.run_id, nic_name=args.nic_name, working_directory=args.working_dir) ns_host, ns_port = NS.start() # stores information for workers to find in working_directory # BOHB is usually so cheap, that we can affort to run a worker on the master node, too. worker = MyWorker(nameserver=ns_host, nameserver_port=ns_port, run_id=args.run_id) worker.run(background=True) HB = BOHB( configspace = config_space, run_id = args.run_id, eta=3,min_budget=27, max_budget=243, host=ns_host, nameserver=ns_host, nameserver_port = ns_port, ping_interval=3600, ) res = HB.run( n_iterations = 4, min_n_workers = 4 # BOHB can wait until a minimum number of workers is online before starting )
args = parser.parse_args() # Step 1: Start a nameserver (see example_1) NS = hpns.NameServer(run_id='example2', host='127.0.0.1', port=None) NS.start() # Step 2: Start the workers # Now we can instantiate the specified number of workers. To emphasize the effect, # we introduce a sleep_interval of one second, which makes every function evaluation # take a bit of time. Note the additional id argument that helps separating the # individual workers. This is necessary because every worker uses its processes # ID which is the same for all threads here. workers = [] for i in range(args.n_workers): w = MyWorker(sleep_interval=0.5, nameserver='127.0.0.1', run_id='example2', id=i) w.run(background=True) workers.append(w) # Step 3: Run an optimizer # Now we can create an optimizer object and start the run. # We add the min_n_workers argument to the run methods to make the optimizer wait # for all workers to start. This is not mandatory, and workers can be added # at any time, but if the timing of the run is essential, this can be used to # synchronize all workers right at the start. bohb = BOHB(configspace=w.get_configspace(), run_id='example2', min_budget=args.min_budget, max_budget=args.max_budget) res = bohb.run(n_iterations=args.n_iterations, min_n_workers=args.n_workers)
# Step 1: # Every run needs a nameserver. It could be a 'static' server with a # permanent address, but here it will be started for the local machine # with a random port. # The nameserver manages the concurrent running workers across all possible threads or clusternodes. NS = hpns.NameServer(run_id=run_id, host='localhost', port=0) ns_host, ns_port = NS.start() # Step 2: # The worker implements the connection to the model to be evaluated. # Its 'compute'-method will be called later by the BOHB-optimizer repeatedly # with the sampled configurations and return for example the computed loss. # Further usages of the worker will be covered in a later example. w = MyWorker( nameserver=ns_host, nameserver_port=ns_port, run_id=run_id, # unique Hyperband run id ) w.run(background=True) # Step 3: # In the last of the 3 Steps, we create a optimizer object. # It samples configurations from the ConfigurationSpace. # The number of sampled configurations is determined by the # parameters eta, min_budget and max_budget. # After evaluating each configuration, starting with the minimum budget # on the same subset size, only a fraction of 1 / eta of them # 'advances' to the next round. At the same time the current budget will be doubled. # This process runs until the maximum budget is reached. HB = BOHB( configspace=config_space,
parser.add_argument('--n_iterations', type=int, help='Number of iterations performed by the optimizer', default=4) parser.add_argument('--n_workers', type=int, help='Number of workers to run in parallel.', default=2) parser.add_argument('--worker', help='Flag to turn this into a worker process', action='store_true') args = parser.parse_args() if args.worker: w = MyWorker(sleep_interval=0.5, nameserver='127.0.0.1', run_id='example3') w.run(background=False) exit(0) # Start a nameserver (see example_1) NS = hpns.NameServer(run_id='example3', host='127.0.0.1', port=None) NS.start() # Run an optimizer (see example_2) bohb = BOHB(configspace=MyWorker.get_configspace(), run_id='example3', min_budget=args.min_budget, max_budget=args.max_budget) res = bohb.run(n_iterations=args.n_iterations, min_n_workers=args.n_workers) # Step 4: Shutdown
help='Which network interface to use for communication.') parser.add_argument( '--shared_directory', type=str, help='A directory that is accessible for all processes, e.g. a NFS share.') args = parser.parse_args() # Every process has to lookup the hostname host = hpns.nic_name_to_host(args.nic_name) if args.worker: time.sleep( 5 ) # short artificial delay to make sure the nameserver is already running w = MyWorker(sleep_interval=0.5, run_id=args.run_id, host=host) w.load_nameserver_credentials(working_directory=args.shared_directory) w.run(background=False) exit(0) # Start a nameserver: # We now start the nameserver with the host name from above and a random open port (by setting the port to 0) NS = hpns.NameServer(run_id=args.run_id, host=host, port=0, working_directory=args.shared_directory) ns_host, ns_port = NS.start() # Most optimizers are so computationally inexpensive that we can affort to run a # worker in parallel to it. Note that this one has to run in the background to # not plock!