def prepare_env(self): """Prepare running environment. * Load parameters from json files. * Initialize system folders, model name and the paths to be saved. * Initialize resource monitor. * Initialize random seed. * Initialize logging. """ # Load config file from json with open(self.args.config_file) as config_params: print(f"loading config file {self.args.config_file}") config = json.load(config_params) # Update configs based on the received args from the command line . update_args(config, self.args) # obtain abspath for the project config["system"]["root_dir"] = os.path.abspath( config["system"]["root_dir"]) # construct unique model run id, which consist of model name, config id and a timestamp timestamp_str = datetime.now().strftime("%Y%m%d_%H%M%S") random_str = "".join( [random.choice(string.ascii_lowercase) for n in range(6)]) config["system"]["model_run_id"] = (config["model"]["model"] + "_" + config["model"]["config_id"] + "_" + timestamp_str + "_" + random_str) # Initialize random seeds set_seed(config["system"]["seed"] if "seed" in config["system"] else 2020) # Initialize working folders self.initialize_folders(config) config["system"]["process_dir"] = os.path.join( config["system"]["root_dir"], config["system"]["process_dir"]) # Initialize log file config["system"]["log_file"] = os.path.join( config["system"]["root_dir"], config["system"]["log_dir"], config["system"]["model_run_id"], ) logger.init_std_logger(config["system"]["log_file"]) print("Python version:", sys.version) print("pytorch version:", torch.__version__) # File paths to be saved config["model"]["run_dir"] = os.path.join( config["system"]["root_dir"], config["system"]["run_dir"], config["system"]["model_run_id"], ) config["system"]["run_dir"] = config["model"]["run_dir"] print( "The intermediate running statuses will be reported in folder:", config["system"]["run_dir"], ) config["system"]["tune_dir"] = os.path.join( config["system"]["root_dir"], config["system"]["tune_dir"]) def get_user_temp_dir(): tempdir = os.path.join(config["system"]["root_dir"], "tmp") print(f"ray temp dir {tempdir}") return tempdir ray.utils.get_user_temp_dir = get_user_temp_dir # Model checkpoints paths to be saved config["system"]["model_save_dir"] = os.path.join( config["system"]["root_dir"], config["system"]["checkpoint_dir"], config["system"]["model_run_id"], ) ensureDir(config["system"]["model_save_dir"]) print("Model checkpoint will save in file:", config["system"]["model_save_dir"]) config["system"]["result_file"] = os.path.join( config["system"]["root_dir"], config["system"]["result_dir"], config["system"]["result_file"], ) print("Performance result will save in file:", config["system"]["result_file"]) print_dict_as_table(config["system"], "System configs") return config
with open(config["config_file"]) as config_params: print("loading config file", config["config_file"]) json_config = json.load(config_params) json_config.update(config) config = json_config root_dir = config["root_dir"] time_str = datetime.now().strftime("%Y%m%d_%H%M%S") log_file = (root_dir + "logs/cornac" + "_" + config["dataset"] + "_" + config["data_split"] + time_str) config["result_file"] = (root_dir + "results/cornac" + "_" + config["dataset"] + "_" + config["data_split"] + ".csv") """ init logger """ logger.init_std_logger(log_file) # cornac.eval_methods.base_method.rating_eval = rating_eval # Load the built-in MovieLens 100K dataset (will be downloaded if not cached): # Here we are comparing Biased MF, PMF, and BPR: # pop = cornac.models.most_pop.recom_most_pop.MostPop(name="MostPop") # mf = cornac.models.MF( # k=10, max_iter=25, learning_rate=0.01, lambda_reg=0.02, use_bias=True, seed=123 # ) # pmf = cornac.models.PMF( # k=10, max_iter=100, learning_rate=0.001, lambda_reg=0.001, seed=123 # ) # bpr = cornac.models.BPR( # k=10, max_iter=200, learning_rate=0.001, lambda_reg=0.01, seed=123
def prepare_env(config): """Prepare running environment - Load parameters from json files. - Initialize system folders, model name and the paths to be saved. - Initialize resource monitor. - Initialize random seed. - Initialize logging. Args: config (dict): Global configs. """ # obtain abspath for the project # You need specified it if it is running in the container. if "root_dir" not in config: file_dir = os.path.dirname(os.path.abspath(__file__)) config["root_dir"] = os.path.abspath(os.path.join(file_dir, "..")) # load config file from json with open(config["config_file"]) as config_params: print("loading config file", config["config_file"]) json_config = json.load(config_params) # update global parameters with these parameters received from the command line . json_config.update(config) config = json_config # construct unique model run id, which consist of model name, config id and a timestamp timestamp_str = datetime.now().strftime("%Y%m%d_%H%M%S") random_str = "".join([random.choice(string.ascii_lowercase) for n in range(6)]) config["model_run_id"] = ( config["model"] + "_" + config["config_id"] + "_" + timestamp_str + "_" + random_str ) set_seed(config["seed"] if "seed" in config else 2020) initialize_folders(config["root_dir"]) # Initialize log file config["log_file"] = os.path.join( config["root_dir"], config["log_dir"], config["model_run_id"] ) logger.init_std_logger(config["log_file"]) print("python version:", sys.version) print("pytorch version:", torch.__version__) # File paths to be saved config["run_dir"] = os.path.join( config["root_dir"], config["run_dir"], config["model_run_id"] ) print( "The intermediate running statuses will be reported in folder:", config["run_dir"], ) # Model checkpoints paths to be saved config["model_save_dir"] = os.path.join( config["root_dir"], config["checkpoint_dir"], config["model_run_id"] ) ensureDir(config["model_save_dir"]) print("Model checkpoint will save in file:", config["model_save_dir"]) config["result_file"] = os.path.join( config["root_dir"], config["result_dir"], config["result_file"] ) print("Performance result will save in file:", config["result_file"]) # remove comments print_dict_as_table(config, "Model configs") return config