我正在使用tensorflow进行DNN,并希望用tensorboard将其可视化。它是一个三层隐藏层网络。这是代码 -
def dnn_model(data, keep_prob, n_classes=10, n_h1 =100, n_h2=100, n_h3=100 ):
with tf.name_scope('hidden1'):
weights = tf.Variable(tf.random_normal([784, n_h1]), name='weights')
biases = tf.Variable(tf.random_normal([n_h1]), name = 'biases')
l1 = tf.add(tf.matmul(data, weights), biases)
l1 = tf.nn.relu(l1)
l1 = tf.nn.dropout(l1, keep_prob)
tf.histogram_summary('l1_weights', weights)
tf.histogram_summary('l1_biases', biases)
with tf.name_scope('hidden2'):
weights = tf.Variable(tf.random_normal([n_h1, n_h2]), name='weights')
biases = tf.Variable(tf.random_normal([n_h2]), name = 'biases')
l2 = tf.add(tf.matmul(l1, weights), biases)
l2 = tf.nn.relu(l2)
l2 = tf.nn.dropout(l2, keep_prob)
tf.histogram_summary('l2_weights', weights)
tf.histogram_summary('l2_biases', biases)
with tf.name_scope('hidden3'):
weights = tf.Variable(tf.random_normal([n_h2, n_h3]), name='weights')
biases = tf.Variable(tf.random_normal([n_h3]), name = 'biases')
l3 = tf.add(tf.matmul(l2, weights), biases)
l3 = tf.nn.relu(l3)
l3 = tf.nn.dropout(l3, keep_prob)
tf.histogram_summary('l3_weights', weights)
tf.histogram_summary('l3_biases', biases)
with tf.name_scope('output'):
weights = tf.Variable(tf.random_normal([n_h3, n_classes]), name='weights')
biases = tf.Variable(tf.random_normal([n_classes]), name='biases')
outputs = tf.matmul(l3, weights)+ biases
tf.histogram_summary('outputs', outputs)
print ('DNN architecture:')
print ('hidden layer one: %d \nhidden layer two: %d \nhidden layer three: %d'%(n_h1, n_h2, n_h3))
print ('output layer:', n_classes )
return (outputs)
def dnn_train(trX, trY, tsX, tsY, n_epochs=10, batch_size=128, keep_rate=1):
with tf.name_scope('input'):
x = tf.placeholder('float', [None, 784])
y = tf.placeholder ('float', [None, 10])
keep_prob = tf.placeholder(tf.float32)
prediction = dnn_model(x, keep_prob=keep_prob)
with tf.name_scope('cost'):
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(prediction,y))
tf.scalar_summary ('cost', cost)
with tf.name_scope('train'):
optimizer = tf.train.AdamOptimizer().minimize(cost)
n_epochs = n_epochs
with tf.Session() as sess:
merged = tf.merge_all_summaries()
writer = tf.train.SummaryWriter("logs/", sess.graph)
sess.run(tf.initialize_all_variables())
for epoch in range(n_epochs):
epoch_loss = 0
for start, end in zip(range(0, len(trX), batch_size), range(batch_size, len(trX)+1, batch_size)):
_, c = sess.run([optimizer, cost], feed_dict={x: trX[start:end], y: trY[start:end], keep_prob: keep_rate})
epoch_loss += c
if epoch % 2 ==0:
print('Epoch', epoch, 'completed out of ', n_epochs, 'loss', epoch_loss)
result = sess.run(merged, feed_dict={x: trX[start:end], y: trY[start:end], keep_prob: keep_rate})
writer.add_summary (result, epoch)
correct = tf.equal(tf.argmax(prediction,1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
print ('dropout keep_rate:', keep_rate)
print ('Accuracy:', accuracy.eval({x: tsX, y: tsY, keep_prob: keep_rate}))
## load data and train model
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
X_train, y_train = mnist.train.images, mnist.train.labels
X_test, y_test = mnist.test.images, mnist.test.labels
dnn_train(X_train, y_train, X_test, y_test, n_epochs = 10, keep_rate=0.99)
代码给了我一个非常令人困惑的错误消息。 但是,当我注释掉这两行(第5行和第6行到最后一行)时,程序可以工作。只是张量板只显示图表,没有事件和摘要。
result = sess.run(merged, feed_dict={x: trX[start:end], y: trY[start:end], keep_prob: keep_rate})
writer.add_summary (result, epoch)
我花了很长时间试图解决这个问题。我做错了什么? 非常感谢帮助!非常感谢。
以下是我收到的错误消息。
DNN architecture:
hidden layer one: 100
hidden layer two: 100
hidden layer three: 100
output layer: 10
Epoch 0 completed out of 10 loss 73920328.1035
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
/Users/tiger/anaconda/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
971 try:
--> 972 return fn(*args)
973 except errors.OpError as e:
/Users/tiger/anaconda/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/client/session.py in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata)
953 feed_dict, fetch_list, target_list,
--> 954 status, run_metadata)
955
/Users/tiger/anaconda/envs/tensorflow/lib/python3.5/contextlib.py in __exit__(self, type, value, traceback)
65 try:
---> 66 next(self.gen)
67 except StopIteration:
/Users/tiger/anaconda/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/framework/errors.py in raise_exception_on_not_ok_status()
462 compat.as_text(pywrap_tensorflow.TF_Message(status)),
--> 463 pywrap_tensorflow.TF_GetCode(status))
464 finally:
InvalidArgumentError: You must feed a value for placeholder tensor 'input_2/Placeholder' with dtype float
[[Node: input_2/Placeholder = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
During handling of the above exception, another exception occurred:
InvalidArgumentError Traceback (most recent call last)
<ipython-input-49-71e47968c9c9> in <module>()
----> 1 dnn_train(X_train, y_train, X_test, y_test, n_epochs = 10, keep_rate=0.99)
<ipython-input-48-c1c50872bea4> in dnn_train(trX, trY, tsX, tsY, n_epochs, batch_size, keep_rate)
33 if epoch % 2 ==0:
34 print('Epoch', epoch, 'completed out of ', n_epochs, 'loss', epoch_loss)
---> 35 result = sess.run(merged, feed_dict={x: trX[start:end], y: trY[start:end], keep_prob: keep_rate})
36 writer.add_summary (result, epoch)
37
/Users/tiger/anaconda/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
715 try:
716 result = self._run(None, fetches, feed_dict, options_ptr,
--> 717 run_metadata_ptr)
718 if run_metadata:
719 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
/Users/tiger/anaconda/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
913 if final_fetches or final_targets:
914 results = self._do_run(handle, final_targets, final_fetches,
--> 915 feed_dict_string, options, run_metadata)
916 else:
917 results = []
/Users/tiger/anaconda/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
963 if handle is None:
964 return self._do_call(_run_fn, self._session, feed_dict, fetch_list,
--> 965 target_list, options, run_metadata)
966 else:
967 return self._do_call(_prun_fn, self._session, handle, feed_dict,
/Users/tiger/anaconda/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
983 except KeyError:
984 pass
--> 985 raise type(e)(node_def, op, message)
986
987 def _extend_graph(self):
InvalidArgumentError: You must feed a value for placeholder tensor 'input_2/Placeholder' with dtype float
[[Node: input_2/Placeholder = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
Caused by op 'input_2/Placeholder', defined at:
File "/Users/tiger/anaconda/envs/tensorflow/lib/python3.5/runpy.py", line 184, in _run_module_as_main
"__main__", mod_spec)
File "/Users/tiger/anaconda/envs/tensorflow/lib/python3.5/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/Users/tiger/anaconda/envs/tensorflow/lib/python3.5/site-packages/ipykernel/__main__.py", line 3, in <module>
app.launch_new_instance()
File "/Users/tiger/anaconda/envs/tensorflow/lib/python3.5/site-packages/traitlets/config/application.py", line 653, in launch_instance
app.start()
File "/Users/tiger/anaconda/envs/tensorflow/lib/python3.5/site-packages/ipykernel/kernelapp.py", line 474, in start
ioloop.IOLoop.instance().start()
File "/Users/tiger/anaconda/envs/tensorflow/lib/python3.5/site-packages/zmq/eventloop/ioloop.py", line 162, in start
super(ZMQIOLoop, self).start()
File "/Users/tiger/anaconda/envs/tensorflow/lib/python3.5/site-packages/tornado/ioloop.py", line 887, in start
handler_func(fd_obj, events)
File "/Users/tiger/anaconda/envs/tensorflow/lib/python3.5/site-packages/tornado/stack_context.py", line 275, in null_wrapper
return fn(*args, **kwargs)
File "/Users/tiger/anaconda/envs/tensorflow/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events
self._handle_recv()
File "/Users/tiger/anaconda/envs/tensorflow/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv
self._run_callback(callback, msg)
File "/Users/tiger/anaconda/envs/tensorflow/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback
callback(*args, **kwargs)
File "/Users/tiger/anaconda/envs/tensorflow/lib/python3.5/site-packages/tornado/stack_context.py", line 275, in null_wrapper
return fn(*args, **kwargs)
File "/Users/tiger/anaconda/envs/tensorflow/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 276, in dispatcher
return self.dispatch_shell(stream, msg)
File "/Users/tiger/anaconda/envs/tensorflow/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 228, in dispatch_shell
handler(stream, idents, msg)
File "/Users/tiger/anaconda/envs/tensorflow/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 390, in execute_request
user_expressions, allow_stdin)
File "/Users/tiger/anaconda/envs/tensorflow/lib/python3.5/site-packages/ipykernel/ipkernel.py", line 196, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "/Users/tiger/anaconda/envs/tensorflow/lib/python3.5/site-packages/ipykernel/zmqshell.py", line 501, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "/Users/tiger/anaconda/envs/tensorflow/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2717, in run_cell
interactivity=interactivity, compiler=compiler, result=result)
File "/Users/tiger/anaconda/envs/tensorflow/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2827, in run_ast_nodes
if self.run_code(code, result):
File "/Users/tiger/anaconda/envs/tensorflow/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2881, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-14-71e47968c9c9>", line 1, in <module>
dnn_train(X_train, y_train, X_test, y_test, n_epochs = 10, keep_rate=0.99)
File "<ipython-input-13-636b5fd33c4d>", line 4, in dnn_train
x = tf.placeholder('float', [None, 784])
File "/Users/tiger/anaconda/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/ops/array_ops.py", line 1332, in placeholder
name=name)
File "/Users/tiger/anaconda/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1748, in _placeholder
name=name)
File "/Users/tiger/anaconda/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 749, in apply_op
op_def=op_def)
File "/Users/tiger/anaconda/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2380, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/Users/tiger/anaconda/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1298, in __init__
self._traceback = _extract_stack()
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'input_2/Placeholder' with dtype float
[[Node: input_2/Placeholder = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]