not sure if this is a bug in the library.
Question also posted here:
https://stackoverflow.com/questions/49333991/tensorflow-add-summary-invalidargumenterror-tags-and-values-not-the-same-shape
i have written a small network with 2 hidden layers (having 4 neurons each)
goal is to predict a decimal number when binary numbers are given as input
i'm trying to add loss in tensorboard summary. but its not allowing.
below is the code i'm using
import tensorflow as tf
import numpy as np
train_data = {
'data' : [ [0.,0.,0.],[1.,0.,0.],[0.,1.,0.],[1.,1.,0.],[0.,0.,1.],[1.,0.,1.],[0.,1.,1.],[1.,1.,1.] ],
'label' : [[0.],[1.],[2.],[3.],[4.],[5.],[6.],[7.]]
}
x_data = tf.placeholder(tf.float32,shape=[None,3],name="x_placeholder")
y_actual = tf.placeholder(tf.float32,name="y_placeholder")
hidden_layer1 = {
'Weights': tf.Variable(tf.random_normal([3,4])),
'Bias': tf.Variable(tf.random_normal([4]))
}
hidden_layer2 = {
'Weights': tf.Variable(tf.random_normal([4, 4])),
'Bias': tf.Variable(tf.random_normal([4]))
}
output_layer = {
'Weights': tf.Variable(tf.random_normal([4, 1])),
'Bias': tf.Variable(tf.random_normal([1]))
}
def Neural_Network(input):
hl1 = tf.add(tf.matmul(input,hidden_layer1['Weights']) , hidden_layer1['Bias'])
hl1 = tf.nn.relu(hl1)
hl2 = tf.add(tf.matmul(hl1,hidden_layer2['Weights']) , hidden_layer2['Bias'])
hl2 = tf.nn.relu(hl2)
output = tf.add(tf.matmul(hl2,output_layer['Weights']) , output_layer['Bias'])
return output
def get_data(i):
return np.reshape(train_data['data'][i],[1,3]) , train_data['label'][i]
def Train():
prediction = Neural_Network(x_data)
loss = tf.squared_difference(prediction,y_actual)
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(loss)
tf.summary.scalar('LOSS',tf.reshape(loss,[1]))
sess = tf.Session()
merged = tf.summary.merge_all()
writer = tf.summary.FileWriter("Graph/",sess.graph)
sess.run(tf.global_variables_initializer())
epoch_x = None
epoch_y = None
for epoch in range(100):
for i in range(8):
epoch_x , epoch_y = get_data(i)
sess.run(train_step, feed_dict={x_data : epoch_x, y_actual:epoch_y})
print("Prediction :", sess.run(prediction, feed_dict={x_data : epoch_x})," actual ",epoch_y)
writer.add_summary(sess.run(merged,feed_dict={x_data : np.array(epoch_x), y_actual:np.array(epoch_y)}), epoch) #Error here
if epoch % 10 == 0:
l = sess.run(loss, feed_dict={x_data: epoch_x, y_actual: epoch_y})
print('loss ', l)
print('=' * 40)
Train()
below is the error message
InvalidArgumentError (see above for traceback): tags and values not the same shape: [] != [1] (tag 'LOSS')
[[Node: LOSS = ScalarSummary[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"](LOSS/tags, Reshape)]]
below is entire call stack
Prediction : [[ 0.89533418]] actual [0.0]
Traceback (most recent call last):
File "C:\Users\Prakash\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1350, in _do_call
return fn(*args)
File "C:\Users\Prakash\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1329, in _run_fn
status, run_metadata)
File "C:\Users\Prakash\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 473, in __exit__
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: tags and values not the same shape: [] != [1] (tag 'LOSS')
[[Node: LOSS = ScalarSummary[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"](LOSS/tags, Reshape)]]During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/Prakash/Documents/Received Files/binary_number_prediction.py", line 71, in
Train()
File "C:/Users/Prakash/Documents/Received Files/binary_number_prediction.py", line 61, in Train
writer.add_summary(sess.run(merged,feed_dict={x_data : np.array(epoch_x), y_actual:np.array(epoch_y)}), epoch)
File "C:\Users\Prakash\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 895, in run
run_metadata_ptr)
File "C:\Users\Prakash\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1128, in _run
feed_dict_tensor, options, run_metadata)
File "C:\Users\Prakash\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1344, in _do_run
options, run_metadata)
File "C:\Users\Prakash\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1363, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: tags and values not the same shape: [] != [1] (tag 'LOSS')
[[Node: LOSS = ScalarSummary[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"](LOSS/tags, Reshape)]]Caused by op 'LOSS', defined at:
File "C:/Users/Prakash/Documents/Received Files/binary_number_prediction.py", line 71, in
Train()
File "C:/Users/Prakash/Documents/Received Files/binary_number_prediction.py", line 47, in Train
tf.summary.scalar('LOSS',tf.reshape(loss,[1]))
File "C:\Users\Prakash\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\summary\summary.py", line 100, in scalar
val = _gen_logging_ops._scalar_summary(tags=tag, values=tensor, name=scope)
File "C:\Users\Prakash\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\gen_logging_ops.py", line 469, in _scalar_summary
"ScalarSummary", tags=tags, values=values, name=name)
File "C:\Users\Prakash\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "C:\Users\Prakash\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 3160, in create_op
op_def=op_def)
File "C:\Users\Prakash\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 1625, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-accessInvalidArgumentError (see above for traceback): tags and values not the same shape: [] != [1] (tag 'LOSS')
[[Node: LOSS = ScalarSummary[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"](LOSS/tags, Reshape)]]
i'm passing same feed_dict to both Train_stepand merged
looks like some issue with shape, but while training (Train_step) the shape is proper (no error).
i get this error only while adding loss to scalar summary
Scalar summaries only log rank-0 data. Maybe we could try removing the tf.reshape op that takes the loss? Or reshape into a scalar, ie tf.reshape(loss,[])?
@chihuahua , Thanks for your reply
removingtf.reshapedidn't help
buttf.reshape(loss,[]) worked
Most helpful comment
Scalar summaries only log rank-0 data. Maybe we could try removing the
tf.reshapeop that takes the loss? Or reshape into a scalar, ietf.reshape(loss,[])?