next (iter ()) выдает ошибку при создании набора данных с использованием тензорного потока в python - PullRequest
0 голосов
/ 01 мая 2020

Я пытаюсь запустить приведенные ниже строки кода, чтобы создать набор данных с использованием тензорного потока в python. Я использую версию tenorflow '2.2.0-rc3'.

data = [[[2107, 1037, 3376, 2154, 1012, 1012, 1012, 10166], 1],
       [[3819, 2305, 2000, 2022, 2012, 1996, 3608, 2380], 1]]

all_dataset = tf.data.Dataset.from_generator(lambda: data,
                                             output_types = (tf.int32 , tf.int32))
next(iter(all_dataset))

Это дает мне следующую ошибку:

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/context.py in execution_mode(mode)
   1985       ctx.executor = executor_new
-> 1986       yield
   1987     finally:

10 frames
InvalidArgumentError: TypeError: `generator` yielded an element that did not match the expected structure. The expected structure was (tf.int32, tf.int32), but the yielded element was [[2107, 1037, 3376, 2154, 1012, 1012, 1012, 10166], 1].
Traceback (most recent call last):

  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/data/ops/dataset_ops.py", line 791, in generator_py_func
    flattened_values = nest.flatten_up_to(output_types, values)

  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/data/util/nest.py", line 396, in flatten_up_to
    assert_shallow_structure(shallow_tree, input_tree)

  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/data/util/nest.py", line 299, in assert_shallow_structure
    "Input has type: %s." % type(input_tree))

TypeError: If shallow structure is a sequence, input must also be a sequence. Input has type: <class 'list'>.

Во время обработки вышеупомянутого исключения, другое исключение произошло :

Traceback (most recent call last):

  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/script_ops.py", line 243, in __call__
    ret = func(*args)

  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/autograph/impl/api.py", line 309, in wrapper
    return func(*args, **kwargs)

  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/data/ops/dataset_ops.py", line 796, in generator_py_func
    "element was %s." % (output_types, values)), sys.exc_info()[2])

  File "/usr/local/lib/python3.6/dist-packages/six.py", line 692, in reraise
    raise value.with_traceback(tb)

  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/data/ops/dataset_ops.py", line 791, in generator_py_func
    flattened_values = nest.flatten_up_to(output_types, values)

  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/data/util/nest.py", line 396, in flatten_up_to
    assert_shallow_structure(shallow_tree, input_tree)

  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/data/util/nest.py", line 299, in assert_shallow_structure
    "Input has type: %s." % type(input_tree))

TypeError: `generator` yielded an element that did not match the expected structure. The expected structure was (tf.int32, tf.int32), but the yielded element was [[2107, 1037, 3376, 2154, 1012, 1012, 1012, 10166], 1].


     [[{{node PyFunc}}]] [Op:IteratorGetNext]

Во время обработки вышеупомянутого исключения произошло другое исключение:

InvalidArgumentError                      Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/executor.py in wait(self)
     65   def wait(self):
     66     """Waits for ops dispatched in this executor to finish."""
---> 67     pywrap_tfe.TFE_ExecutorWaitForAllPendingNodes(self._handle)
     68 
     69   def clear_error(self):

InvalidArgumentError: TypeError: `generator` yielded an element that did not match the expected structure. The expected structure was (tf.int32, tf.int32), but the yielded element was [[2107, 1037, 3376, 2154, 1012, 1012, 1012, 10166], 1].
Traceback (most recent call last):

  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/data/ops/dataset_ops.py", line 791, in generator_py_func
    flattened_values = nest.flatten_up_to(output_types, values)

  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/data/util/nest.py", line 396, in flatten_up_to
    assert_shallow_structure(shallow_tree, input_tree)

  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/data/util/nest.py", line 299, in assert_shallow_structure
    "Input has type: %s." % type(input_tree))

TypeError: If shallow structure is a sequence, input must also be a sequence. Input has type: <class 'list'>.

Во время обработки вышеупомянутого исключения произошло другое исключение:

Traceback (most recent call last):

  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/script_ops.py", line 243, in __call__
    ret = func(*args)

  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/autograph/impl/api.py", line 309, in wrapper
    return func(*args, **kwargs)

  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/data/ops/dataset_ops.py", line 796, in generator_py_func
    "element was %s." % (output_types, values)), sys.exc_info()[2])

  File "/usr/local/lib/python3.6/dist-packages/six.py", line 692, in reraise
    raise value.with_traceback(tb)

  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/data/ops/dataset_ops.py", line 791, in generator_py_func
    flattened_values = nest.flatten_up_to(output_types, values)

  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/data/util/nest.py", line 396, in flatten_up_to
    assert_shallow_structure(shallow_tree, input_tree)

  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/data/util/nest.py", line 299, in assert_shallow_structure
    "Input has type: %s." % type(input_tree))

TypeError: `generator` yielded an element that did not match the expected structure. The expected structure was (tf.int32, tf.int32), but the yielded element was [[2107, 1037, 3376, 2154, 1012, 1012, 1012, 10166], 1].


     [[{{node PyFunc}}]]

1 Ответ

0 голосов
/ 01 мая 2020

Вы сказали, что ваша цель состоит в том, чтобы создать набор данных с использованием tenorflow в python, а затем вместо from_generator, почему бы не использовать from_tensor_slices, проверьте, работает ли это для вас

data = [[[2107, 1037, 3376, 2154, 1012, 1012, 1012, 10166], 1],
       [[3819, 2305, 2000, 2022, 2012, 1996, 3608, 2380], 1]]

x = np.array([data[i][0] for i in range(len(data))])
y = np.array([data[i][1] for i in range(len(data))])

all_dataset = tf.data.Dataset.from_tensor_slices((x, y))
next(iter(all_dataset))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...