#NEWLINE# def evaluate_generator( self , generator , val_samples , max_q_size = 10 ) : #NEWLINE# #INDENT# 'Evaluates the model on a data generator. The generator should\n        return the same kind of data as accepted by `test_on_batch`.\n\n        Arguments:\n            generator:\n                generator yielding tuples (inputs, targets)\n                or (inputs, targets, sample_weights)\n            val_samples:\n                total number of samples to generate from `generator`\n                before returning.\n            max_q_size: maximum size for the generator queue\n\n        # Returns\n            Scalar test loss (if the model has a single output and no metrics)\n            or list of scalars (if the model has multiple outputs\n            and/or metrics). The attribute `model.metrics_names` will give you\n            the display labels for the scalar outputs.\n        ' #NEWLINE# self . _make_test_function ( ) #NEWLINE# processed_samples = 0 #NEWLINE# wait_time = 0.01 #NEWLINE# all_outs = [ ] #NEWLINE# weights = [ ] #NEWLINE# ( data_gen_queue , _stop ) = generator_queue ( generator , max_q_size = max_q_size ) #NEWLINE# while ( processed_samples < val_samples ) : #NEWLINE# #INDENT# generator_output = None #NEWLINE# while ( not _stop . is_set ( ) ) : #NEWLINE# #INDENT# if ( not data_gen_queue . empty ( ) ) : #NEWLINE# #INDENT# generator_output = data_gen_queue . get ( ) #NEWLINE# break #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# time . sleep ( wait_time ) #NEWLINE# #UNINDENT# #UNINDENT# if ( not hasattr ( generator_output , '__len__' ) ) : #NEWLINE# #INDENT# _stop . set ( ) #NEWLINE# raise Exception ( ( 'output of generator should be a tuple (x, y, sample_weight) or (x, y). Found: ' + str ( generator_output ) ) ) #NEWLINE# #UNINDENT# if ( len ( generator_output ) == 2 ) : #NEWLINE# #INDENT# ( x , y ) = generator_output #NEWLINE# sample_weight = None #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( len ( generator_output ) == 3 ) : #NEWLINE# #INDENT# ( x , y , sample_weight ) = generator_output #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# _stop . set ( ) #NEWLINE# raise Exception ( ( 'output of generator should be a tuple (x, y, sample_weight) or (x, y). Found: ' + str ( generator_output ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# try : #NEWLINE# #INDENT# outs = self . test_on_batch ( x , y , sample_weight = sample_weight ) #NEWLINE# #UNINDENT# except Exception as e : #NEWLINE# #INDENT# _stop . set ( ) #NEWLINE# raise #NEWLINE# #UNINDENT# if ( type ( x ) is list ) : #NEWLINE# #INDENT# nb_samples = len ( x [ 0 ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( type ( x ) is dict ) : #NEWLINE# #INDENT# nb_samples = len ( list ( x . values ( ) ) [ 0 ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# nb_samples = len ( x ) #NEWLINE# #UNINDENT# #UNINDENT# all_outs . append ( outs ) #NEWLINE# processed_samples += nb_samples #NEWLINE# weights . append ( nb_samples ) #NEWLINE# #UNINDENT# _stop . set ( ) #NEWLINE# if ( type ( generator_output ) is not list ) : #NEWLINE# #INDENT# return np . average ( np . asarray ( all_outs ) , weights = weights ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# averages = [ ] #NEWLINE# for i in range ( len ( outs ) ) : #NEWLINE# #INDENT# averages . append ( np . average ( [ out [ i ] for out in all_outs ] , weights = weights ) ) #NEWLINE# #UNINDENT# return averages