1

I have wrote a simple code to try out the Tensorflow summarize feature. The code is below.

import tensorflow as tf
import numpy as np

graph = tf.Graph()


with graph.as_default():
    x = tf.placeholder(tf.float32, [1, 2], name='x')
    W = tf.ones([2, 1], tf.float32, name='W')
    b = tf.constant([1.5], dtype=tf.float32, shape=(1, 1), name='bias')
    y_ = tf.add(tf.matmul(x, W, name='mul'), b, name='add')
tf.summary.scalar('y', y_)

with tf.Session(graph=graph) as session:
    merged = tf.summary.merge_all()
    fw = tf.summary.FileWriter("/tmp/tensorflow/logs", graph=graph)

    tf.global_variables_initializer().run()
    x_var = np.array([1., 1.], np.float32).reshape([1, 2])
    print(x_var)
    summary, y = session.run([merged, y_], feed_dict={x: x_var})
    fw.add_summary(summary, 0)
    print(y)

    fw.close()

Basically, it tries to implement y=Wx + b.

The code works if I remove all the summary related code. But if I add the summary related code, I got below error:

InvalidArgumentError (see above for traceback): tags and values not the same shape: [] != [1,1] (tag 'y')
     [[Node: y = ScalarSummary[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](y/tags, add)]]

I tried in both normal python, and IPython.

1 Answer 1

4

Tags and values do not have the same shape. You are passing x_var which is a vector and the summary takes a scalar value. You can simply use tf.reduce_mean to solve this problem:

with graph.as_default():
    x = tf.placeholder(tf.float32, [None, 2], name='x')
    W = tf.ones([2, 1], tf.float32, name='W')
    b = tf.constant([1.5], dtype=tf.float32, shape=(1, 1), name='bias')
    y_ = tf.add(tf.matmul(x, W, name='mul'), b, name='add')
    tf.summary.scalar('y', tf.reduce_mean(y_))

This will create a scalar value.

Sign up to request clarification or add additional context in comments.

2 Comments

With the help of your answer and this post, I finally get the code work. I also updated your answer.
The keys are, input x should NOT be fixed size; use reduce_* on the output y.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.