Victor Ocampo Victor Ocampo - 1 year ago 68
Python Question

Can not convert a function into a Tensor or Operation. Tensorflow Error

So I am getting this Error in tensorflow (1.2) (python 3):

WARNING:tensorflow:Passing a `GraphDef` to the SummaryWriter is deprecated. Pass a `Graph` object instead, such as `sess.graph`.
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/", line 267, in __init__
fetch, allow_tensor=True, allow_operation=True))
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/", line 2584, in as_graph_element
return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/", line 2673, in _as_graph_element_locked
% (type(obj).__name__, types_str))
TypeError: Can not convert a function into a Tensor or Operation.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/theshoutingparrot/Desktop/Programming/Python/MachineLearningPY/Tensorflow/", line 54, in <module>
summary_str =, feed_dict={x: batch_xs, y: batch_ys})
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/", line 789, in run
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/", line 984, in _run
self._graph, fetches, feed_dict_string, feed_handles=feed_handles)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/", line 410, in __init__
self._fetch_mapper = _FetchMapper.for_fetch(fetches)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/", line 238, in for_fetch
return _ElementFetchMapper(fetches, contraction_fn)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/", line 271, in __init__
% (fetch, type(fetch), str(e)))
TypeError: Fetch argument <function merge_all at 0x7f7d0f3d8620> has invalid type <class 'function'>, must be a string or Tensor. (Can not convert a function into a Tensor or Operation.)

And here's the code:

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)

import tensorflow as tf

learning_rate = 0.01
training_iteration = 30
batch_size = 100
display_step = 2

x = tf.placeholder("float", [None, 784])
y = tf.placeholder("float", [None, 10])

W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

with tf.name_scope("Wx_b") as scope:
model = tf.nn.softmax(tf.matmul(x, W) + b)

w_h = tf.summary.histogram("weights", W)
b_h = tf.summary.histogram("biases", b)

with tf.name_scope("cost_function") as scope:
cost_function = -tf.reduce_sum(y*tf.log(model))
tf.summary.scalar("cost_function", cost_function)

with tf.name_scope("train") as scope:
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost_function)

init = tf.global_variables_initializer() #tf.initialize_all_variables()

merged_summary_op = tf.summary.merge_all

#Launch the graph

with tf.Session() as sess:
summary_writer = tf.summary.FileWriter('/home/theshoutingparrot/work/logs', graph_def=sess.graph_def)

for iteration in range(training_iteration):
avg_cost = 0.
total_batch = int(mnist.train.num_examples/batch_size)

for i in range(total_batch):
batch_xs, batch_ys = mnist.train.next_batch(batch_size), feed_dict={x: batch_xs, y: batch_ys})

avg_cost +=, feed_dict={x: batch_xs, y: batch_ys})/total_batch

summary_str =, feed_dict={x: batch_xs, y: batch_ys})
summary_writer.add_summary(summary_str, iteration*total_batch + i)

if iteration % display_step == 0:
print("Iteration", '%04d' % (iteration + 1), "cost=", "{:.9f}".format(avg_cost))
print("Tuning completed!")

predictions = tf.equal(tf.argmax(model,1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(predictions, "float"))
print("Accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))

I am new to tensorflow. I "got" this code from this video (tutorial)

He (the person in the tutorial (Siraj Raval)) is using an older version of tensorflow so that's why a bit of the code is different like this (example):

w_h = tf.histogram_summary("weights", W) => w_h = tf.summary.histogram("weights", W)

More info:

I have tried to run the same code with python (2.7) (of course I have downloaded tensorflow for Python 2.7) but It gives me the same error.

Any help would be nice, Thx in advance.

Answer Source

Replace merged_summary_op = tf.summary.merge_all with merged_summary_op = tf.summary.merge_all()

This is actually what the error message is telling you: TypeError: Can not convert a function into a Tensor or Operation -> tf.summary.merge_all is a function, not a tensor or operation, you can't run it with, opposite to tf.summary.merge_all()

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download