我正在使用Tensorflow Dataset API准备要输入到我的网络中的数据。在此过程中,我有一些自定义的Python函数,这些函数使用tf.py_function
映射到数据集。我希望能够调试进入这些函数的数据以及这些函数内部的数据发生了什么。调用py_function
时,它将返回到主Python进程(根据this answer)。由于此函数是在Python中以及在主进程中,因此,我希望常规的IDE断点能够在此进程中停止。但是,情况似乎并非如此(以下示例中的断点不停止执行)。是否有办法掉入数据集py_function
使用的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
手动设置跟踪功能,该功能将连接到默认调试器服务器并开始向其提供调试数据。
示例代码:
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))
注意:如果您使用的是已经捆绑了pydevd的IDE(例如PyDev或PyCharm),则不必单独安装pydevd
,它将在调试会话期间安装。