def calculate_loss(logits, labels): """ The total loss is defined as the cross entropy loss plus all of the weight decay terms (L2 loss). Args: logits: Logits from inference(), 2D tensor of [None, NUM_CLASSES]. labels: 2D tensor of [None, NUM_CLASSES]. Returns: Loss: 0D tensor of type float. """ myTF.calculate_cross_entropy_loss(logits, labels) return tf.add_n(tf.get_collection('losses'), name='loss_total')
def evaluate(evalDataSet, ckpt_value, eval_value, time_list): with tf.Graph().as_default() as g, tf.device('/gpu:0'): # Placeholders for input, output and dropout feature_size = FLAGS.num_filters * len(FLAGS.filter_sizes.split(',')) # Placeholders for input, output and dropout input_x = tf.placeholder(tf.int32, [None, OPTION.SEQUENCE_LEN], name="input_x") input_y = tf.placeholder(tf.int32, [None, OPTION.NUM_CLASSES], name="input_y") feature_size = feature_size * min(time_list[-1] - 0, OPTION.DP_DEPTH) if feature_size > 0: features_before = tf.placeholder(tf.float32, [None, feature_size], name="features_before") else: features_before = None textcnn = TextCNNpn_model.Model( sequence_length=OPTION.SEQUENCE_LEN, num_classes=OPTION.NUM_CLASSES, vocab_size=None, embedding_size=OPTION.EMEBEDDING_DIMENSION, filter_sizes=list(map(int, FLAGS.filter_sizes.split(","))), num_filters=FLAGS.num_filters, Word2vec=True, Trainable=False) # inference model. logits, _ = textcnn.inference(input_x, features_before, OPTION.EVAL_BATCH_SIZE, eval_data=True) # Calculate loss. loss = myTF.calculate_cross_entropy_loss(logits, input_y) logits = tf.nn.softmax(logits) # Restore the moving average version of the learned variables for eval. # ????????????????????????? variable_averages = tf.train.ExponentialMovingAverage( OPTION.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) # Build the summary operation based on the TF collection of Summaries. summary_op = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(OPTION.EVAL_DIR, g) last_eval_ckpt = ckpt_value best_eval_value = eval_value config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False) # config.gpu_options.per_process_gpu_memory_fraction = 0.5 # 程序最多只能占用指定gpu50%的显存 config.gpu_options.allow_growth = True # 程序按需申请内存 while True: # Start running operations on the Graph. allow_soft_placement must be set to # True to build towers on GPU, as some of the ops do not have GPU implementations. with tf.Session(config=config) as sess: ckpt = tf.train.get_checkpoint_state(OPTION.CHECKPOINT_DIR) if ckpt and ckpt.model_checkpoint_path: # extract global_step global_step_for_restore = int( ckpt.model_checkpoint_path.split('/')[-1].split('-') [-1]) if global_step_for_restore > last_eval_ckpt: # Restores from checkpoint saver.restore(sess, ckpt.model_checkpoint_path) else: if tf.gfile.Exists("TRAIN_SUCCEED"): print("Train terminated, eval terminating...") return else: print('No checkpoint file found') time.sleep(FLAGS.eval_interval_secs) continue if global_step_for_restore > last_eval_ckpt: max_steps_per_epoch = int( math.ceil(evalDataSet.get_dataset_size() / float(OPTION.EVAL_BATCH_SIZE))) start_time = time.time() total_predicted_value = [] total_true_value = [] total_loss = [] for step in range(max_steps_per_epoch): test_data, test_label, test_features = evalDataSet.next_batch( OPTION.EVAL_BATCH_SIZE) if feature_size > 0: feed_dict = { input_x: test_data, input_y: test_label, features_before: test_features } else: feed_dict = { input_x: test_data, input_y: test_label } predicted_value, true_value, loss_value = sess.run( [logits, input_y, loss], feed_dict=feed_dict) total_predicted_value.append(predicted_value) total_true_value.append(true_value) total_loss.append(loss_value) duration = time.time() - start_time # test_data, test_label = evalDataSet.next_batch(OPTION.EVAL_BATCH_SIZE) summary = tf.Summary() # summary.ParseFromString(sess.run(summary_op, feed_dict={input_x: test_data, input_y: test_label})) total_predicted_value = np.concatenate( total_predicted_value, axis=0) total_true_value = np.concatenate(total_true_value, axis=0) total_loss = np.concatenate(total_loss, axis=0) total_predicted_value = total_predicted_value[ 0:evalDataSet.get_dataset_size()] total_true_value = total_true_value[0:evalDataSet. get_dataset_size()] total_loss = total_loss[0:evalDataSet.get_dataset_size()] assert evalDataSet.get_dataset_size( ) == total_predicted_value.shape[0], 'sample_count error!' best_eval_value = evaluation_result( OPTION.EVAL_DIR, total_predicted_value, total_true_value, total_loss, global_step_for_restore, best_eval_value, summary) summary_writer.add_summary(summary, global_step_for_restore) last_eval_ckpt = global_step_for_restore if FLAGS.run_once: break time.sleep(FLAGS.eval_interval_secs)
def evaluate(trainDataSet,time,model_time): with tf.Graph().as_default() as g, tf.device('/cpu:0'): # Placeholders for input, output and dropout input_x = tf.placeholder(tf.int32, [None, OPTION.SEQUENCE_LEN, OPTION.SENT_LEN], name="input_x") input_y = tf.placeholder(tf.int32, [None, OPTION.NUM_CLASSES], name="input_y") han = MODEL.Model(sequence_length = OPTION.SEQUENCE_LEN, sent_length = OPTION.SENT_LEN, num_classes=OPTION.NUM_CLASSES, vocab_size=None, embedding_size=OPTION.EMEBEDDING_DIMENSION, Word2vec=True, Trainable=False) # inference model. logits, _ = han.inference(input_x, eval_data=True) # Calculate loss. loss = myTF.calculate_cross_entropy_loss(logits, input_y) # get model paramaters # paramaters_list_reshape = han.get_paramaters_list_reshape() # Restore the moving average version of the learned variables for eval. # ????????????????????????? variable_averages = tf.train.ExponentialMovingAverage(OPTION.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) # Build the summary operation based on the TF collection of Summaries. summary_op = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(OPTION.EVAL_DIR, g) # Start running operations on the Graph. allow_soft_placement must be set to # True to build towers on GPU, as some of the ops do not have GPU implementations. with tf.Session(config=tf.ConfigProto( allow_soft_placement=FLAGS.allow_soft_placement, log_device_placement=FLAGS.log_device_placement)) as sess: if os.path.exists(os.path.join(OPTION.EVAL_DIR, 'model.ckpt-best.index')): # new_saver = tf.train.import_meta_graph( # os.path.join(OPTION.TRAIN_DIR, 'model.ckpt-' + checkpoint + '.meta')) saver.restore(sess, os.path.join(OPTION.EVAL_DIR, 'model.ckpt-best')) else: print('No checkpoint file found') return max_steps_per_epoch = int(math.ceil( trainDataSet.get_dataset_size() / float(OPTION.EVAL_BATCH_SIZE))) total_predicted_value = [] for step in range(max_steps_per_epoch): train_data, train_label = trainDataSet.next_batch(OPTION.EVAL_BATCH_SIZE) predicted_value = sess.run(loss, feed_dict={input_x: train_data, input_y: train_label}) total_predicted_value.append(predicted_value) # test_data, test_label = evalDataSet.next_batch(OPTION.EVAL_BATCH_SIZE) summary = tf.Summary() # summary.ParseFromString(sess.run(summary_op, feed_dict={input_x: test_data, input_y: test_label})) total_predicted_value = np.concatenate(total_predicted_value,axis=0) assert trainDataSet.get_dataset_size() == total_predicted_value.shape[0], 'sample_count error!' detail_filename = os.path.join(OPTION.MODELPARA_DIR, 'loss_%d_%d' %(time,model_time)) if os.path.exists(detail_filename): os.remove(detail_filename) np.savetxt(detail_filename, total_predicted_value, fmt='%f')