<s> '''<STR_LIT>''' <EOL> from keras . models import Sequential , model_from_yaml <EOL> from keras . layers . core import Dense , Dropout , MaxoutDense , Activation , Flatten , Merge <EOL> from keras . layers . normalization import LRN2D <EOL> from keras . layers import containers <EOL> from keras . layers . convolutional import MaxPooling2D , Convolution2D <EOL> from keras . optimizers import SGD , RMSprop , Adagrad , Adam <EOL> from keras import regularizers <EOL> from keras . callbacks import EarlyStopping , ModelCheckpoint , Callback <EOL> import numpy as np <EOL> from sklearn . metrics import roc_curve , auc <EOL> from sklearn . cross_validation import KFold <EOL> from viz import * <EOL> class ROCModelCheckpoint ( Callback ) : <EOL> def __init__ ( self , filepath , X , y , weights , verbose = True ) : <EOL> super ( Callback , self ) . __init__ ( ) <EOL> self . X , self . y , self . weights = X , y , weights <EOL> self . verbose = verbose <EOL> self . filepath = filepath <EOL> self . best = <NUM_LIT:0.0> <EOL> def on_epoch_end ( self , epoch , logs = { } ) : <EOL> yh = self . model . predict ( self . X , verbose = True ) . ravel ( ) <EOL> print yh [ : <NUM_LIT:30> ] <EOL> fpr , tpr , _ = roc_curve ( self . y , yh , sample_weight = self . weights ) <EOL> select = ( tpr > <NUM_LIT:0.1> ) & ( tpr < <NUM_LIT> ) <EOL> current = auc ( tpr [ select ] , <NUM_LIT:1> / fpr [ select ] ) <EOL> if current > self . best : <EOL> if self . verbose > <NUM_LIT:0> : <EOL> print ( "<STR_LIT>" <EOL> % ( epoch , '<STR_LIT>' , self . best , current , self . filepath ) ) <EOL> self . best = current <EOL> self . model . save_weights ( self . filepath , overwrite = True ) <EOL> else : <EOL> if self . verbose > <NUM_LIT:0> : <EOL> print ( "<STR_LIT>" % ( epoch , '<STR_LIT>' ) ) <EOL> class NDWeights ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , bins ) : <EOL> super ( NDWeights , self ) . __init__ ( ) <EOL> self . bins = bins <EOL> def fit ( self , X , truth , reference ) : <EOL> H_s , _ = np . histogramdd ( X [ truth == <NUM_LIT:1> ] , bins = self . bins , normed = False ) <EOL> H_b , _ = np . histogramdd ( X [ truth == <NUM_LIT:0> ] , bins = self . bins , normed = False ) <EOL> H_ref , _ = np . histogramdd ( reference , bins = self . bins , normed = False ) <EOL> self . flat_cube_s = H_ref / H_s <EOL> self . flat_cube_b = H_ref / H_b <EOL> def predict ( self , X , truth ) : <EOL> ix = [ ( self . bins [ i ] . searchsorted ( X [ : , i ] ) - <NUM_LIT:1> ) for i in xrange ( len ( self . bins ) ) ] <EOL> ix = np . array ( ix ) . T <EOL> print ix <EOL> weights = [ ] <EOL> for i , label in zip ( ix , truth ) : <EOL> if label == <NUM_LIT:1> : <EOL> w = np . copy ( self . flat_cube_s [ i [ <NUM_LIT:0> ] ] ) <EOL> else : <EOL> w = np . copy ( self . flat_cube_b [ i [ <NUM_LIT:0> ] ] ) <EOL> for j in xrange ( <NUM_LIT:1> , len ( self . bins ) ) : <EOL> w = w [ i [ j ] ] <EOL> weights . append ( w ) <EOL> weights = np . array ( weights ) <EOL> weights [ np . isinf ( weights ) ] = weights [ np . isfinite ( weights ) ] . max ( ) <EOL> return weights <EOL> print '<STR_LIT>' <EOL> data = np . load ( '<STR_LIT>' ) <EOL> print '<STR_LIT>' . format ( data . shape [ <NUM_LIT:0> ] ) <EOL> print '<STR_LIT>' <EOL> X = data [ '<STR_LIT:image>' ] . reshape ( ( data . shape [ <NUM_LIT:0> ] , <NUM_LIT> ** <NUM_LIT:2> ) ) . astype ( '<STR_LIT>' ) <EOL> y = data [ '<STR_LIT>' ] . astype ( '<STR_LIT>' ) <EOL> print '<STR_LIT>' <EOL> signal , pt , mass , tau_21 = data [ '<STR_LIT>' ] , data [ '<STR_LIT>' ] , data [ '<STR_LIT>' ] , data [ '<STR_LIT>' ] <EOL> signal = ( signal == <NUM_LIT:1> ) <EOL> background = ( signal == False ) <EOL> window = ( tau_21 < <NUM_LIT> ) & ( tau_21 > <NUM_LIT> ) <EOL> pt , mass , tau_21 , signal , background = pt [ window ] , mass [ window ] , tau_21 [ window ] , signal [ window ] , background [ window ] <EOL> n_obs = int ( window . sum ( ) ) <EOL> ref = np . zeros ( ( n_obs , <NUM_LIT:3> ) ) <EOL> ref [ : , <NUM_LIT:0> ] = pt <EOL> ref [ : , <NUM_LIT:1> ] = mass <EOL> ref [ : , <NUM_LIT:2> ] = tau_21 <EOL> cube = np . zeros ( ( n_obs / <NUM_LIT:2> , <NUM_LIT:3> ) ) <EOL> cube [ : , <NUM_LIT:0> ] = np . random . uniform ( <NUM_LIT> , <NUM_LIT> , n_obs / <NUM_LIT:2> ) <EOL> cube [ : , <NUM_LIT:1> ] = np . random . uniform ( <NUM_LIT> , <NUM_LIT> , n_obs / <NUM_LIT:2> ) <EOL> cube [ : , <NUM_LIT:2> ] = np . random . uniform ( <NUM_LIT> , <NUM_LIT> , n_obs / <NUM_LIT:2> ) <EOL> binning = ( <EOL> np . linspace ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:20> ) , <EOL> np . linspace ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) , <EOL> np . linspace ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) <EOL> ) <EOL> ndweights = NDWeights ( binning ) <EOL> ndweights . fit ( ref , signal , cube ) <EOL> cube_weights = ndweights . predict ( ref , signal ) <EOL> cube_weights [ np . isinf ( cube_weights ) ] = cube_weights [ np . isfinite ( cube_weights ) ] . max ( ) <EOL> print '<STR_LIT>' <EOL> X , y = X [ window ] , y [ window ] <EOL> idx = range ( X . shape [ <NUM_LIT:0> ] ) <EOL> np . random . shuffle ( idx ) <EOL> X = X [ idx ] [ : <NUM_LIT> ] <EOL> y = y [ idx ] [ : <NUM_LIT> ] <EOL> cube_weights = cube_weights [ idx ] . astype ( '<STR_LIT>' ) [ : <NUM_LIT> ] <EOL> try : <EOL> kf = KFold ( X . shape [ <NUM_LIT:0> ] , <NUM_LIT:10> ) <EOL> foldN = <NUM_LIT:1> <EOL> for train_ix , test_ix in kf : <EOL> print '<STR_LIT>' . format ( foldN ) <EOL> print '<STR_LIT>' <EOL> dl = Sequential ( ) <EOL> dl . add ( MaxoutDense ( <NUM_LIT> , <NUM_LIT:5> , input_shape = ( <NUM_LIT> , ) , init = '<STR_LIT>' ) ) <EOL> dl . add ( Dropout ( <NUM_LIT> ) ) <EOL> dl . add ( MaxoutDense ( <NUM_LIT> , <NUM_LIT:5> , init = '<STR_LIT>' ) ) <EOL> dl . add ( Dropout ( <NUM_LIT> ) ) <EOL> dl . add ( Dense ( <NUM_LIT:64> ) ) <EOL> dl . add ( Activation ( '<STR_LIT:relu>' ) ) <EOL> dl . add ( Dropout ( <NUM_LIT> ) ) <EOL> dl . add ( Dense ( <NUM_LIT> ) ) <EOL> dl . add ( Activation ( '<STR_LIT:relu>' ) ) <EOL> dl . add ( Dropout ( <NUM_LIT> ) ) <EOL> dl . add ( Dense ( <NUM_LIT:1> ) ) <EOL> dl . add ( Activation ( '<STR_LIT>' ) ) <EOL> print '<STR_LIT>' <EOL> dl . compile ( loss = '<STR_LIT>' , optimizer = '<STR_LIT>' , class_mode = '<STR_LIT>' ) <EOL> print '<STR_LIT>' <EOL> h = dl . fit ( X [ train_ix ] , y [ train_ix ] , batch_size = <NUM_LIT:32> , nb_epoch = <NUM_LIT:50> , show_accuracy = True , <EOL> validation_data = ( X [ test_ix ] , y [ test_ix ] ) , <EOL> callbacks = <EOL> [ <EOL> EarlyStopping ( verbose = True , patience = <NUM_LIT:10> , monitor = '<STR_LIT>' ) , <EOL> ModelCheckpoint ( '<STR_LIT>' . format ( foldN ) , monitor = '<STR_LIT>' , verbose = True , save_best_only = True ) , <EOL> ROCModelCheckpoint ( '<STR_LIT>' . format ( foldN ) , X [ test_ix ] , y [ test_ix ] , cube_weights [ test_ix ] , verbose = True ) <EOL> ] , <EOL> sample_weight = cube_weights [ train_ix ] <EOL> ) <EOL> foldN += <NUM_LIT:1> <EOL> except KeyboardInterrupt : <EOL> print '<STR_LIT>' <EOL> with open ( '<STR_LIT>' , '<STR_LIT:wb>' ) as f : <EOL> f . write ( dl . to_yaml ( ) ) </s>
