python - TensorFlow Dataset API 中的 IDE 断点映射 py_function?
问题描述
我正在使用Tensorflow 数据集 API来准备我的数据以输入到我的网络中。在此过程中,我有一些自定义 Python 函数,这些函数使用tf.py_function
. 我希望能够调试进入这些函数的数据以及这些函数中的数据会发生什么。当 apy_function
被调用时,这会回调到主要的 Python 进程(根据这个答案)。由于这个函数在 Python 中,并且在主进程中,我希望一个常规的 IDE 断点能够在这个进程中停止。但是,情况似乎并非如此(下面的示例断点不会停止执行)。有没有办法进入py_function
Dataset 使用的断点map
?
断点不停止执行的示例
import tensorflow as tf
def add_ten(example, label):
example_plus_ten = example + 10 # Breakpoint here.
return example_plus_ten, label
examples = [10, 20, 30, 40, 50, 60, 70, 80]
labels = [ 0, 0, 1, 1, 1, 1, 0, 0]
examples_dataset = tf.data.Dataset.from_tensor_slices(examples)
labels_dataset = tf.data.Dataset.from_tensor_slices(labels)
dataset = tf.data.Dataset.zip((examples_dataset, labels_dataset))
dataset = dataset.map(map_func=lambda example, label: tf.py_function(func=add_ten, inp=[example, label],
Tout=[tf.int32, tf.int32]))
dataset = dataset.batch(2)
example_and_label = next(iter(dataset))
解决方案
tf.data.Dataset 的 Tensorflow 2.0 实现为每个调用打开一个 C 线程,而不通知您的调试器。使用pydevd
's 手动设置一个跟踪函数,该函数将连接到您的默认调试器服务器并开始为其提供调试数据。
import pydevd
pydevd.settrace()
您的代码示例:
import tensorflow as tf
import pydevd
def add_ten(example, label):
pydevd.settrace(suspend=False)
example_plus_ten = example + 10 # Breakpoint here.
return example_plus_ten, label
examples = [10, 20, 30, 40, 50, 60, 70, 80]
labels = [ 0, 0, 1, 1, 1, 1, 0, 0]
examples_dataset = tf.data.Dataset.from_tensor_slices(examples)
labels_dataset = tf.data.Dataset.from_tensor_slices(labels)
dataset = tf.data.Dataset.zip((examples_dataset, labels_dataset))
dataset = dataset.map(map_func=lambda example, label: tf.py_function(func=add_ten, inp=[example, label],
Tout=[tf.int32, tf.int32]))
dataset = dataset.batch(2)
example_and_label = next(iter(dataset))
注意:如果您使用的 IDE 已经捆绑了 pydevd(例如 PyDev 或 PyCharm),您不必pydevd
单独安装,它将在调试会话期间拾取。
推荐阅读
- android - 由 Camera2 字节巨大的位图
- python - NeuralNetwork 类无法访问其属性
- angular - 由于这个奇怪的问题,无法运行 ionic 3 build
- google-classroom - 授权 API 访问所有课程和所有课程作业需要哪些帐户权限
- javascript - 以编程方式设置输入值将在其中包含“\”
- python - 元组索引超出范围?
- java - 如何使用primefaces gMap实现谷歌地图绘制路线
- uwp - C# UWP KeyDown 事件 e.Handled 奇怪的行为
- recaptcha-v3 - V3 标记真实访客
- scala - Scala中可覆盖的默认类型参数