【发布时间】:2019-08-20 01:18:24
【问题描述】:
我在数据处理中使用 tf.data.Dataset,我想用 tf.py_func 应用一些 python 代码。
顺便说一句,我发现在 tf.py_func 中,我无法返回字典。有什么办法或解决方法吗?
我的代码如下所示
def map_func(images, labels):
"""mapping python function"""
# do something
# cannot be expressed as a tensor graph
return {
'images': images,
'labels': labels,
'new_key': new_value}
def tf_py_func(images, labels):
return tf.py_func(map_func, [images, labels], [tf.uint8, tf.string], name='blah')
return dataset.map(tf_py_func)
================================================ ==============================
已经有一段时间了,我忘了我问过这个问题。 我以另一种方式解决了它,这很容易,以至于我觉得自己几乎是个愚蠢的人。问题是:
- tf.py_func 无法返回字典。
- dataset.map 可以返回字典。
答案是:映射两次。
def map_func(images, labels):
"""mapping python function"""
# do something
# cannot be expressed as a tensor graph
return processed_images, processed_labels
def tf_py_func(images, labels):
return tf.py_func(map_func, [images, labels], [tf.uint8, tf.string], name='blah')
def _to_dict(images, labels):
return { 'images': images, 'labels': labels }
return dataset.map(tf_py_func).map(_to_dict)
【问题讨论】:
-
但这是适合的必需格式吗?我认为它类似于 ({'words': words_data, 'importance':importance_data}, {'output1': output1_data, 'output2': output2_data}) stackoverflow.com/questions/61249708/…
标签: python tensorflow tensorflow-datasets