TF.data.dataset.map(map_func) with Eager Mode(具有渴望模式的 TF.data.dataset.map(map_func))
问题描述
我正在使用启用了 Eager 模式的 TF 1.8.
I am using TF 1.8 with eager mode enabled.
我无法在 mapfunc 中打印示例.当我从 mapfunc 中运行 tf.executing_eagerly() 时,我得到False"
I cannot print the example inside the mapfunc. It when I run tf.executing_eagerly() from within the mapfunc I get "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) 输出:
print(new_ex) outputs:
Tensor("add:0", shape=(10,), dtype=int64)
在 mapfunc 之外,它工作正常.但在其中,传递的示例没有值,也没有 .numpy() 属性.
Outside mapfunc, it works fine. But inside it, the passed example does not have a value, nor .numpy() attribute.
推荐答案
tf.data 转换实际上是作为图形执行的,因此 map 函数本身并不会急切地执行.有关此问题的更多讨论,请参阅 #14732.
The tf.data transformations actually execute as a graph, so the body of the map function itself isn't executed eagerly. See #14732 for some more discussion on this.
如果你真的需要 map 函数的急切执行,你可以使用 tf.contrib.eager.py_func,比如:
If you really need eager execution for the map function, you could use tf.contrib.eager.py_func, so something like:
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 解释器将在生成的每个元素的循环中.
Note that by adding a py_func to the dataset, the single-threaded Python interpreter will be in the loop for every element produced.
这篇关于具有渴望模式的 TF.data.dataset.map(map_func)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:具有渴望模式的 TF.data.dataset.map(map_func)
基础教程推荐
- 求两个直方图的卷积 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 包装空间模型 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
