def evaluate(): with tf.Graph().as_default() as g: x = tf.placeholder(tf.float32, [None, character_inference.INPUT_NODE], name='x-input') y_ = tf.placeholder(tf.float32, name='y-input') y = character_inference.inference(x, None) mse = tf.reduce_mean(tf.square(y_ - y)) variable_averages = tf.train.ExponentialMovingAverage( MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) while True: with tf.Session() as sess: ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH) validate_feed = {x: text_list_side, y_: text_list_tag[:, 1]} if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) global_step = ckpt.model_checkpoint_path.split( '/')[-1].split('-')[-1] # eval_aws = sess.run(y, feed_dict=validate_feed) loss = sess.run(mse, feed_dict=validate_feed) print("After %s training step(s) loss %s" % (global_step, loss)) print("==========================================") else: print('No checkpoint file found') return time.sleep(EVAL_INTERVAL_SECS)
def train(): x = tf.placeholder(tf.float32, [None, character_inference.INPUT_NODE], name='x-input') y_ = tf.placeholder(tf.float32, name='y-input') regularizer = tf.contrib.layers.l2_regularizer(REGULARIZATION_RATE) y = character_inference.inference(x, regularizer) global_step = tf.Variable(0, trainable=False) variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step) variables_averages_op = variable_averages.apply(tf.trainable_variables()) mse = tf.reduce_mean(tf.square(y_ - y)) loss = mse + tf.add_n(tf.get_collection('losses')) learning_rate = tf.train.exponential_decay( LEARNING_RATE_BASE, global_step, DATASET_SIZE / BATCH_SIZE, LEARNING_RATE_DECAY, staircase=True) train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) with tf.control_dependencies([train_step, variables_averages_op]): train_op = tf.no_op(name='train') saver = tf.train.Saver() with tf.Session() as sess: tf.initialize_all_variables().run() for i in range(TRAINING_STEPS): _, loss_value, step = sess.run([train_op, loss, global_step], feed_dict={x: train_list_side, y_: train_list_tag[:, 1]}) if i % 1000 == 0: print("After %d training step(s), loss on training batch is %g." % (step, loss_value)) saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME), global_step=global_step)
def evaluate(character): with tf.Graph().as_default() as g: x = tf.placeholder(tf.float32, [None, character_inference.INPUT_NODE], name='x-input') y_ = tf.placeholder(tf.float32, name='y-input') y = character_inference.inference(x, None) # 训练时损失函数 cross_entropy = tf.nn.sigmoid_cross_entropy_with_logits(logits=y, targets=y_) cross_entropy_mean = tf.reduce_mean(cross_entropy) loss = cross_entropy_mean variable_averages = tf.train.ExponentialMovingAverage( MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) dict_acc = {} dict_precision = {} dict_recall = {} dict_f1 = {} dict_acc_lsit = {} while True: with tf.Session() as sess: validate_feed = {x: text_list_side, y_: text_list_tag} # tf.train.get_checkpoint_state 会根据checkpoint文件自动找到目录中最新模型的文件名 ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) global_step = ckpt.model_checkpoint_path.split( '/')[-1].split('-')[-1] # accuracy_score = sess.run(accuracy, feed_dict=validate_feed) # accuracy_score = get_acc(sess,true_y, pred_y) # print("After %s training step(s), validation accuracy = %g" % (global_step, accuracy_score)) # print("the input data are \n%s" % test_list_side) # print("the truly answer are \n%s" % test_list_tag) eval_aws = sess.run(y, feed_dict=validate_feed) eval_loss = sess.run(loss, feed_dict=validate_feed) print("========the evaluate eval_loss are %s" % eval_loss) # print("the evaluate answer are \n%s" % eval_aws) accuracy_score, acc_list = get_acc(sess, text_list_tag, eval_aws) print( "After %s training step(s), all validation accuracy = %g" % (global_step, accuracy_score)) print( "After %s training step(s), 5 validation accuracy = %s" % (global_step, acc_list)) precision_list = get_precision(text_list_tag, eval_aws) print("After %s training step(s), 5 precision = %s" % (global_step, precision_list)) recall_list = get_recall(text_list_tag, eval_aws) print("After %s training step(s), 5 recall = %s" % (global_step, recall_list)) f1_list = get_f1(precision_list, recall_list) print("After %s training step(s), 5 f1 = %s" % (global_step, f1_list)) print("==========================================") if int(global_step) > 1: dict_acc[global_step] = accuracy_score dict_precision[global_step] = precision_list dict_recall[global_step] = recall_list dict_f1[global_step] = f1_list dict_acc_lsit[global_step] = acc_list if int(global_step) == 29001: # print("================全部准确率===================") # sort_dict(dict_acc) print("================5个准确率===================") sort_dict(dict_acc_lsit) print("================5个精准率===================") sort_dict(dict_precision) print("================5个召回率===================") sort_dict(dict_recall) print("================5个f1===================") sort_dict(dict_f1) break else: print('No checkpoint file found') return time.sleep(EVAL_INTERVAL_SECS)
def train(): # 定义输入输出placeholder。 x = tf.placeholder(tf.float32, [None, character_inference.INPUT_NODE], name='x-input') y_ = tf.placeholder(tf.float32, name='y-input') # L2正则化 regularizer = tf.contrib.layers.l2_regularizer(REGULARIZATION_RATE) # 计算在当前参数下神经网络前向传播的结果 y = character_inference.inference(x, regularizer) # y = character_inference.inference_nlayer(x,regularizer) # 定义存储训练轮数的便利那个。这个变量不需要计算滑动平均值,所以这里指定这个变量为不可训练的变量 global_step = tf.Variable(0, trainable=False) # ///////////////====定义损失函数、学习率、滑动平均操作以及训练过程。=====////////////// # 初始化滑动平均类 variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step) # 在所有代表神经网络参数的变量上使用滑动平均 variables_averages_op = variable_averages.apply(tf.trainable_variables()) # 计算交叉熵作为刻画预测值和真实值之间茶军的损失函数 """ // 参考损失函数的计算 http://blog.csdn.net/u013250416/article/details/78230464 sigmoid_cross_entropy_with_logits 应用于多标签或者二分类 """ # 多目标损失函数 cross_entropy = tf.nn.sigmoid_cross_entropy_with_logits(logits=y, targets=y_) cross_entropy_mean = tf.reduce_mean(cross_entropy) # 总损失等于交叉熵损失和正则化损失的和 loss = cross_entropy_mean + tf.add_n(tf.get_collection('losses')) # 指数衰减设置学习率 learning_rate = tf.train.exponential_decay( LEARNING_RATE_BASE, global_step, DATASET_SIZE / BATCH_SIZE, LEARNING_RATE_DECAY, staircase=True) # 优化损失函数,在minimize中传入global_step将自动更新global_step,从而更新学习率 train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) # 在训练神经网络模型时既需要通过反向传播来更新神经网络的参数,又要更新每一个参数的滑动平均值。 with tf.control_dependencies([train_step, variables_averages_op]): train_op = tf.no_op(name='train') # 初始化TensorFlow持久化类。 saver = tf.train.Saver() with tf.Session() as sess: tf.initialize_all_variables().run() for i in range(TRAINING_STEPS): # # 每次选取batch_size样本进行训练 # start = (i * BATCH_SIZE) % DATASET_SIZE # end = min(start + BATCH_SIZE, DATASET_SIZE) # _, loss_value, step = sess.run([train_op, loss, global_step], # feed_dict={x: train_list_side[start:end], # y_: train_list_tag[start:end]}) # 每次选取all_size样本进行训练 _, loss_value, step = sess.run([train_op, loss, global_step], feed_dict={x: train_list_side, y_: train_list_tag}) if i % 1000 == 0: print("After %d training step(s), loss on training batch is %g." % (step, loss_value)) saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME), global_step=global_step)