Python 如何将字典传递给tensorflow py_func

Python 如何将字典传递给tensorflow py_func,python,tensorflow,Python,Tensorflow,使用tensorflow数据集API时,map函数接受lambda和python函数 如果map中的python函数进行数据库调用,py_func可以包装python函数py_func函数抛出 ### Dict in Tensorflow def parse(data): # Make some DB calls with data, but here just add the values. return data[0] + data[1] iterator = tf.dat

使用tensorflow数据集API时,map函数接受
lambda和python函数

如果map中的python函数进行数据库调用,
py_func
可以包装python函数<当
inp
参数收到字典时,code>py_func函数抛出

### Dict in Tensorflow
def parse(data):
    # Make some DB calls with data, but here just add the values.
    return data[0] + data[1]

iterator = tf.data.Dataset.range(10).map(
     lambda x: {0: [x * 2], 1: [x ** 2]}).map(lambda x: 
     tf.py_func(parse, inp=[x], 
        Tout=tf.int64)).make_initializable_iterator()

init_op = iterator.initializer
get_next = iterator.get_next()

with tf.Session() as sess:
    sess.run(init_op)
    print(sess.run(get_next))
回溯

TypeError                                 Traceback (most recent call last)
 ~/miniconda3/envs/metadata/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py in make_tensor_proto(values, dtype, shape, verify_shape)
467     try:
--> 468       str_values = [compat.as_bytes(x) for x in proto_values]
469     except TypeError:

~/miniconda3/envs/metadata/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py in <listcomp>(.0)
467     try:
 --> 468       str_values = [compat.as_bytes(x) for x in proto_values]
469     except TypeError:

~/miniconda3/envs/metadata/lib/python3.6/site-packages/tensorflow/python/util/compat.py in as_bytes(bytes_or_text, encoding)
 64     raise TypeError('Expected binary or unicode string, got %r' %
 ---> 65                     (bytes_or_text,))
      66 

TypeError: Expected binary or unicode string, got {0: <tf.Tensor 'arg0:0' shape=(1,) dtype=int64>, 1: <tf.Tensor 'arg1:0' shape=(1,) dtype=int64>}
TypeError回溯(最近一次调用)
make\u tensor\u proto中的~/miniconda3/envs/metadata/lib/python3.6/site-packages/tensorflow/python/framework/tensor\u util.py(值、数据类型、形状、验证形状)
467尝试:
-->468 str_值=[proto_值中x的compat.as_字节(x)]
469除类型错误外:
~/miniconda3/envs/metadata/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py in(.0)
467尝试:
-->468 str_值=[proto_值中x的compat.as_字节(x)]
469除类型错误外:
~/miniconda3/envs/metadata/lib/python3.6/site-packages/tensorflow/python/util/compat.py作为字节(字节或文本,编码)
64 raise TypeError('应为二进制或unicode字符串,获得%r'%
--->65(字节或文本)
66
TypeError:应为二进制或unicode字符串,得到{0:,1:}
解决方法是将字典中的键和值作为单独的参数传递

py_func
能否将字典作为调用函数的参数传递


Tensorflow version==1.4,Python==3.5

我正在尝试运行您的示例,但在我的示例中,它没有经过第一个
映射
AttributeError:'list'对象没有属性“get_shape”
@GPhilo您正在运行哪个版本的TF?我使用的是
1.4.0
我使用的是最新版本,但据我所知,自1.4版以来
tf.data
的界面没有任何变化(至少变更日志没有提及任何内容)。现在我无法在1.4 ThoughPython3.5版上真正尝试这一点。谢谢您的尝试。
py_func
不适用于字典,只适用于列表