有没有办法在 tf.data.Dataset w/tf.py

编程入门 行业动态 更新时间:2024-10-26 00:28:27
本文介绍了有没有办法在 tf.data.Dataset w/tf.py_func 中传递字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我在数据处理中使用 tf.data.Dataset,我想用 tf.py_func 应用一些 python 代码.

I'm using tf.data.Dataset in data processing and I want to do apply some python code with tf.py_func.

顺便说一句,我发现在 tf.py_func 中,我无法返回字典.有什么办法或解决方法吗?

BTW, I found that in tf.py_func, I cannot return a dictionary. Is there any way to do it or workaround?

我有如下代码

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)

==============================================================================

===========================================================================

已经有一段时间了,我忘记我问过这个问题了.我以另一种方式解决了它,它是如此简单,以至于我觉得我几乎是个傻瓜.问题是:

It's been a while and I forgot I asked this question. I solved it other way around and it was so easy that I felt I was almost a stupid. The problem was:

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)

推荐答案

您可以将字典转换为字符串,然后返回,然后拆分为字典.

You could turn the dictionary into a string which you return and then split into a dictionary.

这可能看起来像这样:

return (images + " " + labels + " " + new value)

然后在您的其他函数中:

and then in your other function:

l = map_func(image, label).split(" ")
d['images'] = l[0]
d[
...

这篇关于有没有办法在 tf.data.Dataset w/tf.py_func 中传递字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 03:47:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1403889.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:没有办法   tf   data   Dataset   py

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!