2017-03-10 88 views
0

首先,我在Python和Tensorflow中都很新。 我试图链接的演示:https://www.tensorflow.org/get_started/mnist/beginners 它运行良好。 但是,我想调试(或记录)一些占位符的值,这些变量在运行Session.run()时发生了变化。我Tensorflow:在会话运行时输出值

请你告诉我的方式来“调试”或登录时,会话运行在循环? 这是我的代码

import tensorflow as tf 
 
from tensorflow.examples.tutorials.mnist import input_data 
 

 
mnist = input_data.read_data_sets("mnist/", one_hot=True) 
 

 
x = tf.placeholder(tf.float32, [None, 784]) 
 

 

 
W = tf.Variable(tf.zeros([784,10])) 
 
b = tf.Variable(tf.zeros([10])) 
 

 
y = tf.nn.softmax(tf.matmul(x, W) + b) 
 

 
y1 = tf.add(tf.matmul(x,W),b) 
 
y_ = tf.placeholder(tf.float32, [None, 10]) 
 

 
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1])) 
 

 
cross_entropy1 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y1, y_)) 
 

 
train_step = tf.train.GradientDescentOptimizer(0.05).minimize(cross_entropy1) 
 

 
sess = tf.InteractiveSession() 
 
tf.global_variables_initializer().run() 
 

 
for _ in range(1000): 
 
    batch_xs, batch_ys = mnist.train.next_batch(100) 
 
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys}) 
 

 
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1)) 
 
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 
 

 
sess.run(tf.argmax(y,1), feed_dict={x: mnist.test.images, y_: mnist.test.labels})

在该脚本中,我想记录y和tf.argmax的数值(y,1),用于处理每一个测试图像。

+1

你见过这个[幻灯片](https://wookayin.github.io/tensorflow-talk-debugging/#1)吗? – xxi

+0

谢谢@xxi,这是一个有趣的幻灯片。我现在会尝试。 – nguyenhoai890

回答

3

Mrry最好的回答了这个计算器的答案:https://stackoverflow.com/a/33633839/6487788

正是你问(sess.run在打印过程中)会是他的回答,这部分:

  • 要打印值没有将它返回给你的Python程序,你可以使用tf.Print()op,并在另一个答案中提出。请注意,您仍然需要运行部分图表来查看此op的输出,该输出将打印到标准输出。如果您正在运行分布式TensorFlow,tf.Print()op会将其输出打印到运行该任务的标准输出。

这将是此代码为argmax:

argmaxy = tf.Print(tf.argmax(y,1)) 
correct_prediction = tf.equal(argmaxy, tf.argmax(y_,1)) 

祝你好运!

+0

我使用tf.Print(y)是不正确的,但更改为tf.Print(y,[y])是可以的。但是,谢谢你,因为你指出了代码:argmaxy = tf.Print(tf.argmax(y,1))。其中向我展示了如何在Session.Process中打印。 – nguyenhoai890

2

虽然@ rmeerten的回答是正确的,但您也可以考虑使用TensorBoard这可以成为调试模型和查看正在发生的事情的有用工具。作为背景,您还可以查看TensorFlow Dev Summit上的TensorBoard session

+0

是的。好评! – rmeertens

相关问题