<s> from __future__ import print_function <EOL> from hyperopt import Trials , STATUS_OK , tpe <EOL> from hyperas import optim <EOL> from hyperas . distributions import choice , uniform , conditional <EOL> from keras . models import Sequential <EOL> from keras . layers . core import Dense , Dropout , Activation <EOL> from keras . datasets import mnist <EOL> from keras . utils import np_utils <EOL> def data ( ) : <EOL> '''<STR_LIT>''' <EOL> ( X_train , y_train ) , ( X_test , y_test ) = mnist . load_data ( ) <EOL> X_train = X_train . reshape ( <NUM_LIT> , <NUM_LIT> ) <EOL> X_test = X_test . reshape ( <NUM_LIT> , <NUM_LIT> ) <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> nb_classes = <NUM_LIT:10> <EOL> Y_train = np_utils . to_categorical ( y_train , nb_classes ) <EOL> Y_test = np_utils . to_categorical ( y_test , nb_classes ) <EOL> return X_train , Y_train , X_test , Y_test <EOL> def model ( X_train , Y_train , X_test , Y_test ) : <EOL> '''<STR_LIT>''' <EOL> model = Sequential ( ) <EOL> model . add ( Dense ( <NUM_LIT> , input_shape = ( <NUM_LIT> , ) ) ) <EOL> model . add ( Activation ( '<STR_LIT:relu>' ) ) <EOL> model . add ( Dropout ( { { uniform ( <NUM_LIT:0> , <NUM_LIT:1> ) } } ) ) <EOL> model . add ( Dense ( { { choice ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) } } ) ) <EOL> model . add ( Activation ( { { choice ( [ '<STR_LIT:relu>' , '<STR_LIT>' ] ) } } ) ) <EOL> model . add ( Dropout ( { { uniform ( <NUM_LIT:0> , <NUM_LIT:1> ) } } ) ) <EOL> if conditional ( { { choice ( [ '<STR_LIT>' , '<STR_LIT>' ] ) } } ) == '<STR_LIT>' : <EOL> model . add ( Dense ( <NUM_LIT:100> ) ) <EOL> model . add ( { { choice ( [ Dropout ( <NUM_LIT:0.5> ) , Activation ( '<STR_LIT>' ) ] ) } } ) <EOL> model . add ( Activation ( '<STR_LIT:relu>' ) ) <EOL> model . add ( Dense ( <NUM_LIT:10> ) ) <EOL> model . add ( Activation ( '<STR_LIT>' ) ) <EOL> model . compile ( loss = '<STR_LIT>' , <EOL> optimizer = { { choice ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) } } , <EOL> metrics = [ '<STR_LIT>' ] ) <EOL> model . fit ( X_train , Y_train , <EOL> batch_size = { { choice ( [ <NUM_LIT:64> , <NUM_LIT> ] ) } } , <EOL> nb_epoch = <NUM_LIT:1> , <EOL> verbose = <NUM_LIT:2> , <EOL> validation_data = ( X_test , Y_test ) ) <EOL> score , acc = model . evaluate ( X_test , Y_test , verbose = <NUM_LIT:0> ) <EOL> print ( '<STR_LIT>' , acc ) <EOL> return { '<STR_LIT>' : - acc , '<STR_LIT:status>' : STATUS_OK , '<STR_LIT>' : model } <EOL> if __name__ == '<STR_LIT:__main__>' : <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> X_train , Y_train , X_test , Y_test = data ( ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( best_model . evaluate ( X_test , Y_test ) ) </s>
