<s> from __future__ import print_function <EOL> from hyperopt import Trials , STATUS_OK , tpe <EOL> from hyperas import optim <EOL> from hyperas . distributions import uniform <EOL> from keras . models import Sequential <EOL> from keras . layers . core import Dense , Dropout , Activation , Flatten <EOL> from keras . layers . convolutional import Convolution2D , MaxPooling2D <EOL> from keras . optimizers import SGD <EOL> from keras . preprocessing . image import ImageDataGenerator <EOL> from keras . datasets import cifar10 <EOL> from keras . utils import np_utils <EOL> def data ( ) : <EOL> nb_classes = <NUM_LIT:10> <EOL> ( X_train , y_train ) , ( X_test , y_test ) = cifar10 . load_data ( ) <EOL> print ( '<STR_LIT>' , X_train . shape ) <EOL> print ( X_train . shape [ <NUM_LIT:0> ] , '<STR_LIT>' ) <EOL> print ( X_test . shape [ <NUM_LIT:0> ] , '<STR_LIT>' ) <EOL> Y_train = np_utils . to_categorical ( y_train , nb_classes ) <EOL> Y_test = np_utils . to_categorical ( y_test , nb_classes ) <EOL> X_train = X_train . astype ( '<STR_LIT>' ) <EOL> X_test = X_test . astype ( '<STR_LIT>' ) <EOL> X_train /= <NUM_LIT:255> <EOL> X_test /= <NUM_LIT:255> <EOL> datagen = ImageDataGenerator ( <EOL> featurewise_center = False , <EOL> samplewise_center = False , <EOL> featurewise_std_normalization = False , <EOL> samplewise_std_normalization = False , <EOL> zca_whitening = False , <EOL> rotation_range = <NUM_LIT:0> , <EOL> width_shift_range = <NUM_LIT:0.1> , <EOL> height_shift_range = <NUM_LIT:0.1> , <EOL> horizontal_flip = True , <EOL> vertical_flip = False ) <EOL> datagen . fit ( X_train ) <EOL> return datagen , X_train , Y_train , X_test , Y_test <EOL> def model ( datagen , X_train , Y_train , X_test , Y_test ) : <EOL> batch_size = <NUM_LIT:32> <EOL> nb_epoch = <NUM_LIT:200> <EOL> img_rows , img_cols = <NUM_LIT:32> , <NUM_LIT:32> <EOL> img_channels = <NUM_LIT:3> <EOL> model = Sequential ( ) <EOL> model . add ( Convolution2D ( <NUM_LIT:32> , <NUM_LIT:3> , <NUM_LIT:3> , border_mode = '<STR_LIT>' , <EOL> input_shape = ( img_channels , img_rows , img_cols ) ) ) <EOL> model . add ( Activation ( '<STR_LIT:relu>' ) ) <EOL> model . add ( Convolution2D ( <NUM_LIT:32> , <NUM_LIT:3> , <NUM_LIT:3> ) ) <EOL> model . add ( Activation ( '<STR_LIT:relu>' ) ) <EOL> model . add ( MaxPooling2D ( pool_size = ( <NUM_LIT:2> , <NUM_LIT:2> ) ) ) <EOL> model . add ( Dropout ( { { uniform ( <NUM_LIT:0> , <NUM_LIT:1> ) } } ) ) <EOL> model . add ( Convolution2D ( <NUM_LIT:64> , <NUM_LIT:3> , <NUM_LIT:3> , border_mode = '<STR_LIT>' ) ) <EOL> model . add ( Activation ( '<STR_LIT:relu>' ) ) <EOL> model . add ( Convolution2D ( <NUM_LIT:64> , <NUM_LIT:3> , <NUM_LIT:3> ) ) <EOL> model . add ( Activation ( '<STR_LIT:relu>' ) ) <EOL> model . add ( MaxPooling2D ( pool_size = ( <NUM_LIT:2> , <NUM_LIT:2> ) ) ) <EOL> model . add ( Dropout ( { { uniform ( <NUM_LIT:0> , <NUM_LIT:1> ) } } ) ) <EOL> model . add ( Flatten ( ) ) <EOL> model . add ( Dense ( <NUM_LIT> ) ) <EOL> model . add ( Activation ( '<STR_LIT:relu>' ) ) <EOL> model . add ( Dropout ( <NUM_LIT:0.5> ) ) <EOL> model . add ( Dense ( nb_classes ) ) <EOL> model . add ( Activation ( '<STR_LIT>' ) ) <EOL> sgd = SGD ( lr = <NUM_LIT> , decay = <NUM_LIT> , momentum = <NUM_LIT> , nesterov = True ) <EOL> model . compile ( loss = '<STR_LIT>' , <EOL> optimizer = sgd , <EOL> metrics = [ '<STR_LIT>' ] ) <EOL> model . fit_generator ( datagen . flow ( X_train , Y_train , <EOL> batch_size = batch_size ) , <EOL> samples_per_epoch = X_train . shape [ <NUM_LIT:0> ] , <EOL> nb_epoch = nb_epoch , <EOL> validation_data = ( X_test , Y_test ) ) <EOL> score , acc = model . evaluate ( X_test , Y_test , verbose = <NUM_LIT:0> ) <EOL> return { '<STR_LIT>' : - acc , '<STR_LIT:status>' : STATUS_OK , '<STR_LIT>' : model } <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> datagen , X_train , Y_train , X_test , Y_test = data ( ) <EOL> best_run , best_model = optim . minimize ( model = model , <EOL> data = data , <EOL> algo = tpe . suggest , <EOL> max_evals = <NUM_LIT:5> , <EOL> trials = Trials ( ) ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( best_model . evaluate ( X_test , Y_test ) ) </s>
