在Tensorflow 1.4.1中,tf.data.Dataset中的map方法可以返回字符串,所以我可以返回这样的地图函数:
return filename, image, one_hot_label
其中filename
是字符串。这在TF1.5 +中不再起作用:
dataset = dataset.map(self._mapper)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/data/ops/dataset_ops.py", line 838, in map
return MapDataset(self, map_func)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/data/ops/dataset_ops.py", line 1826, in __init__
self._map_func.add_to_graph(ops.get_default_graph())
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/function.py", line 488, in add_to_graph
self._create_definition_if_needed()
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/function.py", line 321, in _create_definition_if_needed
self._create_definition_if_needed_impl()
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/function.py", line 338, in _create_definition_if_needed_impl
outputs = self._func(*inputs)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/data/ops/dataset_ops.py", line 1814, in tf_map_func
ret, [t.get_shape() for t in nest.flatten(ret)])
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/data/ops/dataset_ops.py", line 1814, in <listcomp>
ret, [t.get_shape() for t in nest.flatten(ret)])
AttributeError: 'str' object has no attribute 'get_shape'
这是设计还是回归?
可重现的例子:
import tensorflow as tf
def map_fn(x):
return x*2, 'foo'
dataset = tf.data.Dataset.range(5)
dataset = dataset.map(map_fn)
答案 0 :(得分:1)
正如评论中所讨论的,这似乎是TF 1.5中的一个错误,至少为1.6,也可能是1.7。我在https://github.com/tensorflow/tensorflow/issues/18355
打开了一个Github问题在未来的Tensorflow版本中解决问题之前,我建议将字符串输出显式转换为张量:
import tensorflow as tf
def map_fn(x):
# Explicitly convert 'foo' to tensor
return x*2, tf.convert_to_tensor('foo')
dataset = tf.data.Dataset.range(5)
dataset = dataset.map(map_fn)