Repository navigation
Description
Please go to Stack Overflow for help and support:
If you open a GitHub issue, here is our policy:
- It must be a bug or a feature request.
- The form below must be filled out.
- It shouldn't be a TensorBoard issue. Those go here.
Here's why we have that policy: TensorFlow developers respond to issues. We want to focus on work that benefits the whole community, e.g., fixing bugs and adding features. Support only helps individuals. GitHub also notifies thousands of people when issues are filed. We want them to see you communicating an interesting problem, rather than being redirected to Stack Overflow.
System information
- Have I written custom code (as opposed to using a stock example script provided in TensorFlow): Yes
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Windows 10 x64
- TensorFlow installed from (source or binary): binary
- TensorFlow version (use command below): 1.2.1
- Python version: 3.6.1 (Anaconda 4.4.0 64-bit)
- Bazel version (if compiling from source):
- CUDA/cuDNN version:
- GPU model and memory: GTX 780
- Exact command to reproduce: tf.reshape()
You can collect some of this information using our environment capture script:
You can obtain the TensorFlow version with
python -c "import tensorflow as tf; print(tf.GIT_VERSION, tf.VERSION)"
Describe the problem
Describe the problem clearly here. Be sure to convey here why it's a bug in TensorFlow or a feature request.
tf.reshape does not accept a list with mixed Integer and Dimension() as elements for the shape parameter. It should accept shapes that have Dimension as elements since tensor shapes consist of dimensions. Specifically, tf.tensor.shape returns a list of Dimensions, therefore using a similar object to specify a shape in tf.reshape should not cause an error.
Source code / logs
Include any logs or source code that would be helpful to diagnose the problem. If including tracebacks, please include the full traceback. Large logs and files should be attached. Try to provide a reproducible test case that is the bare minimum necessary to generate the problem.
Example code:
X = tf.placeholder("float", [1,784])
X = tf.reshape(X, [1, X.shape[-1]])
results in
TypeError: Failed to convert object of type <class 'list'> to Tensor. Contents: [1, Dimension(784)]. Consider casting elements to a supported type.
This is rectified by casting X.shape[-1] to int before passing to tf.reshape.
A similar unrelated issue is that X.shape returns (784,) while it should return (1,784). This requires reshaping to turn the placeholder back into a 2D tensor. I haven't determined if this is a bug but it occurs when the tensor is explicitly specified as a 2D tensor so it is probably worth changing.
Activity
yaroslavvb commentedon Aug 2, 2017
One fix could be to add some code before this line that casts all tf.Dimension objects in the list to Python int
shivaniag commentedon Aug 2, 2017
We currently don't have anybody working on this. It would be great if you could help us by working on this and submitting a PR. Let us know if you need further clarification. Thanks!
pmccarter commentedon Aug 3, 2017
I'd be interested in working on this, might take a few days to figure out dev workflow.
yaroslavvb commentedon Aug 3, 2017
I think you basically need to do something along the lines of:
And then run the tests and see if something breaks. The
lenrestriction is because this only really matters for shapes and TF has rank limit of 8facaiy commentedon Aug 9, 2017
Hi, @pmccarter . Because
tf.reshapeis generated automatically, and the implementation is written by C++, hence the question is more difficult to debug than I expected. I opened a PR #12127 to take a try to fix it. If your work has not been started yet, is it OK to link the PR to the issue? Thanks.yaroslavvb commentedon Aug 9, 2017
@facaiy this is a Python specific issue so it needs to be at Python side. Look at the stack trace of failing reshape to see where it's happening. The Python layer calls tensor_util.make_tensor_proto which calls numpy.asarray on the result. Numpy asarray tries to figure out dtype of the list, and because it doesn't know about tf.Dimension, it makes it type "np.object" which corresponds to "tf.string".
The ideal way to fix it would be to figure out how to modify tf.Dimension object so that np.asarray would recognize them as integer. IE, you want
np.asarray([1,tf.Dimension(1)])to return
array([1, 1]instead of
array([1, Dimension(1)], dtype=object)If that's not possible, the next best thing would be to have special case for tf.Dimension object to convert them to int just before
np.asarraycall. You would want to special-case this for length <=8 since we don't need larger ranks for shape calculations, and this would avoid performance penalty for more general arraysfacaiy commentedon Aug 9, 2017
@yaroslavvb Thanks for your suggestion! Yes, I agree with you that casting object to string is a really awkward solution.
As for the two solutions you advised, I will take a try:
np.asarrayrecognize tf.Dimension as int, ornp.asarraycall.facaiy commentedon Aug 9, 2017
Hi, @yaroslavvb .
If I understand correctly,
[1, Dimension(1)]doesn't gonp.asarrayroute,In fact, it goes the last branch. In the route,
[1, Dimension(1)]is treated as a list of objects. Then each object is treated as string, see:That's why I think obj.str() might fix the issue. However, the solution is worse in performance. Do you have a better suggestion? Thanks.
yaroslavvb commentedon Aug 9, 2017
Hm, I may be missing where
obj.stris turned intoint. Anyway, the performance should be OK if you restrict it to tensors of length <= 8pmccarter commentedon Aug 9, 2017
np.array([1, tf.Dimension(784)], dtype=tf.int32) returns array([1, 2], dtype=int32), so maybe a solution would be to set the dtype to int if it is None and values is a list mix of numbers and Dimensions?
facaiy commentedon Aug 10, 2017
Thanks, all.
@pmccarter 's advice sounds attractive, I'd like to dig deeper.
pmccarter commentedon Aug 10, 2017
What about defining a thin ungenerated reshape() function that delegates to the generated function, and coerces the shape to a 2d int list if possible?
yaroslavvb commentedon Aug 10, 2017
@pmccarter actually I like that idea . It would have much less side-effects. We already have Python ops which are essentially 1 line wrappers over underlying generated ops, like reduce sum
tensorflow/tensorflow/python/ops/math_ops.py
Line 1274 in 2cd0810
pmccarter commentedon Aug 10, 2017
Cool I can probably get a pull request in tomorrow.
CharlesMei commentedon Sep 1, 2017
X.get_shape().as_list()[-1]
RoboEvangelist commentedon Mar 7, 2018
I am using tensorflow 1.4.1, and I am still having the same problem with models/official/resnet/resnet.py
TypeError: Failed to convert object of type <class 'tensorflow.python.data.ops.dataset_ops.PrefetchDataset'> to Tensor. Contents: <PrefetchDataset shapes: ((?, 32, 32, 3), (?, 10)), types: (tf.float32, tf.float32)>. Consider casting elements to a supported type.When will this be fixed? I would not want to spend time fixing this myself, please
facaiy commentedon Mar 7, 2018
@RoboEvangelist The bug has been fixed. And about your problem, I don't think so. As the exception said, you mistake Dataset as Tensor.
RoboEvangelist commentedon Mar 12, 2018
@facaiy I just upgraded to tensorflow 1.6.0, and I don't have the problem anymore. It is still a bug in tensorflow 1.4.1 due to my experience
dhinar1991 commentedon Apr 1, 2018
@yaroslavvb @mrry I have a completely different error that says
Failed to convert object of type <class 'werkzeug.datastructures.File.Storage> to tensor.
below is the client file with flask framework for tensorflow serving running inside a docker. https://github.com/tensorflow/tensorflow/issues/18157
giladsharir commentedon Apr 3, 2018
I still have this issue, using tensorflow 1.7
Duke-ding commentedon Apr 8, 2018
i also have this issue
Duke-ding commentedon Apr 8, 2018
def read_and_decode(filename):
filename_quece = tf.train.string_input_producer([filename])#读入流中
if name == 'main':
img,label = read_and_decode("train_data.tfrecords")
print(img.shape)
print(img[0])
img_batch = tf.reshape(img_batch,[None,2562563])
ValueError: Cannot feed value of shape (32, 256, 256, 3) for Tensor 'Placeholder_2:0', which has shape '(?, 196608)'
I can't deal with this issue