Как использовать карту с кортежами в наборе данных Tensflow 2? - PullRequest
0 голосов
/ 21 ноября 2019

пытается отобразить кортеж на кортеж в наборе данных в tf 2 (см. Код ниже). мой вывод (см. ниже) показывает, что функция map вызывается только один раз. и я не могу получить в кортеж.

как я могу получить на «а», «b», «с» из входного параметра, который является:

tuple Tensor("args_0:0", shape=(3,), dtype=string)
type <class 'tensorflow.python.framework.ops.Tensor'>

edit: похоже, что использование Dataset.from_tensor_slices создает данные сразу. это объясняет, почему 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

1 Ответ

0 голосов
/ 21 ноября 2019

Значение или значения, возвращаемые функцией карты (map1), определяют структуру каждого элемента в возвращенном наборе данных. [Ref]
В вашем случае result - это набор данных tf, и в вашем кодировании нет ничего плохого.

Чтобы проверить, правильно ли сопоставлены все касания, вы можете пройти по каждомуПример вашего набора данных выглядит следующим образом:
[Обновленный код]

    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

Надеюсь, это поможет.

...