【发布时间】:2018-07-06 09:40:17
【问题描述】:
我有下面的 map 函数(可运行示例),它输入 string 并输出 string 和 integer。
在tf.data.Dataset.from_tensor_slices 中,我将原始输入命名为'filenames'。但是当我从映射函数map_element_counts 返回值时,我只能返回一个元组(返回字典会产生异常)。
有没有办法命名从我的map_element_counts 函数返回的 2 个元素?
import tensorflow as tf
filelist = ['fileA_6', 'fileB_10', 'fileC_7']
def map_element_counts(fname):
# perform operations outside of tensorflow
return 'test', 10
ds = tf.data.Dataset.from_tensor_slices({'filenames': filelist})
ds = ds.map(map_func=lambda x: tf.py_func(
func=map_element_counts, inp=[x['filenames']], Tout=[tf.string, tf.int64]
))
element = ds.make_one_shot_iterator().get_next()
with tf.Session() as sess:
print(sess.run(element))
结果:
(b'test', 10)
期望的结果:
{'elementA': b'test', 'elementB': 10)
添加细节:
当我执行return {'elementA': 'test', 'elementB': 10} 时,我得到了这个异常:
tensorflow.python.framework.errors_impl.UnimplementedError: Unsupported object type dict
【问题讨论】:
-
返回字典有什么异常?
-
我将它添加到问题的底部。
-
你能放完整的堆栈跟踪吗?
标签: python dictionary tensorflow mapping tensorflow-datasets