Search code examples
python-2.7tensorflowtensorboard

Tensorboad - add_summary makes my code crashes


I am very new to tensorflow, and I try to display my first tensorboard.

I downloaded and executed ok the board for the example given here https://www.tensorflow.org/versions/r0.7/how_tos/summaries_and_tensorboard/index.html

Following the method, I have in my code:

weights_hidden = tf.Variable(tf.truncated_normal([image_size * image_size, 1024]), name='weights_hidden')
_ = tf.histogram_summary('weights_hidden', weights_hidden)

and when I run the session

with tf.Session(graph=graph) as session:
  merged = tf.merge_all_summaries()
  writer = tf.train.SummaryWriter("/tmp/test", session.graph_def)
  tf.initialize_all_variables().run()
  for step in range(num_steps):
    summary_str, l, predictions = session.run(
      [optimizer, loss, train_prediction], feed_dict=feed_dict)

    if (step % 500 == 0):
      writer.add_summary(summary_str, step)

The process crashes with the following error

Traceback (most recent call last):
  File "/home/xxx/Desktop/xxx/xxx.py", line 108, in <module>
    writer.add_summary(summary_str, step)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/summary_io.py", line 128, in add_summary
    event = event_pb2.Event(wall_time=time.time(), summary=summary)
  File "/usr/local/lib/python2.7/dist-packages/google/protobuf/internal/python_message.py", line 522, in init
    _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
  File "/usr/local/lib/python2.7/dist-packages/google/protobuf/internal/python_message.py", line 453, in _ReraiseTypeErrorWithFieldName
    six.reraise(type(exc), exc, sys.exc_info()[2])
  File "/usr/local/lib/python2.7/dist-packages/google/protobuf/internal/python_message.py", line 520, in init
    copy.MergeFrom(new_val)
  File "/usr/local/lib/python2.7/dist-packages/google/protobuf/internal/python_message.py", line 1237, in MergeFrom
    "expected %s got %s." % (cls.__name__, type(msg).__name__))
TypeError: Parameter to MergeFrom() must be instance of same class: expected Summary got NoneType. for field Event.summary

What am I missing ? Any help/comment would be very welcome

Thank you very much for the help

K.


Solution

  • You should write:

    _, summary_str, l, predictions = session.run(
      [optimizer, merged, loss, train_prediction], feed_dict=feed_dict)
    

    I added a 4th argument merged which corresponds to the summary you are trying to get (you were only getting the result of the optimization step).