我正在使用启用了 eager 模式的 TF 1.8。
我无法打印 mapfunc 内的示例。当我从 mapfunc 中运行 tf.executing_eagerly() 时,我得到“False”
import os
import tensorflow as tf
tf.logging.set_verbosity(tf.logging.ERROR)
tfe = tf.contrib.eager
tf.enable_eager_execution()
x = tf.random_uniform([16,10], -10, 0, tf.int64)
print(x)
DS = tf.data.Dataset.from_tensor_slices((x))
def mapfunc(ex, con):
import pdb; pdb.set_trace()
new_ex = ex + con
print(new_ex)
return new_ex
DS = DS.map(lambda x: mapfunc(x, [7]))
DS = DS.make_one_shot_iterator()
print(DS.next())
print(new_ex) 输出:
Tensor("add:0", shape=(10,), dtype=int64)
在mapfunc之外,它工作得很好。但在其中,传递的示例没有值,也没有 .numpy() 属性。
The tf.data
转换实际上作为图形执行,因此映射函数本身的主体不会立即执行。看#14732 https://github.com/tensorflow/tensorflow/issues/14732#issuecomment-347276740对此进行更多讨论。
如果您确实需要立即执行地图函数,您可以使用tf.contrib.eager.py_func https://www.tensorflow.org/api_docs/python/tf/contrib/eager/py_func,所以类似:
DS = DS.map(lambda x: tf.contrib.eager.py_func(
mapfunc,
[x, tf.constant(7, dtype=tf.int64)], tf.int64)
# In TF 1.9+, the next line can be print(next(DS))
print(DS.make_one_shot_iterator().next())
希望有帮助。
请注意,通过添加py_func
对于数据集,单线程 Python 解释器将循环生成每个元素。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)