Ask questionstf.data.Dataset.from_tensor_slices: ValueError: Failed to convert a NumPy array to a Tensor (Unsupported␣ ,→object type list), worked on 2.0.0-beta1
<em>Please make sure that this is a bug. As per our GitHub Policy, we only address code/doc bugs, performance issues, feature requests and build/installation issues on GitHub. tag:bug_template</em>
You can collect some of this information using our environment capture script You can also obtain the TensorFlow version with:
python -c "import tensorflow as tf; print(tf.GIT_VERSION, tf.VERSION)"
python -c "import tensorflow as tf; print(tf.version.GIT_VERSION, tf.version.VERSION)"
Describe the current behavior I get an error when call tf.data.Dataset.from_tensor_slices with ValueError: Failed to convert a NumPy array to a Tensor (Unsupported␣ ,→object type list), see attachment Answer.Tensorflow.pad.sequence.feature.column.DenseFeatures.pdf
Describe the expected behavior I tried to use this example https://github.com/EgorBEremeev/SoloLearnML/blob/master/stackoverflow/Answer.%20Tensorflow%20pad%20sequence%20feature%20column.%20DenseFeatures.ipynb
It looks like it was running on 2.0.0-beta1, but not more in the current version. You can use this notebook to reproduce the case.
Standalone code to reproduce the issue Provide a reproducible test case that is the bare minimum necessary to generate the problem. If possible, please share a link to Colab/Jupyter/any notebook. https://github.com/EgorBEremeev/SoloLearnML/blob/master/stackoverflow/Answer.%20Tensorflow%20pad%20sequence%20feature%20column.%20DenseFeatures.ipynb You need to adapt the path the the csv file which will also be available in the repository.
Other info / 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.
Answer questions aaudiber
The problem is that
from_tensor_slices needs to convert its input into a Tensor, but the given input contains variable-length numpy lists, which cannot be converted into tensors (tensors must be rectangular). You can get the same error message by running
a = np.array([[1, 2, 3], [4, 5]], dtype=object) print(tf.convert_to_tensor(a))
This error appears to occur even in tensorflow 2.0.0-beta1, so it doesn't look like a regression.
To make this work, you need to pad the dataframe's lists so that they are the same length.