我想在属性上拆分一个 tf.data.DataSet
,在我的例子中是参与者或手势。目前,数据集正在进行中,参与者/手势的数量可能会增加。我最初设置了一个 tfds config为此 gestures-dataset , 但我也没有弄清楚如何在此处配置参与者/手势拆分。
我应该如何拆分 tf.data.DataSet
对象?目前,我的数据集作为单个 tf_record 存在。我宁愿保持这种方式,而不是为每个参与者和手势生成不同的文件,并且在添加新参与者时必须重新生成所有手势 tf 记录。
这个有效(方法 1,粗略):
subset = ds.filter(lambda x: (x['participant'] == 1 or x['participant'] == 2))
这不是(方法2,梦想):
subset = ds.filter(lambda x: any(x['participant'] == p for p in [1,2]))
OperatorNotAllowedInGraphError: using a
tf.Tensor
as a Pythonbool
is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function.
我还尝试了与修饰的@tf.function 相同的操作。
带有公开可用的 mnist 数据集的示例代码:juptyer notebook on colab
他们是以与方法 2 类似的方式执行此操作的方法吗?
最佳答案
您可以通过以下方式解决:
def predicate(label, labels_to_filter):
return tf.math.reduce_any(tf.equal(label, labels_to_filter))
labels_to_filter = tf.constant([0, 1, 2, 3, 4], dtype=tf.int64)
subset = dataset.filter(lambda x: predicate(x["participant"], labels_to_filter))
工作原理:
tf.equal
如果 label
与 labels_to_filter
中的标签之一匹配,则返回包含 True
值的 bool 张量>。 tf.math.reduce_any
如果其输入 bool 张量中存在 True
值,则返回 True
。
关于python - 参与者拆分的 tensorflow 数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69146074/