无法在tensorboard中使用summary.merge进行单独的培训和评估摘要

时间:2017-02-23 14:06:31

标签: python tensorflow tensorboard

我正在尝试使用张量板来观察卷积神经网络的学习。我正在使用tf.summary.merge_all函数来创建合并的摘要。但是,我希望跟踪培训和测试数据的准确性和损失。这篇文章很有用:Logging training and validation loss in tensorboard

为了使事情更容易处理,我想将我的摘要合并到两个合并的摘要中,一个用于培训,一个用于验证。(我将最终添加更多内容,如图像权重等)我试图按照描述来自tensorboard tf.summary.merge。我无法使它工作,我无法找到任何有用的例子来帮助我理解我的错误。

with tf.name_scope('accuracy'):
    correct_prediction = tf.equal(tf.argmax(y_logits, 1), tf.argmax(y, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))
    tf.summary.scalar('accuracy', accuracy)
    tf.summary.scalar('train_accuracy', accuracy)

with tf.name_scope('Cost'):
    cross_entropy = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(logits=y_logits, labels=y))
    opt = tf.train.AdamOptimizer()
    optimizer = opt.minimize(cross_entropy)
    grads = opt.compute_gradients(cross_entropy, [b_fc_loc2])
    tf.summary.scalar('cost', cross_entropy)
    tf.summary.scalar('train_cost', cross_entropy)


with tf.Session() as sess:
    writer = tf.summary.FileWriter('./logs/mnistlogs/1f', sess.graph)
    sess.run(tf.global_variables_initializer())
    merged = tf.summary.merge([cost, accuracy])

这会导致以下错误:

  

InvalidArgumentError(请参见上面的回溯):无法解析其中一个摘要输入            [[Node:Merge / MergeSummary = MergeSummary [N = 2,_device =“/ job:localhost / replica:0 / task:0 / cpu:0”](Merge / MergeSummary / inputs_0,Merge / MergeSummary / inputs_1)]]

我想知道为什么这不起作用,以及我如何找到解决方案,任何工作的例子都值得赞赏。

1 个答案:

答案 0 :(得分:14)

我明白了。我需要在合并之前给出摘要名称。下面的代码解决了这个问题:

with tf.name_scope('Cost'):
    cross_entropy = tf.reduce_mean(
            tf.nn.softmax_cross_entropy_with_logits(logits=y_logits, labels=y))
    opt = tf.train.AdamOptimizer(learning_rate=0.000003)
    optimizer = opt.minimize(cross_entropy)
    grads = opt.compute_gradients(cross_entropy, [b_fc_loc2])
    cost_sum = tf.summary.scalar('val_cost', cross_entropy)
    training_cost_sum = tf.summary.scalar('train_cost', cross_entropy)


with tf.name_scope('accuracy'):
    correct_prediction = tf.equal(tf.argmax(y_logits, 1), tf.argmax(y, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))
    train_accuracy = accuracy
    accuracy_sum = tf.summary.scalar('val_accuracy', accuracy)
    training_accuracy_sum = tf.summary.scalar('train_accuracy', accuracy)


with tf.Session() as sess:
    writer = tf.summary.FileWriter('./logs/{}/{}'.format(session_name, run_num), sess.graph)
    sess.run(tf.global_variables_initializer())
    train_merged = tf.summary.merge([training_accuracy_sum, training_cost_sum])