<s> """<STR_LIT>""" <EOL> import sys <EOL> import os <EOL> import glob <EOL> import theano <EOL> import theano . tensor as T <EOL> import numpy as np <EOL> import scipy <EOL> import load_data <EOL> TARGET_PATH = "<STR_LIT>" <EOL> TARGET_PATH_SEPARATE = "<STR_LIT>" <EOL> TARGET_PATH_UNIFORM = "<STR_LIT>" <EOL> predictions_valid_dir = "<STR_LIT>" <EOL> predictions_test_dir = "<STR_LIT>" <EOL> y_train = np . load ( "<STR_LIT>" ) <EOL> train_ids = load_data . train_ids <EOL> test_ids = load_data . test_ids <EOL> num_train = len ( train_ids ) <EOL> num_test = len ( test_ids ) <EOL> num_valid = num_train // <NUM_LIT:10> <EOL> num_train -= num_valid <EOL> y_valid = y_train [ num_train : ] <EOL> y_train = y_train [ : num_train ] <EOL> valid_ids = train_ids [ num_train : ] <EOL> train_ids = train_ids [ : num_train ] <EOL> train_indices = np . arange ( num_train ) <EOL> valid_indices = np . arange ( num_train , num_train + num_valid ) <EOL> test_indices = np . arange ( num_test ) <EOL> predictions_test_paths = glob . glob ( os . path . join ( predictions_test_dir , "<STR_LIT>" ) ) <EOL> predictions_valid_paths = [ os . path . join ( predictions_valid_dir , os . path . basename ( path ) ) for path in predictions_test_paths ] <EOL> print "<STR_LIT>" <EOL> predictions_list = [ load_data . load_gz ( path ) for path in predictions_valid_paths ] <EOL> predictions_stack = np . array ( predictions_list ) . astype ( theano . config . floatX ) <EOL> del predictions_list <EOL> print <EOL> print "<STR_LIT>" <EOL> individual_prediction_errors = np . sqrt ( ( ( predictions_stack - y_valid [ None ] ) ** <NUM_LIT:2> ) . reshape ( predictions_stack . shape [ <NUM_LIT:0> ] , - <NUM_LIT:1> ) . mean ( <NUM_LIT:1> ) ) <EOL> print <EOL> print "<STR_LIT>" <EOL> X = theano . shared ( predictions_stack ) <EOL> t = theano . shared ( y_valid ) <EOL> W = T . vector ( '<STR_LIT>' ) <EOL> s = T . nnet . softmax ( W ) . reshape ( ( W . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> weighted_avg_predictions = T . sum ( X * s , axis = <NUM_LIT:0> ) <EOL> error = T . mean ( ( weighted_avg_predictions - t ) ** <NUM_LIT:2> ) <EOL> grad = T . grad ( error , W ) <EOL> f = theano . function ( [ W ] , error ) <EOL> g = theano . function ( [ W ] , grad ) <EOL> s2 = T . nnet . softmax ( W . reshape ( ( <NUM_LIT> , predictions_stack . shape [ <NUM_LIT:0> ] ) ) ) . dimshuffle ( <NUM_LIT:1> , '<STR_LIT:x>' , <NUM_LIT:0> ) <EOL> weighted_avg_predictions2 = T . sum ( X * s2 , axis = <NUM_LIT:0> ) <EOL> error2 = T . mean ( ( weighted_avg_predictions2 - t ) ** <NUM_LIT:2> ) <EOL> grad2 = T . grad ( error2 , W ) <EOL> f2 = theano . function ( [ W ] , error2 ) <EOL> g2 = theano . function ( [ W ] , grad2 ) <EOL> print <EOL> print "<STR_LIT>" <EOL> w_init = np . random . randn ( predictions_stack . shape [ <NUM_LIT:0> ] ) . astype ( theano . config . floatX ) * <NUM_LIT> <EOL> w_zero = np . zeros ( predictions_stack . shape [ <NUM_LIT:0> ] , dtype = theano . config . floatX ) <EOL> out , res , _ = scipy . optimize . fmin_l_bfgs_b ( f , w_init , fprime = g , pgtol = <NUM_LIT> , epsilon = <NUM_LIT> , maxfun = <NUM_LIT> ) <EOL> rmse = np . sqrt ( res ) <EOL> out_s = np . exp ( out ) <EOL> out_s /= out_s . sum ( ) <EOL> rmse_uniform = np . sqrt ( f ( w_zero ) ) <EOL> print <EOL> print "<STR_LIT>" <EOL> w_init2 = np . random . randn ( predictions_stack . shape [ <NUM_LIT:0> ] * <NUM_LIT> ) . astype ( theano . config . floatX ) * <NUM_LIT> <EOL> out2 , res2 , _ = scipy . optimize . fmin_l_bfgs_b ( f2 , w_init2 , fprime = g2 , pgtol = <NUM_LIT> , epsilon = <NUM_LIT> , maxfun = <NUM_LIT> ) <EOL> rmse2 = np . sqrt ( res2 ) <EOL> out_s2 = np . exp ( out2 ) . reshape ( <NUM_LIT> , predictions_stack . shape [ <NUM_LIT:0> ] ) . T <EOL> out_s2 /= out_s2 . sum ( <NUM_LIT:0> ) [ None , : ] <EOL> print <EOL> print "<STR_LIT>" <EOL> for path , error in zip ( predictions_valid_paths , individual_prediction_errors ) : <EOL> print "<STR_LIT>" % ( error , os . path . basename ( path ) ) <EOL> print <EOL> print "<STR_LIT>" <EOL> for path , weight in zip ( predictions_valid_paths , out_s ) : <EOL> print "<STR_LIT>" % ( weight , os . path . basename ( path ) ) <EOL> print <EOL> print "<STR_LIT>" % rmse <EOL> print "<STR_LIT>" % rmse2 <EOL> print "<STR_LIT>" % rmse_uniform <EOL> print <EOL> print "<STR_LIT>" <EOL> blended_predictions = None <EOL> blended_predictions_separate = None <EOL> blended_predictions_uniform = None <EOL> for path , weight , weights_separate in zip ( predictions_test_paths , out_s , out_s2 ) : <EOL> predictions = load_data . load_gz ( path ) <EOL> predictions_uniform = predictions * ( <NUM_LIT:1.0> / len ( predictions_test_paths ) ) <EOL> predictions_separate = predictions * weights_separate [ None , : ] <EOL> predictions *= weight <EOL> if blended_predictions is None : <EOL> blended_predictions = predictions <EOL> blended_predictions_separate = predictions_separate <EOL> blended_predictions_uniform = predictions_uniform <EOL> else : <EOL> blended_predictions += predictions <EOL> blended_predictions_separate += predictions_separate <EOL> blended_predictions_uniform += predictions_uniform <EOL> print <EOL> print "<STR_LIT>" % TARGET_PATH <EOL> load_data . save_gz ( TARGET_PATH , blended_predictions ) <EOL> print <EOL> print "<STR_LIT>" % TARGET_PATH_SEPARATE <EOL> load_data . save_gz ( TARGET_PATH_SEPARATE , blended_predictions_separate ) <EOL> print <EOL> print "<STR_LIT>" % TARGET_PATH_UNIFORM <EOL> load_data . save_gz ( TARGET_PATH_UNIFORM , blended_predictions_uniform ) <EOL> print <EOL> print "<STR_LIT>" </s>
