我正在学习Tensorboard,我正在遵循this tutorial.中的代码
下面是我的代码:
import tensorflow as tf
LOGDIR = "/tmp/mnist_tutorial/"
mnist = tf.contrib.learn.datasets.mnist.read_data_sets(train_dir=LOGDIR + "data", one_hot=True)
def conv_layer(input, size_in, size_out, name="conv"):
with tf.name_scope(name):
w = tf.Variable(tf.zeros([5, 5, size_in, size_out]))
b = tf.Variable(tf.zeros([size_out]))
conv = tf.nn.conv2d(input, w, strides=[1, 1, 1, 1], padding="SAME")
act = tf.nn.relu(conv + b)
tf.summary.histogram("weights", w)
tf.summary.histogram("biases", b)
tf.summary.histogram("activations", act)
return act
def fc_layer(input, size_in, size_out, name="fc"):
with tf.name_scope(name):
w = tf.Variable(tf.zeros([size_in, size_out]))
b = tf.Variable(tf.zeros([size_out]))
act = tf.nn.relu(tf.matmul(input, w)+b)
tf.summary.histogram("weights", w)
tf.summary.histogram("biases", b)
tf.summary.histogram("activations", act)
return act
x = tf.placeholder(tf.float32, shape=[None, 784], name='x')
x_image = tf.reshape(x, [-1, 28, 28, 1])
tf.summary.image('input', x_image, 3)
y = tf.placeholder(tf.float32, shape=[None, 10], name='labels')
conv1 = conv_layer(x_image, 1, 32, name='conv1')
pool1 = tf.nn.max_pool(conv1, ksize=[1,2,2,1], strides=[1,2,2,1], padding="SAME")
conv2 = conv_layer(pool1, 32, 64, name='conv2')
pool2 = tf.nn.max_pool(conv2, ksize=[1,2,2,1], strides=[1,2,2,1], padding="SAME")
flattened = tf.reshape(pool2, [-1, 7*7*64])
fc1 = fc_layer(flattened, 7*7*64, 1024, name='fc1')
logits = fc_layer(fc1, 1024, 10, name='fc2')
with tf.name_scope('xent'):
xent = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits, labels=y))
tf.summary.scalar('cross_entropy', xent)
with tf.name_scope('train'):
train_step = tf.train.AdamOptimizer(1e-4).minimize(xent)
with tf.name_scope('accruacy'):
correct_prediction = tf.equal(tf.argmax(logits,1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
tf.summary.scalar('accruacy', accuracy)
summ = tf.summary.merge_all()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
# writer =tf.summary.FileWriter("tmp/mnist_demo/1")
# writer.add_graph(sess.graph)
# writer.close()
for i in range(20):
batch = mnist.train.next_batch(100)
# Occasionally report back the accruacy
if i%2 == 0:
[train_accruacy] = sess.run([accuracy], feed_dict={x:batch[0], y:batch[1]}) # works
# [s, train_accruacy] = sess.run([summ, accuracy], feed_dict={x:batch[0], y:batch[1]}) #error!
print("step %d, training accruacy %g" % (i, train_accruacy))
sess.run(train_step, feed_dict={x:batch[0],y:batch[1]})
我在使用这行时遇到了一个错误:
[s, train_accruacy] = sess.run([summ, accuracy], feed_dict={x:batch[0], y:batch[1]}) #error!
这是我收到的错误消息:
You must feed a value for placeholder tensor 'x' with dtype float and shape [?,784]
[[{{node x}} = Placeholder[dtype=DT_FLOAT, shape=[?,784], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
我知道我输入的张量不是(x,784)的正确形状
然而,我不明白为什么[train_accruacy] = sess.run([accuracy], feed_dict={x:batch[0], y:batch[1]}) # works
。毕竟,我把相同的东西输入到相同的占位符变量中,它们接受相同形状的张量
除非我完全错了,sess.run([argument], feed_dict=...)
的第一个参数描述了要返回的张量。我看不出这会如何影响我输入的数据的形状
另外:这个模型应该有一个错误
对于感兴趣的人,完整的代码是here
是否返回的数据类型也不同tf.summary.merge_all()
返回一个字符串张量,但我怀疑这是导致问题的原因
我似乎在网上找不到有关这个问题的任何文件。这应该发生吗
我会回答我自己的问题:
tf.reset_default_graph()
工作,添加在def conv_layer()
之前如果不想使用
tf.reset_default_graph()
:结果是我在同一个环节中输入了2个张量,这是张量流不允许的
上面的代码不起作用。结果是,由于某种原因,
i%2
下面的第一行代码输入张量batch[0]
很好,但是,当它注释掉那行并用第二行替换它时,tensorflow似乎没有“清除”出x
占位符变量,因此2个独立的张量被输入到输入(来自独立的sess.run()
)事件中此代码适用于:
这里张量是分开输入的,一切正常
我很高兴如果有人能告诉我为什么会这样,或者这是一个错误
相关问题 更多 >
编程相关推荐