<s> from __future__ import print_function <EOL> from math import sqrt <EOL> from os . path import abspath , dirname , join <EOL> import sys <EOL> from time import localtime , strftime <EOL> from git import Repo <EOL> import json <EOL> sys . path . append ( abspath ( dirname ( dirname ( __file__ ) ) ) ) <EOL> from utils . data_io import load_numpy_array_from_file <EOL> from utils . data_stats import load_stats_from_file <EOL> from utils . data_paths import DATA_DIR_PATH , RESULTS_DIR_PATH <EOL> def calculate_rmse ( true_ratings , predictions ) : <EOL> return sqrt ( ( ( predictions - true_ratings ) ** <NUM_LIT:2> ) . mean ( ) ) <EOL> def predict_and_save_rmse ( model , test_points , rmse_file_path , <EOL> keep_predictions = False , <EOL> predictions_file_name = '<STR_LIT>' ) : <EOL> predictions = model . predict ( test_points ) <EOL> true_ratings = test_points [ : , <NUM_LIT:3> ] <EOL> rmse = calculate_rmse ( true_ratings , predictions ) <EOL> print ( '<STR_LIT>' , rmse ) <EOL> save_rmse ( rmse , rmse_file_path , append = True ) <EOL> if keep_predictions : <EOL> save_predictions ( predictions , predictions_file_name ) <EOL> def save_model ( model , model_file_name ) : <EOL> print ( '<STR_LIT>' . format ( model_file_name ) ) <EOL> model . save ( model_file_name ) <EOL> def save_run_info ( model , test_set_name , train_set_name , date_string , <EOL> time_string , feature_epoch_order , create_files , <EOL> epochs , run_multi , run_name , commit ) : <EOL> info_file_name = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> . format ( model_class = model . __class__ . __name__ , <EOL> short_commit = commit [ : <NUM_LIT:5> ] , <EOL> run_name = run_name , <EOL> start_time = time_string ) <EOL> ) <EOL> info_file_path = join ( RESULTS_DIR_PATH , info_file_name ) <EOL> excluded_params = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> run_info = { key : value for key , value in model . __dict__ . items ( ) <EOL> if key not in excluded_params } <EOL> run_info [ '<STR_LIT>' ] = model . __class__ . __name__ <EOL> run_info [ '<STR_LIT>' ] = commit <EOL> run_info [ '<STR_LIT>' ] = train_set_name <EOL> run_info [ '<STR_LIT:name>' ] = run_name <EOL> run_info [ '<STR_LIT:time>' ] = time_string <EOL> run_info [ '<STR_LIT:date>' ] = date_string <EOL> run_info [ '<STR_LIT>' ] = test_set_name <EOL> run_info [ '<STR_LIT>' ] = epochs <EOL> run_info [ '<STR_LIT>' ] = create_files <EOL> run_info [ '<STR_LIT>' ] = run_multi <EOL> run_info [ '<STR_LIT>' ] = feature_epoch_order <EOL> json . dump ( run_info , open ( info_file_path , '<STR_LIT:w>' ) , indent = <NUM_LIT:4> , <EOL> sort_keys = True ) <EOL> return info_file_path <EOL> def run ( model , train_set_name , test_set_name , run_name , epochs = None , <EOL> feature_epoch_order = False , create_files = True , run_multi = False ) : <EOL> print ( '<STR_LIT>' <EOL> . format ( model_class = model . __class__ . __name__ , train = train_set_name ) ) <EOL> if not create_files : <EOL> print ( "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> confirm = input ( "<STR_LIT>" ) <EOL> if confirm == '<STR_LIT:Y>' or confirm == '<STR_LIT:y>' or confirm == '<STR_LIT>' : <EOL> pass <EOL> else : <EOL> return <EOL> if epochs is not None : <EOL> print ( '<STR_LIT>' , epochs ) <EOL> if model . num_features is not None : <EOL> print ( '<STR_LIT>' , model . num_features ) <EOL> train_file_path = join ( DATA_DIR_PATH , train_set_name + '<STR_LIT>' ) <EOL> stats_file_path = join ( DATA_DIR_PATH , '<STR_LIT>' , train_set_name + <EOL> '<STR_LIT>' ) <EOL> model . debug = True <EOL> train_points = load_numpy_array_from_file ( train_file_path ) <EOL> stats = load_stats_from_file ( stats_file_path ) <EOL> test_file_path = join ( DATA_DIR_PATH , test_set_name + '<STR_LIT>' ) <EOL> test_points = load_numpy_array_from_file ( test_file_path ) <EOL> date_format = '<STR_LIT>' <EOL> time_format = '<STR_LIT>' <EOL> latest_commit = Repo ( '<STR_LIT:.>' ) . commit ( '<STR_LIT>' ) . hexsha <EOL> date_string = strftime ( date_format , localtime ( ) ) <EOL> time_string = strftime ( time_format , localtime ( ) ) <EOL> run_info_file_path = save_run_info ( <EOL> model = model , test_set_name = test_set_name , <EOL> train_set_name = train_set_name , <EOL> epochs = epochs , <EOL> time_string = time_string , <EOL> date_string = date_string , <EOL> feature_epoch_order = feature_epoch_order , <EOL> create_files = create_files , <EOL> run_multi = run_multi , <EOL> run_name = run_name , <EOL> commit = latest_commit <EOL> ) <EOL> print ( '<STR_LIT>' , run_info_file_path ) <EOL> rmse_file_path = run_info_file_path . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> predictions_file_name = ( run_info_file_path . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] <EOL> . replace ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if not run_multi : <EOL> if not feature_epoch_order : <EOL> model . train ( train_points , stats = stats , epochs = epochs ) <EOL> else : <EOL> model . train_feature_epoch ( train_points = train_points , stats = stats , <EOL> epochs = epochs ) <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> for epoch in range ( epochs ) : <EOL> if epoch == <NUM_LIT:0> : <EOL> model . train ( train_points , stats = stats , epochs = <NUM_LIT:1> ) <EOL> else : <EOL> model . train_more ( epochs = <NUM_LIT:1> ) <EOL> if create_files : <EOL> print ( '<STR_LIT>' . format ( test = test_set_name ) ) <EOL> predict_and_save_rmse ( <EOL> model , test_points = test_points , <EOL> rmse_file_path = rmse_file_path , <EOL> keep_predictions = ( create_files and epoch == epochs - <NUM_LIT:1> ) , <EOL> predictions_file_name = predictions_file_name <EOL> ) <EOL> model . train_points = None <EOL> if create_files : <EOL> model_file_name = ( run_info_file_path . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] <EOL> . replace ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> save_model ( model , model_file_name ) <EOL> if not run_multi : <EOL> print ( '<STR_LIT>' . format ( test = test_set_name ) ) <EOL> predict_and_save_rmse ( model , test_points = test_points , <EOL> rmse_file_path = rmse_file_path , <EOL> keep_predictions = True , <EOL> predictions_file_name = predictions_file_name ) <EOL> def save_predictions ( predictions , predictions_file_name ) : <EOL> print ( '<STR_LIT>' . format ( predictions_file_name ) ) <EOL> predictions_file_path = join ( RESULTS_DIR_PATH , predictions_file_name ) <EOL> with open ( predictions_file_path , '<STR_LIT>' ) as predictions_file : <EOL> predictions_file . writelines ( [ '<STR_LIT>' . format ( p ) for p in predictions ] ) <EOL> def save_rmse ( rmse , rmse_file_path , append = True ) : <EOL> write_format = '<STR_LIT>' <EOL> if append : <EOL> write_format = '<STR_LIT>' <EOL> with open ( rmse_file_path , write_format ) as rmse_file : <EOL> rmse_file . write ( '<STR_LIT>' . format ( rmse ) ) </s>
