python - 参与者拆分的 tensorflow 数据集

标签 python tensorflow

我想在属性上拆分一个 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 Python bool 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 如果 labellabels_to_filter 中的标签之一匹配,则返回包含 True 值的 bool 张量>。 tf.math.reduce_any 如果其输入 bool 张量中存在 True 值,则返回 True

关于python - 参与者拆分的 tensorflow 数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69146074/

相关文章:

python - 使用 pyinstaller 编译时找不到 exe 文件

python - 为什么神经网络在自己的训练数据上预测错误?

c++ - 了解来自 tensorflow c++ API 的代码

python - TensorFlow 2 中自定义层的 call() 中的 tf.while_loop()

python-3.x - 未实现错误 : __deepcopy__() is only available when eager execution is enabled

python - 如何使用 object_detector.EfficientDetLite4Spec tensorflow lite 继续训练检查点

python - 如何使用sqlalchemy进行sql?

python - 我们可以在不定义 tf.placeholder 的情况下提供一个值吗?

python socket.connect 似乎不适用于虚拟 IP

python - cx_freeze 和导入模块