【问题标题】:Summary, tensorflow, which data to feed when evaluating?摘要,张量流,评估时要提供哪些数据?
【发布时间】:2018-09-01 23:05:49
【问题描述】:

Hands-on TensorBoard video by Dandelion Mané 中,他在谈到收集一些摘要并将其写入磁盘时编写了以下代码:

#(... some code and some summaries...)
merged_summary = tf.summary.merge_all()
writer = tf.summary.FileWriter("/tmp/mnist_demo/3")
writer.add_graph(sess.graph)

for i in range(2001):
  batch = mnist.train.next_batch(100)
  if i % 5 == 0:
    s = sess.run(merged_summary, feed_dict={x:batch[0], y: batch[1]})
    writer.add_summary(s, i)

所以我从那里获得了我的代码的灵感,下面我展示了一个 sn-p:

costs = []   # To keep track of the cost per epoch
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=Z5, labels=Y))
tf.summary.scalar('cost', cost)

for epoch in range(num_epochs):

        minibatches_cost = 0
        seed = seed + 1
        minibatches_train = random_mini_batches(X_train, Y_train, minibatch_size, seed)
        num_minibatches_train = len(minibatches_train)

        for minibatch in minibatches_train:

            # Select a minibatch
            (minibatch_X, minibatch_Y) = minibatch

            # Run the session to execute the optimizer and the cost, the feedict should contain a minibatch for (X,Y).
            _ , minibatch_cost = sess.run([optimizer, cost], feed_dict={X:minibatch_X, Y:minibatch_Y})

            minibatches_cost += minibatch_cost    # Adding the cost per minibatch

        epoch_cost = minibatches_cost / num_minibatches_train  # Cost per epoch

        if print_cost == True and epoch % 5 == 0:      # Print the cost
            print ("Cost after epoch %i: %f" % (epoch, epoch_cost))
            print ("Time elapsed: %i" % t_elapsed)

        if epoch % 1 == 0:                             # Append the cost
            costs.append(epoch_cost)

        if epoch % 1 == 0:                             # Write summaries
            summary_str = merged_summary.eval(feed_dict={X:minibatch_X, Y:minibatch_Y})
            file_writer.add_summary(summary_str, epoch)

我的问题是在评估 merge_summary 时是否向会话提供了正确的数据,因为我现在这样做的方式是,在摘要中写入磁盘的成本是一个 minibatch 的成本(实际上是最后一个 minibatch,使用 random_mini_batches 生成),而我保存在成本变量中然后绘制并研究其演变的每个 epoch 的成本(代码中的 epoch_cost)是每个 epoch 的平均成本(我假设比每小批量的成本更准确地衡量成本)。

我想提供整个训练数据不是解决方案,但我有点困惑为什么在评估摘要时只提供一批训练数据。

感谢您的帮助

【问题讨论】:

    标签: python tensorflow neural-network conv-neural-network tensorboard


    【解决方案1】:

    在 tensorflow 中,如果您想要整个数据集的平均成本,您需要使用指标,而不是摘要。有关如何使用它们的更多信息,请查看 tf 指标的文档。

    【讨论】:

    • 你能把答案扩大一点吗?现在我使用 tf.Summary.Value 来记录我在 epoch_cost 中计算的每个时期的数据。但是我的问题是,仅用一个 minibatch 评估摘要是否有意义?将其用作大致预测趋势的工具是否合适?
    • 啊,预测趋势是否有效很大程度上取决于您在数据集中查看的事物的方差。请注意,张量板可让您在摘要图中添加平滑,这将有很大帮助。
    猜你喜欢
    • 2019-08-02
    • 2016-04-06
    • 1970-01-01
    • 2016-10-28
    • 2017-08-21
    • 2018-06-20
    • 2018-07-26
    • 1970-01-01
    • 2017-04-09
    相关资源
    最近更新 更多