Python 3.x 如何在tensorflow 2数据集中使用带元组的映射?

Python 3.x 如何在tensorflow 2数据集中使用带元组的映射?,python-3.x,tensorflow-datasets,tensorflow2.0,Python 3.x,Tensorflow Datasets,Tensorflow2.0,尝试将一个元组映射到TF2中数据集中的一个元组(请参见下面的代码)。我的输出(请参见下文)显示map函数只被调用一次。我似乎无法理解元组 如何从输入参数a中获得“a”、“b”、“c”: tuple Tensor("args_0:0", shape=(3,), dtype=string) type <class 'tensorflow.python.framework.ops.Tensor'> 元组张量(“args_0:0”,shape=(3,),dtype=string) 类型

尝试将一个元组映射到TF2中数据集中的一个元组(请参见下面的代码)。我的输出(请参见下文)显示map函数只被调用一次。我似乎无法理解元组

如何从输入参数a中获得“a”、“b”、“c”:

tuple Tensor("args_0:0", shape=(3,), dtype=string)
type <class 'tensorflow.python.framework.ops.Tensor'>
元组张量(“args_0:0”,shape=(3,),dtype=string) 类型 编辑:似乎使用Dataset.from_tensor_切片一次生成所有数据。这解释了为什么map只被调用一次。因此,我可能需要以其他方式创建数据集

from __future__ import absolute_import, division, print_function, unicode_literals
from timeit import default_timer as timer
print('import tensorflow')
start = timer()
import tensorflow as tf
end = timer()
print('Elapsed time: ' + str(end - start),"for",tf.__version__)
import numpy as np
def map1(tuple):
    print("<<<")
    print("tuple",tuple)
    print("type",type(tuple))
    print("shape",tuple.shape)
    print("tuple 0",tuple[0])
    print("type 0",type(tuple[0]))
    print("shape 0",tuple.shape[0])
    # how do i get "a","b","c" from the input parameter?
    print(">>>")
    return ("1","2","3")
l=[]
l.append(("a","b","c"))
l.append(("d","e","f"))
print(l)
ds=tf.data.Dataset.from_tensor_slices(l)
print("ds",ds)
print("start mapping")
result = ds.map(map1)
print("end mapping")


$ py mapds.py
import tensorflow
Elapsed time: 12.002168990751619 for 2.0.0
[('a', 'b', 'c'), ('d', 'e', 'f')]
ds <TensorSliceDataset shapes: (3,), types: tf.string>
start mapping
<<<
tuple Tensor("args_0:0", shape=(3,), dtype=string)
type <class 'tensorflow.python.framework.ops.Tensor'>
shape (3,)
tuple 0 Tensor("strided_slice:0", shape=(), dtype=string)
type 0 <class 'tensorflow.python.framework.ops.Tensor'>
shape 0 3
>>>
end mapping
来自未来导入绝对导入、除法、打印函数、unicode文本
从timeit导入默认\u计时器作为计时器
打印(“导入tensorflow”)
开始=计时器()
导入tensorflow作为tf
结束=计时器()
打印('经过的时间:'+str(结束-开始),“for”,tf.\uuuuuuuuu版本)
将numpy作为np导入
def map1(元组):
打印(“”)
报税表(“1”、“2”、“3”)
l=[]
l、 附加((“a”、“b”、“c”))
l、 附加((“d”、“e”、“f”))
印刷品(l)
ds=tf.data.Dataset.from_tensor_切片(l)
打印(“ds”,ds)
打印(“开始映射”)
结果=ds.map(map1)
打印(“结束映射”)
$py mapds.py
输入张量流
运行时间:2.0.0的12.002168990751619
[('a','b','c'),('d','e','f')]
ds
开始映射
>
端映射

映射函数(
map1
)返回的一个或多个值决定了返回数据集中每个元素的结构
在您的例子中,
result
是一个tf数据集,您的编码没有任何错误

要检查是否正确映射了每个touple,您可以遍历数据集的每个样本,如下所示:
[更新代码]

    def map1(tuple):
        print(tuple[0].numpy().decode("utf-8")) # Print first element of tuple
        return ("1","2","3")
    l=[]
    l.append(("a","b","c"))
    l.append(("d","e","f"))
    ds=tf.data.Dataset.from_tensor_slices(l)
    ds = ds.map(lambda tpl: tf.py_function(map1, [tpl], [tf.string, tf.string, tf.string]))

    for sample in ds:
        print(str(sample[0].numpy().decode()), sample[1].numpy().decode(), sample[2].numpy().decode())
Output:
a
1 2 3
d
1 2 3

希望能有所帮助。

我仍然不知道如何从输入参数中获取“a”、“b”、“c”,该参数是:元组张量(“args_0:0”,shape=(3,),dtype=string)类型请查看我的更新答案。我在急切模式下使用tf 1.14。这在转换后的代码中得到了一个:AttributeError:mapds.py:17 map1*打印(“numpy decode”,tuple[0].numpy().decode(“utf-8”))AttributeError:“Tensor”对象没有属性“numpy”,请在激活急切模式后使用
tf.启用急切执行()
.tf.急切执行()返回true。我还能做什么?