2016-11-22 47 views
2

我正在嘗試使用Tensorboard來顯示我的訓練過程。我的目的是,當每個時代完成時,我想使用整個驗證數據集來測試網絡的準確性,並將該準確性結果存儲到摘要文件中,以便我可以在Tensorboard中對其進行可視化。在Tensorflow中添加整個列車/測試數據集的精度摘要

我知道Tensorflow有summary_op這樣做,但它似乎只運行一個批次時運行代碼sess.run(summary_op)。我需要計算整個數據集的準確度。怎麼樣?

有沒有什麼例子可以做到這一點?

回答

4

定義tf.scalar_summary接受的佔位符:

accuracy_value_ = tf.placeholder(tf.float32, shape=()) 
accuracy_summary = tf.scalar_summary('accuracy', accuracy_value_) 

然後計算精度爲整個數據集(定義計算數據集中每一批的精度的程序,並提取的平均值),並將其保存變成一個python變量,我們稱之爲va

一旦你的va值,只需運行accuracy_summary運算,喂accuracy_value_佔位符:

sess.run(accuracy_summary, feed_dict={accuracy_value_: va}) 
1

我實施幼稚一層模型爲例來MNIST數據集進行分類並可視驗證精度Tensorboard,它適用於我。

import tensorflow as tf 
from tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_sets 
import os 

# number of epoch 
num_epoch = 1000 
model_dir = '/tmp/tf/onelayer_model/accu_info' 
# mnist dataset location, change if you need 
data_dir = '../data/mnist' 

# load MNIST dataset without one hot 
dataset = read_data_sets(data_dir, one_hot=False) 

# Create placeholder for input images X and labels y 
X = tf.placeholder(tf.float32, [None, 784]) 
# one_hot = False 
y = tf.placeholder(tf.int32) 

# One layer model graph 
W = tf.Variable(tf.truncated_normal([784, 10], stddev=0.1)) 
b = tf.Variable(tf.constant(0.1, shape=[10])) 
logits = tf.nn.relu(tf.matmul(X, W) + b) 

init = tf.initialize_all_variables() 

cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits, y) 
# loss function 
loss = tf.reduce_mean(cross_entropy) 
train_op = tf.train.GradientDescentOptimizer(0.01).minimize(loss) 

_, top_1_op = tf.nn.top_k(logits) 
top_1 = tf.reshape(top_1_op, shape=[-1]) 
correct_classification = tf.cast(tf.equal(top_1, y), tf.float32) 
# accuracy function 
acc = tf.reduce_mean(correct_classification) 

# define info that is used in SummaryWritter 
acc_summary = tf.scalar_summary('valid_accuracy', acc) 
valid_summary_op = tf.merge_summary([acc_summary]) 

with tf.Session() as sess: 
    # initialize all the variable 
    sess.run(init) 

    print("Writing Summaries to %s" % model_dir) 
    train_summary_writer = tf.train.SummaryWriter(model_dir, sess.graph) 

    # load validation dataset 
    valid_x = dataset.validation.images 
    valid_y = dataset.validation.labels 

    for epoch in xrange(num_epoch): 
     batch_x, batch_y = dataset.train.next_batch(100) 
     feed_dict = {X: batch_x, y: batch_y} 
     _, acc_value, loss_value = sess.run(
      [train_op, acc, loss], feed_dict=feed_dict) 
     vsummary = sess.run(valid_summary_op, 
          feed_dict={X: valid_x, 
             y: valid_y}) 

     # Write validation accuracy summary 
     train_summary_writer.add_summary(vsummary, epoch) 
+0

所以驗證數據集必須預先加載到內存中? –

+0

絕對是的,如果你想在epoch中評估驗證的準確性。 – daoliker

+0

是否可以使用隊列? –

0

如果您正在使用tf.metrics ops(使用內部計數器),則可以對您的驗證集使用批處理。下面是一個簡單的例子:

model = create_model() 
tf.summary.scalar('cost', model.cost_op) 
acc_value_op, acc_update_op = tf.metrics.accuracy(labels,predictions) 

summary_common = tf.summary.merge_all() 

summary_valid = tf.summary.merge([ 
    tf.summary.scalar('accuracy', acc_value_op), 
    # other metrics here... 
]) 

with tf.Session() as sess: 
    train_writer = tf.summary.FileWriter(logs_path + '/train', 
             sess.graph) 
    valid_writer = tf.summary.FileWriter(logs_path + '/valid') 

雖然培訓,只用你的火車作家寫的共同總結:

summary = sess.run(summary_common) 
train_writer.add_summary(summary, tf.train.global_step(sess, gstep_op)) 
train_writer.flush() 

每個驗證後,使用有效的作家寫的兩摘要:

gstep, summaryc, summaryv = sess.run([gstep_op, summary_common, summary_valid]) 
valid_writer.add_summary(summaryc, gstep) 
valid_writer.add_summary(summaryv, gstep) 
valid_writer.flush() 

當使用tf.metrics時,不要忘記重置內部cou在每個驗證步驟之前進行(局部變量)。