def main(): analysis = Analysis(TUNE_RESULTS_FOLDER) print("Best hyperparameter {}".format( analysis.get_best_config(metric="mean_reward", mode="max"))) best_model_path = analysis.get_best_logdir(metric="mean_reward", mode="max") print( "Best model found in {}, start rendering .gif".format(best_model_path)) best_model = SomeModelToTrain({ 'learning_rate': 0.1, 'batch_size': 1, 'target_update': 1 }) checkpoint_path = f'{best_model_path}/checkpoint_{MAX_TRAINING_ITERATION}' best_model.load(checkpoint_path + '/' + MODEL_FILENAME) # we got this part from https://stable-baselines.readthedocs.io/en/master/guide/examples.html and modified it env = gym.make('LunarLander-v2') images = [] state = env.reset() for j in range(210): action = best_model.agent.act(state) img = env.render(mode='rgb_array') images.append(img) state, reward, done, _ = env.step(action) if done: break env.close() imageio.mimsave( 'best_model.gif', [np.array(img) for i, img in enumerate(images) if i % 2 == 0], fps=29) optimize('best_model.gif')
def meta_setup(self): from ray.tune import Analysis out_meta = ContextCompositeNode.meta_setup(self) if 'tune' in self.conf: if 'local_dir' in self.conf['tune']: path = self.conf['tune']['local_dir'] if 'name' in self.conf['tune']: exp = self.conf['tune']['name'] try: analysis = Analysis(path + '/' + exp) if 'best' in self.conf: best = analysis.get_best_config( **self.conf['best']) for key in best.keys(): self.conf['context'][key]['value'] = best[key] print('get best', best) out_meta.outports[self.OUTPUT_CONFIG] = self.conf except Exception: pass return out_meta
def testBestConfigIsLogdir(self): analysis = Analysis(self.test_dir) for metric, mode in [(self.metric, "min"), (self.metric, "max")]: logdir = analysis.get_best_logdir(metric, mode=mode) best_config = analysis.get_best_config(metric, mode=mode) self.assertEquals(analysis.get_all_configs()[logdir], best_config)
from ray.tune import Analysis import pandas as pd import os import numpy as np if __name__ == "__main__": analysis = Analysis( "/Users/shaobohu/Documents/我的坚果云/project/circles_experiment/TRY_ALL/Train") print(sorted(analysis.dataframe()['acc'].tolist())) print(analysis.get_best_config('acc', 'max'))
import torch import matplotlib.pyplot as plt from core.dataloader import * from core.model import * from core.training import * from core.evaluating import * from plots.plots import * from core.create_folder import * from core.pred_sequence import * folders = [] folders.append( "Run_w_11_timesteps_3_hiddenDim_1_layers_0.00021723989839730966_LR") configs = [] analysis = Analysis("~/ray_results/Jan21") configs.append(analysis.get_best_config(metric="error", mode="max")) for i in range(2, 17): analysis = Analysis("~/ray_results/" + str(i) + "forward") config = analysis.get_best_config(metric="error", mode="max") configs.append(config) print(config) folder_name = 'Predicting' + str(config["num_forward"]) + '_w_' + str( config["timesteps"]) + '_timesteps_' + str( config["hidden_dim"]) + '_hiddenDim_' + str( config["num_layers"]) + '_layers_' + str(config["lr"]) + "_LR" folders.append(folder_name) #print(folder_name) print(len(folders)) print(len(configs)) model_keys = [] config_multiple_models = {}