我在这里使用代码https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/3_NeuralNetworks/multilayer_perceptron.py来构建多层感知器,以解决MNIST问题。

在以下代码中,

with tf.Session() as sess:
    sess.run(init)

    # Training cycle
    for epoch in range(training_epochs):
        avg_cost = 0.
        total_batch = int(mnist.train.num_examples/batch_size)
        # Loop over all batches
        for i in range(total_batch):
            batch_x, batch_y = mnist.train.next_batch(batch_size)
            # Run optimization op (backprop) and cost op (to get loss value)
            _, c = sess.run([train_op, loss_op], feed_dict={X: batch_x,
                                                            Y: batch_y})
            # Compute average loss
            avg_cost += c / total_batch
        # Display logs per epoch step
        if epoch % display_step == 0:
            print("Epoch:", '%04d' % (epoch+1), "cost={:.9f}".format(avg_cost))
    print("Optimization Finished!")


我想仍然使用sess.run记录每次迭代的准确性;我怎样才能做到这一点?

最佳答案

您是否有代码来衡量训练的准确性?您还需要运行该块。将其粘贴在Loop over all batches块的底部,以便它在每次迭代时运行。

如果要损失而不是准确性,则只需在该位置打印avg_cost。如果要打印每个时期(而不是每个迭代)的损失,则删除模有条件的if epoch % display_step == 0:,然后取消缩进print

其中之一满足您的需求吗?

关于machine-learning - 如何在Tensorflow中的sess.run中记录每次迭代的准确性?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47425102/

10-12 21:01