# width=ng.p.Scalar(lower=0, upper=20), # height=ng.p.Scalar(lower=-100, upper=100), # activation=ng.p.Choice(choices=["relu", "tanh"]) # ) algo = NevergradSearch( optimizer=ng.optimizers.OnePlusOne, # space=space, # If you want to set the space manually ) algo = ConcurrencyLimiter(algo, max_concurrent=4) scheduler = AsyncHyperBandScheduler() analysis = tune.run( easy_objective, metric="mean_loss", mode="min", name="nevergrad", search_alg=algo, scheduler=scheduler, num_samples=10 if args.smoke_test else 50, config={ "steps": 100, "width": tune.uniform(0, 20), "height": tune.uniform(-100, 100), "activation": tune.choice(["relu", "tanh"]), }, ) print("Best hyperparameters found were: ", analysis.best_config)
train_ray( path="/data/classification.parquet", num_workers=4, num_boost_rounds=100, num_files=64, regression=False, use_gpu=False, ray_params=ray_params, lightgbm_params=config, ) if __name__ == "__main__": search_space = { "eta": tune.loguniform(1e-4, 1e-1), "subsample": tune.uniform(0.5, 1.0), "max_depth": tune.randint(1, 9), } ray.init(address="auto") ray_params = RayParams( elastic_training=False, max_actor_restarts=2, num_actors=4, cpus_per_actor=1, gpus_per_actor=0, ) start = time.time() analysis = tune.run(
agent.gamma = config["gamma"] agent.delta = config["delta"] agent.epsilon_decay = config["epsilon_decay"] agent.eta = config["eta"] for i in range(10): agent.play() agent_test = AgentTest(agent, RandomPlayer(), 0) reward = agent_test.play() tune.report(steps=agent.steps, reward=reward) bayesopt = BayesOptSearch(metric="reward", mode="max") analysis = tune.run(training_function, config={ "learning_rate": tune.loguniform(1e-3, 1e-1), "alpha": tune.loguniform(1e-3, 1e-1), "gamma": tune.loguniform(1e-3, 1e-1), "delta": tune.loguniform(0.1, 0.5), "epsilon_decay": tune.uniform(0.9999, 0.999999), "eta": tune.loguniform(1e-3, 1e-1) }, local_dir="/DoubleDQN", search_alg=bayesopt, num_samples=100) print("Best config: ", analysis.get_best_config(metric="reward", mode="max")) # Get a dataframe for analyzing trial results. df = analysis.results_df
datasets.MNIST("~/data", train=True, download=True) args = parser.parse_args() import ray from ray import tune from ray.tune.schedulers import HyperBandScheduler ray.init(redis_address=args.redis_address) sched = HyperBandScheduler(time_attr="training_iteration", metric="mean_loss", mode="min") tune.run(TrainMNIST, scheduler=sched, **{ "stop": { "mean_accuracy": 0.95, "training_iteration": 1 if args.smoke_test else 20, }, "resources_per_trial": { "cpu": 3, "gpu": int(not args.no_cuda) }, "num_samples": 1 if args.smoke_test else 20, "checkpoint_at_end": True, "config": { "args": args, "lr": tune.uniform(0.001, 0.1), "momentum": tune.uniform(0.1, 0.9), } })
tune.report(score=score) # This sends the score to Tune. def trainable(config): # config (dict): A dict of hyperparameters. for _ in range(20): score = objective(config) tune.report(score=score) # This sends the score to Tune. analysis = tune.run( trainable, config={ # Where to place. Note winning is best at all costs "public_vps": tune.uniform(0.0, 100.0), "production": tune.uniform(0.0, 100.0), "enemy_production": tune.uniform(-100.0, 0.0), "num_tiles": tune.uniform(0.0, 100.0), # Towards where to expand and when "reachable_production_0": tune.uniform(0.0, 100.0), "reachable_production_1": tune.uniform(0.0, 100.0), "buildable_nodes": tune.uniform(0.0, 100.0), "longest_road": tune.uniform(0.0, 100.0), # Hand, when to hold and when to use. "hand_synergy": tune.uniform(0.0, 100.0), "hand_resources": tune.uniform(0.0, 100.0), "discard_penalty": tune.uniform(-100.0, 0.0), "hand_devs": tune.uniform(0.0, 100.0), "army_size": tune.uniform(0.0, 100.0), },
parser.add_argument( "--server-address", type=str, default=None, required=False, help="The address of server to connect to if using Ray Client.", ) args, _ = parser.parse_known_args() if args.server_address is not None: ray.init(f"ray://{args.server_address}") else: ray.init(num_cpus=4 if args.smoke_test else None) # Hyperband early stopping, configured with `episode_reward_mean` as the # objective and `training_iteration` as the time unit, # which is automatically filled by Tune. hyperband = HyperBandScheduler(max_t=200) analysis = tune.run( train, name="hyperband_test", num_samples=20, metric="episode_reward_mean", mode="max", stop={"training_iteration": 10 if args.smoke_test else 99999}, config={"height": tune.uniform(0, 100)}, scheduler=hyperband, fail_fast=True, ) print("Best hyperparameters found were: ", analysis.best_config)
"task": "predict_and_get_currency_data_from_web_pound", "schedule": 60, }, "fit_time_series_model_pound": { "task": "fit_time_series_model_pound", "schedule": 900, }, "optimize_time_series_model_pound": { "task": "optimize_time_series_model_pound", "schedule": 3780, }, } SEARCH_SPACE = { "gru_units": tune.choice(list(range(20, 120))), "rec_dropout": tune.uniform(0.0, 0.4), "batch_size": tune.choice([8, 12, 16, 20, 24]), } TRAINING_PARAMETER_LIST = ["batch_size"] CURRENCY_PREDICTOR_COMMON_DICT = { # data_from_web "get_currency_data": webscrap_currency_data_bid_and_time_from_investing_com_bid, "pred_fit_optim_time_offset_tuple": (12, 10, 5), # fiting_predicting "n_points_model": 180, "n_points_training": 120, "n_steps": 10, "create_model": create_model,
def experiment(): ucbstate = None if IS_UCB: ucbstate = ucb_state(n_params=N_PARAMS) scheduler = PopulationBasedTraining( time_attr="training_iteration", metric="mean_accuracy", mode="max", ucb=ucbstate, perturbation_interval=5, hyperparam_mutations={ # distribution for resampling "lr": lambda: np.random.uniform(0.0001, 1), # allow perturbations within this set of categorical values "momentum": [0.8, 0.9, 0.99], "train_batch_size": lambda: random.randint(32, 256) }) ray.shutdown( ) # Restart Ray defensively in case the ray connection is lost. # ray.init(log_to_driver=False, local_mode=True) ray.init(log_to_driver=False) analysis = tune.run( PytorchTrainble, name=EXPERIMENT_NAME, scheduler=scheduler, reuse_actors=True, checkpoint_freq=20, verbose=1, stop={ "training_iteration": TRAINING_ITERATION, }, num_samples=NUM_WORKERS, # PBT starts by training many neural networks in parallel with random hyperparameters. config={ "lr": tune.uniform(0.001, 1), "momentum": tune.uniform(0.001, 1), "train_batch_size": sample_from(lambda spec: random.choice([32, 64, 128, 256])), }) # Plot by wall-clock time dfs = analysis.fetch_trial_dataframes() ## Save pickle with open(f"data/{EXPERIMENT_NAME}_trials.pickle", "wb") as fw: pickle.dump(dfs, fw) # This plots everything on the same plot ax = None for d in dfs.values(): ax = d.plot("training_iteration", "mean_accuracy", ax=ax, legend=False) a = np.asarray( [list(dfs.values())[i].mean_accuracy.max() for i in range(K)]) topk = heapq.nlargest(3, range(len(a)), a.__getitem__) sum = 0 for i in topk: sum += a[i] avg_top_k = sum / 3 plt.xlabel("epoch") plt.ylabel("Test Accuracy") # plt.show() plt.savefig(f'{EXPERIMENT_NAME}_accuracy.png') if IS_UCB: # bar chart fig, axs = plt.subplots(1, 2, figsize=(9, 3)) axs[0].bar(range(len(ucbstate.num_of_selections) - 1), ucbstate.num_of_selections[1:]) axs[1].bar(range(len(ucbstate.rewards) - 1), ucbstate.rewards[1:]) print(ucbstate.rewards) print(ucbstate.num_of_selections) ## Save pickle with open(f"data/{EXPERIMENT_NAME}_bandit.pickle", "wb") as fw: pickle.dump(ucbstate, fw) plt.savefig(f'data/{EXPERIMENT_NAME}_bandit_final.png') # plt.show() return avg_top_k
args = parser.parse_args() if args.ray_address: ray.init(address=args.ray_address) else: ray.init(num_cpus=2 if args.smoke_test else None) # for early stopping sched = AsyncHyperBandScheduler() analysis = tune.run( train_mnist, metric="mean_accuracy", mode="max", name="exp", scheduler=sched, stop={ "mean_accuracy": 0.98, "training_iteration": 5 if args.smoke_test else 100 }, resources_per_trial={ "cpu": 2, "gpu": 0.25 # set this for GPUs }, num_samples=1 if args.smoke_test else 50, config={ "lr": tune.loguniform(1e-4, 1e-2), "momentum": tune.uniform(0.1, 0.9), }) print("Best config is:", analysis.best_config)
def setUp(self): self.config = {"report": tune.uniform(0.0, 5.0)}