本文介绍了无法在张量板中使用 summary.merge 进行单独的培训和评估摘要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 tensorboard 来观看卷积神经网络的学习.我很好地使用 tf.summary.merge_all 函数来创建合并的摘要.但是,我想跟踪训练和测试数据的准确性和损失.这篇文章很有用:在张量板中记录训练和验证损失.

I am trying to use tensorboard to watch the learning of a convolutional neural net. I am doing good with the tf.summary.merge_all function to create a merged summary. However, I would like to have tracking on accuracy and loss both for training and test data. This post is useful:Logging training and validation loss in tensorboard.

为了使事情更容易处理,我想将我的摘要合并为两个合并的摘要,一个用于训练,一个用于验证.(我最终会添加更多内容,例如图像权重等)我尝试按照说明进行操作来自张量板 tf.summary.merge.我无法让它工作,我无法找到任何有效的例子来帮助我理解我哪里出错了.

To make things easier to handle, I would like to merge my summaries into two merged summaries, one for training and one for validation.(I will add more stuff eventually, like images weights etc.) I tried to follow the description from tensorboard tf.summary.merge. I can't make it work and I am unable to find any working examples to help me understand where I am going wrong.

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(回溯见上文):无法解析摘要输入之一[[节点:Merge/MergeSummary = MergeSummary[N=2, _device="/job:localhost/replica:0/task:0/cpu:0"](Merge/MergeSummary/inputs_0, Merge/MergeSummary/inputs_1)]]

我想知道为什么这不起作用,以及我如何找到解决方案,感谢任何有效的示例.

I would like to know why this doesn't work, and how I can find a solution, any working examples are appreciated.

推荐答案

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

I figured it out. I need to give the summaries names before merging. The code below solves the problem:

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])

这篇关于无法在张量板中使用 summary.merge 进行单独的培训和评估摘要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 21:27