0
votes

When running a tensorflow program, I keep having the following error messages, the major part of which is something like TypeError: Fetch argument[....]has invalid type <class 'list'>, must be a string or Tensor. (Can not convert a list into a Tensor or Operation.) The related code segment that directly causes this error is _, loss, lr, gradients = sess.run((self.optimizer, self.net.cost, self.learning_rate_node, self.net.gradients_node), feed_dict={self.net.x: batch_x,self.net.y: util.crop_to_shape(batch_y,pred_shape), self.net.keep_prob: dropout})

I just feel confused about the reason to cause this error message because it can be run successfully by the author as shown in the github post.

The total error messages is as follows:

`Traceback (most recent call last):
File "/develop/tfw/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 480, in _process_fetches
allow_operation=True)
File "/develop/tfw/lib/python3.4/site-packages/tensorflow/python/framework/ops.py", line 2301, in as_graph_element
% (type(obj).name, types_str))
TypeError: Can not convert a list into a Tensor or Operation.

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "test.py", line 30, in path = trainer.train(generator, "./unet_trained", training_iters=20, epochs=100, display_step=2) File "/home/user/test/u-net/ver3/unet.py", line 364, in train self.net.keep_prob: dropout}) File "/develop/tfw/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 340, in run run_metadata_ptr) File "/develop/tfw/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 523, in _run processed_fetches = self._process_fetches(fetches) File "/develop/tfw/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 493, in _process_fetches % (subfetch, fetch, type(subfetch), str(e))) TypeError: Fetch argument [, , , , , , , , , , , , , , , , , , , ] of [, , , , , , , , , , < :0' shape=(16,) dtype=float32>, , , , , , , , ] has invalid type , must be a string or Tensor. (Can not convert a list into a Tensor or Operation.) `

1

1 Answers

0
votes

If you use Tensorflow version r0.9 or a previous version, Session.run didn't accept arbitrarily nested list or tuple in the fetches argument. So self.net.gradients_node (which is a list of tensor) in the tuple cause your TypeError. This code should work in Tensorflow r0.10. If you prefer stay at r0.9, modify the line :

_, loss, lr, gradients = sess.run((self.optimizer, self.net.cost, self.learning_rate_node, self.net.gradients_node), feed_dict={self.net.x: batch_x,self.net.y: util.crop_to_shape(batch_y,pred_shape), self.net.keep_prob: dropout})

to

list_of_outputs = sess.run([self.optimizer, self.net.cost, self.learning_rate_node]+ self.net.gradients_node, feed_dict={self.net.x: batch_x,self.net.y: util.crop_to_shape(batch_y,pred_shape), self.net.keep_prob: dropout})
loss = list_of_outputs[1]
lr = list_of_outputs[2]
gradients = list_of_outputs[2:]

or (with Python 3.*)

_,loss,lr,*gradients = sess.run([self.optimizer, self.net.cost, self.learning_rate_node]+ self.net.gradients_node, feed_dict={self.net.x: batch_x,self.net.y: util.crop_to_shape(batch_y,pred_shape), self.net.keep_prob: dropout})

This should work