是否可以/建议在tf.data.Dataset.map的映射函数中评估张量?我想先创建一个文件名数据集,然后应用随机播放并对其重复。然后,从该数据集中,使用tf.data.Dataset.map创建一个新的数据集。映射函数将加载文件并以张量形式返回内容。
问题是,为了加载文件的内容,我想我需要eval()字符串张量文件名。但是,在映射函数中使用eval()时,tensorflow似乎不喜欢它。这是一个简单的测试用例:
>>> sess = tf.Session()
>>> with sess.as_default():
... d1 = tf.data.Dataset.from_tensor_slices([1,2,3])
... d2 = d1.map(lambda s: tf.convert_to_tensor(s.eval(session = sess)))
...
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
File "/home/hrbigelow/anaconda3/lib/python3.6/site-
packages/tensorflow/python/data/ops/dataset_ops.py", line 875, in map
return MapDataset(self, map_func)
File "/home/hrbigelow/anaconda3/lib/python3.6/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 1864, in __init__
self._map_func.add_to_graph(ops.get_default_graph())
File "/home/hrbigelow/anaconda3/lib/python3.6/site- packages/tensorflow/python/framework/function.py", line 475, in add_to_graph
self._create_definition_if_needed()
File "/home/hrbigelow/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/function.py", line 331, in _create_definition_if_needed
self._create_definition_if_needed_impl()
File "/home/hrbigelow/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/function.py", line 340, in _create_definition_if_needed_impl
self._capture_by_value, self._caller_device)
File "/home/hrbigelow/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/function.py", line 806, in func_graph_from_py_func
outputs = func(*func_graph.inputs)
File "/home/hrbigelow/anaconda3/lib/python3.6/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 1829, in tf_map_func
ret = map_func(nested_args)
File "<stdin>", line 3, in <lambda>
File "/home/hrbigelow/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 678, in eval
return _eval_using_default_session(self, feed_dict, self.graph, session)
File "/home/hrbigelow/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 4981, in _eval_using_default_session
raise ValueError("Cannot use the given session to evaluate tensor: "
ValueError: Cannot use the given session to evaluate tensor: the tensor's graph is different from the session's graph.