Невозможно прервать работу при переборе набора данных tenorflow, созданного с помощью `tf.data.Dataset.from_generator` - PullRequest
0 голосов
/ 04 марта 2020

Набор данных tensorflow, созданный из функции генератора, нельзя использовать с break внутри для l oop. С другой стороны, наборы данных, созданные без генератора, ломаются изящно. Ошибка tf.errors.CancelledError возникает из-за отмены, т. Е. Внутри l oop.

есть оператор break. См. Код ниже:

import numpy as np
import tensorflow as tf


def gen():
    while True:
        yield np.zeros(10, dtype=np.int32)


ds_without_gen = tf.data.Dataset.from_tensor_slices(
    [np.zeros(10, dtype=np.int32)]
).repeat()

ds_from_gen = tf.data.Dataset.from_generator(
    generator=gen, output_types=np.int32
)


# this works normally with break statement
print(">>>>>>>>> ds_without_gen")
for r in ds_without_gen.as_numpy_iterator():
    print(r)
    break


# this does work but a error is thrown by tensorflow as below
#    W tensorflow/core/kernels/data/generator_dataset_op.cc:103]
#    Error occurred when finalizing GeneratorDataset iterator:
#    Cancelled: Operation was cancelled
print(">>>>>>>>> ds_from_gen")
for r in ds_from_gen.as_numpy_iterator():
    print(r)
    break

Вывод:

2020-03-04 17:39:29.472521: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
2020-03-04 17:39:32.463827: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'nvcuda.dll'; dlerror: nvcuda.dll not found
2020-03-04 17:39:32.464110: E tensorflow/stream_executor/cuda/cuda_driver.cc:351] failed call to cuInit: UNKNOWN ERROR (303)
2020-03-04 17:39:32.469124: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: NXL23343
2020-03-04 17:39:32.469517: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: NXL23343
2020-03-04 17:39:32.469969: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
 ds_without_gen
[0 0 0 0 0 0 0 0 0 0]
 ds_from_gen
[0 0 0 0 0 0 0 0 0 0]
2020-03-04 17:39:32.528149: W tensorflow/core/kernels/data/generator_dataset_op.cc:103] Error occurred when finalizing GeneratorDataset iterator: Cancelled: Operation was cancelled
...