<s> from __future__ import division , print_function , absolute_import <EOL> import numpy as np <EOL> import tensorflow as tf <EOL> from tensorflow . python . training import optimizer as tf_optimizer <EOL> import tflearn <EOL> from . . import callbacks <EOL> from . . config import init_training_mode <EOL> from . . utils import to_list , id_generator , check_dir_name , standarize_dict , get_dict_first_element , make_batches , slice_array , check_scope_path <EOL> from . summarizer import summaries , summarize , summarize_gradients , summarize_variables , summarize_activations <EOL> class Trainer ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , train_ops , graph = None , clip_gradients = <NUM_LIT> , <EOL> tensorboard_dir = "<STR_LIT>" , <EOL> tensorboard_verbose = <NUM_LIT:0> , checkpoint_path = None , <EOL> max_checkpoints = None , <EOL> keep_checkpoint_every_n_hours = <NUM_LIT> , random_seed = None , <EOL> session = None ) : <EOL> self . graph = tf . get_default_graph ( ) <EOL> if graph : <EOL> self . graph = graph <EOL> with self . graph . as_default ( ) : <EOL> init_training_mode ( ) <EOL> train_ops = to_list ( train_ops ) <EOL> duplicate_identical_ops ( train_ops ) <EOL> if random_seed : <EOL> tf . set_random_seed ( random_seed ) <EOL> self . restored = False <EOL> self . tensorboard_dir = check_dir_name ( tensorboard_dir ) <EOL> self . training_step = <NUM_LIT:0> <EOL> self . train_ops = to_list ( train_ops ) <EOL> self . validate_trainop_names ( ) <EOL> self . global_loss = None <EOL> self . global_step = tf . Variable ( <NUM_LIT:0.> , name = '<STR_LIT>' , <EOL> trainable = False ) <EOL> self . incr_global_step = tf . assign ( self . global_step , <EOL> tf . add ( self . global_step , <NUM_LIT:1> ) ) <EOL> config = None <EOL> tflearn_conf = tf . get_collection ( tf . GraphKeys . GRAPH_CONFIG ) <EOL> if tflearn_conf : <EOL> config = tflearn_conf [ <NUM_LIT:0> ] <EOL> if not session : <EOL> self . session = tf . Session ( config = config ) <EOL> else : <EOL> self . session = session <EOL> self . restored = True <EOL> for i , train_op in enumerate ( self . train_ops ) : <EOL> if len ( train_ops ) == <NUM_LIT:1> : <EOL> train_op . scope_name = "<STR_LIT>" <EOL> train_op . initialize_training_ops ( i , self . session , <EOL> tensorboard_verbose , <EOL> clip_gradients ) <EOL> self . saver = tf . train . Saver ( <EOL> max_to_keep = max_checkpoints , <EOL> keep_checkpoint_every_n_hours = keep_checkpoint_every_n_hours ) <EOL> all_vars = tf . get_collection ( tf . GraphKeys . VARIABLES ) <EOL> excl_vars = tf . get_collection ( tf . GraphKeys . EXCL_RESTORE_VARS ) <EOL> to_restore = [ item for item in all_vars if item not in excl_vars ] <EOL> self . restorer = tf . train . Saver ( <EOL> var_list = to_restore , <EOL> max_to_keep = max_checkpoints , <EOL> keep_checkpoint_every_n_hours = keep_checkpoint_every_n_hours ) <EOL> self . checkpoint_path = checkpoint_path <EOL> if not self . restored : <EOL> init = tf . initialize_all_variables ( ) <EOL> self . session . run ( init ) <EOL> def fit ( self , feed_dicts , n_epoch = <NUM_LIT:10> , val_feed_dicts = None , show_metric = False , <EOL> snapshot_step = None , snapshot_epoch = True , shuffle_all = None , <EOL> run_id = None ) : <EOL> """<STR_LIT>""" <EOL> if not run_id : <EOL> run_id = id_generator ( <NUM_LIT:6> ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" + run_id ) <EOL> print ( "<STR_LIT>" + self . tensorboard_dir ) <EOL> if isinstance ( shuffle_all , bool ) : <EOL> for t in self . train_ops : t . shuffle = shuffle_all <EOL> with self . graph . as_default ( ) : <EOL> self . summ_writer = tf . train . SummaryWriter ( <EOL> self . tensorboard_dir + run_id , self . session . graph_def ) <EOL> feed_dicts = to_list ( feed_dicts ) <EOL> for d in feed_dicts : standarize_dict ( d ) <EOL> val_feed_dicts = to_list ( val_feed_dicts ) <EOL> if val_feed_dicts : [ standarize_dict ( d ) for d in val_feed_dicts ] <EOL> validation_split ( val_feed_dicts , feed_dicts ) <EOL> termlogger = callbacks . TermLogger ( self . training_step ) <EOL> modelsaver = callbacks . ModelSaver ( self . save , <EOL> self . training_step , <EOL> self . checkpoint_path , <EOL> snapshot_epoch ) <EOL> for i , train_op in enumerate ( self . train_ops ) : <EOL> vd = val_feed_dicts [ i ] if val_feed_dicts else None <EOL> train_op . initialize_fit ( feed_dicts [ i ] , vd , show_metric , <EOL> self . summ_writer ) <EOL> metric_term_name = None <EOL> if train_op . metric is not None : <EOL> if hasattr ( train_op . metric , '<STR_LIT>' ) : <EOL> metric_term_name = train_op . metric . m_name <EOL> else : <EOL> metric_term_name = train_op . metric . name . split ( '<STR_LIT::>' ) [ <NUM_LIT:0> ] <EOL> termlogger . add ( train_op . n_train_samples , <EOL> val_size = train_op . n_val_samples , <EOL> metric_name = metric_term_name , <EOL> name = train_op . name ) <EOL> max_batches_len = np . max ( [ t . n_batches for t in self . train_ops ] ) <EOL> termlogger . on_train_begin ( ) <EOL> modelsaver . on_epoch_begin ( ) <EOL> try : <EOL> for epoch in range ( n_epoch ) : <EOL> termlogger . on_epoch_begin ( ) <EOL> modelsaver . on_epoch_begin ( ) <EOL> for batch_step in range ( max_batches_len ) : <EOL> self . training_step += <NUM_LIT:1> <EOL> termlogger . on_batch_begin ( ) <EOL> modelsaver . on_batch_begin ( ) <EOL> global_loss , global_acc = <NUM_LIT:0.> , <NUM_LIT:0.> <EOL> for i , train_op in enumerate ( self . train_ops ) : <EOL> termlogger . on_sub_epoch_begin ( ) <EOL> modelsaver . on_sub_batch_begin ( ) <EOL> snapshot = train_op . _train ( self . training_step , <EOL> snapshot_epoch , <EOL> snapshot_step , <EOL> show_metric ) <EOL> global_loss += train_op . loss_value <EOL> if train_op . acc_value and global_acc : <EOL> global_acc += train_op . acc_value / len ( <EOL> self . train_ops ) <EOL> else : <EOL> global_acc = None <EOL> termlogger . on_sub_batch_end ( i , train_op . epoch , <EOL> train_op . step , <EOL> train_op . loss_value , <EOL> train_op . acc_value , <EOL> train_op . val_loss , <EOL> train_op . val_acc ) <EOL> modelsaver . on_sub_batch_end ( ) <EOL> self . session . run ( self . incr_global_step ) <EOL> termlogger . on_batch_end ( global_loss , global_acc , <EOL> snapshot ) <EOL> modelsaver . on_batch_end ( snapshot ) <EOL> termlogger . on_epoch_end ( ) <EOL> modelsaver . on_epoch_end ( ) <EOL> finally : <EOL> termlogger . on_train_end ( ) <EOL> modelsaver . on_train_end ( ) <EOL> def save ( self , model_file , global_step = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> l = tf . get_collection_ref ( "<STR_LIT>" ) <EOL> except Exception : <EOL> l = tf . get_collection ( "<STR_LIT>" ) <EOL> l_stags = list ( l ) <EOL> del l [ : ] <EOL> if model_file [ <NUM_LIT:0> ] not in [ '<STR_LIT:/>' , '<STR_LIT>' ] : model_file = '<STR_LIT>' + model_file <EOL> self . saver . save ( self . session , model_file , global_step = global_step ) <EOL> for t in l_stags : <EOL> tf . add_to_collection ( "<STR_LIT>" , t ) <EOL> def restore ( self , model_file ) : <EOL> """<STR_LIT>""" <EOL> self . close_session ( ) <EOL> self . session = tf . Session ( ) <EOL> self . session . run ( tf . initialize_all_variables ( ) ) <EOL> self . restorer . restore ( self . session , model_file ) <EOL> for o in self . train_ops : <EOL> o . session = self . session <EOL> self . restored = True <EOL> self . training_step = int ( self . global_step . eval ( self . session ) ) <EOL> def close_session ( self ) : <EOL> """<STR_LIT>""" <EOL> self . session . close ( ) <EOL> def validate_trainop_names ( self ) : <EOL> """<STR_LIT>""" <EOL> t_len = len ( self . train_ops ) <EOL> for i in range ( t_len ) : <EOL> if not self . train_ops [ i ] . name : <EOL> self . train_ops [ i ] . name = '<STR_LIT>' <EOL> self . train_ops [ i ] . scope_name = '<STR_LIT>' <EOL> for i in range ( t_len ) : <EOL> dupl = <NUM_LIT:0> <EOL> for j in range ( i + <NUM_LIT:1> , t_len ) : <EOL> if not self . train_ops [ i ] . name : <EOL> break <EOL> if self . train_ops [ i ] . name == self . train_ops [ j ] . name : <EOL> if dupl == <NUM_LIT:0> : <EOL> self . train_ops [ i ] . name += '<STR_LIT:_>' + str ( dupl ) <EOL> self . train_ops [ i ] . scope_name = self . train_ops [ i ] . name <EOL> dupl += <NUM_LIT:1> <EOL> self . train_ops [ j ] . name += '<STR_LIT:_>' + str ( dupl ) <EOL> self . train_ops [ j ] . scope_name = self . train_ops [ j ] . name <EOL> class TrainOp ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , loss , optimizer , metric = None , batch_size = <NUM_LIT:64> , ema = <NUM_LIT:0.> , <EOL> trainable_vars = None , shuffle = True , step_tensor = None , <EOL> name = None , graph = None ) : <EOL> self . graph = tf . get_default_graph ( ) <EOL> if graph : <EOL> self . graph = graph <EOL> self . name = name <EOL> self . scope_name = name <EOL> self . loss = loss <EOL> self . optimizer = optimizer <EOL> self . metric = metric <EOL> self . metric_summ_name = "<STR_LIT>" <EOL> if metric is not None : <EOL> self . metric_summ_name = metric . name . split ( '<STR_LIT:/>' ) [ <NUM_LIT:0> ] <EOL> self . grad = None <EOL> self . apply_grad = None <EOL> self . summ_op = None <EOL> self . val_summary_op = None <EOL> self . train_vars = trainable_vars <EOL> self . shuffle = shuffle <EOL> self . epoch = <NUM_LIT:0> <EOL> self . step = <NUM_LIT:0> <EOL> self . batches = None <EOL> self . batch_index = <NUM_LIT:0> <EOL> self . batch_start = <NUM_LIT:0> <EOL> self . batch_end = <NUM_LIT:0> <EOL> self . batch_size = batch_size <EOL> self . data_size = <NUM_LIT:0> <EOL> self . n_batches = <NUM_LIT:0> <EOL> self . ema = ema <EOL> self . feed_dict = None <EOL> self . val_feed_dict = None <EOL> self . loss_value = None <EOL> self . val_loss = None <EOL> self . acc_value = None <EOL> self . val_acc = None <EOL> if step_tensor is None : <EOL> with self . graph . as_default ( ) : <EOL> self . training_steps = tf . Variable ( <NUM_LIT:0.> , name = "<STR_LIT>" , <EOL> trainable = False ) <EOL> else : <EOL> self . training_steps = step_tensor <EOL> if not isinstance ( self . loss , tf . Tensor ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if not isinstance ( self . optimizer , tf_optimizer . Optimizer ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if self . train_vars is None : <EOL> self . train_vars = tf . trainable_variables ( ) <EOL> else : <EOL> self . train_var = to_list ( self . train_vars ) <EOL> self . train = None <EOL> def initialize_training_ops ( self , i , session , tensorboard_verbose , <EOL> clip_gradients ) : <EOL> """<STR_LIT>""" <EOL> self . session = session <EOL> self . val_loss_T = tf . Variable ( <NUM_LIT:0.> , name = '<STR_LIT>' , trainable = False ) <EOL> self . val_acc_T = tf . Variable ( <NUM_LIT:0.> , name = '<STR_LIT>' , trainable = False ) <EOL> if self . metric is not None : <EOL> self . acc_averages = tf . train . ExponentialMovingAverage ( <NUM_LIT> , self . training_steps , <EOL> name = '<STR_LIT>' ) <EOL> acc_avg_op = self . acc_averages . apply ( [ self . metric ] ) <EOL> else : <EOL> acc_avg_op = tf . no_op ( ) <EOL> with tf . name_scope ( self . name ) : <EOL> lss = [ self . loss ] + tf . get_collection ( tf . GraphKeys . REGULARIZATION_LOSSES ) <EOL> total_loss = tf . add_n ( lss , name = "<STR_LIT>" ) <EOL> loss_avg_op = summaries . add_loss_summaries ( <EOL> total_loss , <EOL> self . loss , <EOL> regul_losses_collection_key = tf . GraphKeys . REGULARIZATION_LOSSES , <EOL> name_prefix = self . scope_name , <EOL> summaries_collection_key = self . name + "<STR_LIT>" , <EOL> exp_moving_avg = <NUM_LIT> , <EOL> ema_num_updates = self . training_steps ) <EOL> with tf . control_dependencies ( [ loss_avg_op , acc_avg_op ] ) : <EOL> self . grad = tf . gradients ( total_loss , self . train_vars ) <EOL> if clip_gradients > <NUM_LIT:0.0> : <EOL> self . grad , self . grad_norm = tf . clip_by_global_norm ( self . grad , clip_gradients ) <EOL> self . grad = list ( zip ( self . grad , self . train_vars ) ) <EOL> self . apply_grad = self . optimizer . apply_gradients ( <EOL> grads_and_vars = self . grad , <EOL> global_step = self . training_steps , <EOL> name = "<STR_LIT>" + str ( i ) ) <EOL> self . create_summaries ( tensorboard_verbose ) <EOL> if self . ema > <NUM_LIT:0.> : <EOL> var_averages = tf . train . ExponentialMovingAverage ( <EOL> self . ema , self . training_steps ) <EOL> var_averages_op = var_averages . apply ( self . train_vars ) <EOL> with tf . control_dependencies ( [ var_averages_op ] ) : <EOL> with tf . control_dependencies ( [ self . apply_grad ] ) : <EOL> self . train = tf . no_op ( name = "<STR_LIT>" + str ( i ) ) <EOL> else : <EOL> with tf . control_dependencies ( [ self . apply_grad ] ) : <EOL> self . train = tf . no_op ( name = "<STR_LIT>" + str ( i ) ) <EOL> def initialize_fit ( self , feed_dict , val_feed_dict , show_metric , <EOL> summ_writer ) : <EOL> """<STR_LIT>""" <EOL> self . summary_writer = summ_writer <EOL> self . feed_dict = feed_dict <EOL> self . val_feed_dict = val_feed_dict <EOL> self . n_train_samples = len ( get_dict_first_element ( feed_dict ) ) <EOL> self . n_val_samples = <NUM_LIT:0> <EOL> if val_feed_dict : <EOL> self . n_val_samples = len ( get_dict_first_element ( val_feed_dict ) ) <EOL> self . index_array = np . arange ( self . n_train_samples ) <EOL> self . create_testing_summaries ( show_metric , self . metric_summ_name , <EOL> val_feed_dict ) <EOL> if self . shuffle : <EOL> np . random . shuffle ( self . index_array ) <EOL> self . set_batches ( make_batches ( self . n_train_samples , self . batch_size ) ) <EOL> def set_batches ( self , batches ) : <EOL> self . batches = batches <EOL> self . n_batches = len ( batches ) <EOL> self . batch_size = int ( batches [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] - batches [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) <EOL> self . data_size = self . batch_size * ( self . n_batches - <NUM_LIT:1> ) + int ( batches [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] - batches [ - <NUM_LIT:1> ] [ <NUM_LIT:0> ] ) <EOL> self . batch_start , self . batch_end = self . batches [ self . batch_index ] <EOL> def next_batch ( self ) : <EOL> """<STR_LIT>""" <EOL> self . batch_index += <NUM_LIT:1> <EOL> self . step = min ( self . batch_index * self . batch_size , self . data_size ) <EOL> if self . batch_index == self . n_batches : <EOL> self . batch_index = <NUM_LIT:0> <EOL> self . epoch += <NUM_LIT:1> <EOL> self . step = <NUM_LIT:0> <EOL> return False <EOL> self . batch_start , self . batch_end = self . batches [ self . batch_index ] <EOL> return True <EOL> def _train ( self , training_step , snapshot_epoch , snapshot_step , <EOL> show_metric ) : <EOL> """<STR_LIT>""" <EOL> tflearn . is_training ( True , self . session ) <EOL> self . loss_value , self . acc_value = None , None <EOL> self . val_loss , self . val_acc = None , None <EOL> train_summ_str , test_summ_str = None , None <EOL> snapshot = False <EOL> batch_ids = self . index_array [ self . batch_start : self . batch_end ] <EOL> feed_batch = { } <EOL> for key in self . feed_dict : <EOL> if np . ndim ( self . feed_dict [ key ] ) > <NUM_LIT:0> : <EOL> feed_batch [ key ] = slice_array ( self . feed_dict [ key ] , batch_ids ) <EOL> else : <EOL> feed_batch [ key ] = self . feed_dict [ key ] <EOL> tflearn . is_training ( True , self . session ) <EOL> self . session . run ( [ self . train ] , feed_batch ) <EOL> tflearn . is_training ( False , self . session ) <EOL> if self . summ_op is not None : <EOL> train_summ_str = self . session . run ( self . summ_op , feed_batch ) <EOL> sname = "<STR_LIT>" + self . scope_name <EOL> self . loss_value = summaries . get_value_from_summary_string ( <EOL> sname , train_summ_str ) <EOL> if show_metric and self . metric is not None : <EOL> sname = "<STR_LIT>" + self . metric_summ_name + "<STR_LIT:/>" + self . scope_name <EOL> self . acc_value = summaries . get_value_from_summary_string ( <EOL> sname , train_summ_str ) <EOL> if not self . next_batch ( ) : <EOL> if self . shuffle : <EOL> np . random . shuffle ( self . index_array ) <EOL> batches = make_batches ( self . n_train_samples , self . batch_size ) <EOL> self . set_batches ( batches ) <EOL> if snapshot_epoch : <EOL> snapshot = True <EOL> if snapshot_step : <EOL> if training_step % snapshot_step == <NUM_LIT:0> : <EOL> snapshot = True <EOL> if snapshot and self . val_feed_dict : <EOL> self . val_loss = evaluate ( self . session , self . loss , <EOL> self . val_feed_dict , <EOL> self . batch_size ) <EOL> if show_metric and self . metric is not None : <EOL> self . val_acc = evaluate ( self . session , self . metric , <EOL> self . val_feed_dict , <EOL> self . batch_size ) <EOL> if show_metric : <EOL> update_val_op = [ tf . assign ( self . val_loss_T , self . val_loss ) , <EOL> tf . assign ( self . val_acc_T , self . val_acc ) ] <EOL> else : <EOL> update_val_op = tf . assign ( self . val_loss_T , self . val_loss ) <EOL> self . session . run ( update_val_op ) <EOL> test_summ_str = self . session . run ( self . val_summary_op , <EOL> self . val_feed_dict ) <EOL> n_step = self . training_steps . eval ( session = self . session ) <EOL> if n_step > <NUM_LIT:1> : <EOL> if train_summ_str : <EOL> self . summary_writer . add_summary ( <EOL> train_summ_str , n_step ) <EOL> if test_summ_str : <EOL> self . summary_writer . add_summary ( <EOL> test_summ_str , n_step ) <EOL> return snapshot <EOL> def duplicate ( self ) : <EOL> """<STR_LIT>""" <EOL> return TrainOp ( self . loss , optimizer = self . optimizer , <EOL> batch_size = self . batch_size , ema = self . ema , <EOL> metric = self . metric , <EOL> trainable_vars = self . train_vars , <EOL> shuffle = self . shuffle ) <EOL> def create_summaries ( self , verbose = <NUM_LIT:2> ) : <EOL> """<STR_LIT>""" <EOL> summ_collection = self . name + "<STR_LIT>" <EOL> if verbose in [ <NUM_LIT:3> ] : <EOL> activations = tf . get_collection ( tf . GraphKeys . ACTIVATIONS ) <EOL> summarize_activations ( activations , summ_collection ) <EOL> if verbose in [ <NUM_LIT:2> , <NUM_LIT:3> ] : <EOL> summarize_variables ( self . train_vars , summ_collection ) <EOL> if verbose in [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] : <EOL> summarize_gradients ( self . grad , summ_collection ) <EOL> self . summ_op = tf . merge_summary ( tf . get_collection ( summ_collection ) ) <EOL> def create_testing_summaries ( self , show_metric = False , <EOL> metric_name = "<STR_LIT>" , validation_set = None ) : <EOL> """<STR_LIT>""" <EOL> tr_summ_collection = self . name + "<STR_LIT>" <EOL> te_summ_collection = self . name + "<STR_LIT>" <EOL> mn = metric_name . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if show_metric and self . metric is not None : <EOL> sname = "<STR_LIT>" + mn + "<STR_LIT:/>" + self . scope_name + "<STR_LIT>" <EOL> summarize ( self . metric , "<STR_LIT>" , sname , tr_summ_collection ) <EOL> sname = "<STR_LIT>" + mn + "<STR_LIT:/>" + self . scope_name <EOL> self . summ_op = summarize ( self . acc_averages . average ( self . metric ) , <EOL> "<STR_LIT>" , sname , tr_summ_collection ) <EOL> if validation_set is not None : <EOL> loss_val_name = "<STR_LIT>" + self . scope_name + "<STR_LIT>" <EOL> loss_val_name = check_scope_path ( loss_val_name ) <EOL> self . val_summary_op = summarize ( self . val_loss_T , "<STR_LIT>" , <EOL> loss_val_name , te_summ_collection ) <EOL> if show_metric and self . metric is not None : <EOL> acc_val_name = "<STR_LIT>" + mn + "<STR_LIT:/>" + self . scope_name + "<STR_LIT>" <EOL> acc_val_name = check_scope_path ( acc_val_name ) <EOL> self . val_summary_op = summarize ( self . val_acc_T , "<STR_LIT>" , <EOL> acc_val_name , <EOL> te_summ_collection ) <EOL> def duplicate_identical_ops ( ops ) : <EOL> """<STR_LIT>""" <EOL> for i in range ( len ( ops ) ) : <EOL> for j in range ( i + <NUM_LIT:1> , len ( ops ) ) : <EOL> if ops [ i ] == ops [ j ] : <EOL> ops [ j ] = ops [ i ] . duplicate ( ) <EOL> def validation_split ( val_feed_dicts , feed_dicts ) : <EOL> """<STR_LIT>""" <EOL> if val_feed_dicts : <EOL> for i , val_dict in enumerate ( val_feed_dicts ) : <EOL> for key , val in val_dict . items ( ) : <EOL> if isinstance ( val , float ) : <EOL> split = val <EOL> if type ( feed_dicts [ i ] [ key ] ) in [ list , np . ndarray ] : <EOL> split_at = int ( len ( feed_dicts [ i ] [ key ] ) * ( <NUM_LIT:1> - split ) ) <EOL> feed_dicts [ i ] [ key ] , val_feed_dicts [ i ] [ key ] = ( slice_array ( feed_dicts [ i ] [ key ] , <NUM_LIT:0> , split_at ) , <EOL> slice_array ( feed_dicts [ i ] [ key ] , split_at ) ) <EOL> else : <EOL> val_feed_dicts [ i ] [ key ] = feed_dicts [ i ] [ key ] <EOL> def evaluate ( session , op_to_evaluate , feed_dict , batch_size ) : <EOL> """<STR_LIT>""" <EOL> tflearn . is_training ( False , session ) <EOL> n_test_samples = len ( get_dict_first_element ( feed_dict ) ) <EOL> batches = make_batches ( n_test_samples , batch_size ) <EOL> index_array = np . arange ( n_test_samples ) <EOL> avg = <NUM_LIT:0.0> <EOL> for i , ( batch_start , batch_end ) in enumerate ( batches ) : <EOL> batch_ids = index_array [ batch_start : batch_end ] <EOL> feed_batch = { } <EOL> for key in feed_dict : <EOL> if np . ndim ( feed_dict [ key ] ) > <NUM_LIT:0> : <EOL> feed_batch [ key ] = slice_array ( feed_dict [ key ] , batch_ids ) <EOL> else : <EOL> feed_batch [ key ] = feed_dict [ key ] <EOL> avg += session . run ( op_to_evaluate , feed_batch ) / len ( batches ) <EOL> return avg </s>
