问题描述
我需要将基于 Sequence
的数据生成器转换为 tf.data.Dataset
格式。为此,我使用 from_generator
函数为我的所有训练、验证和测试数据创建重复的 BatchedDataset。
dataset = tf.data.Dataset.from_generator(gen_function,output_signature=output_signature)
dataset = dataset.shuffle(shuffle_buffer,reshuffle_each_iteration=True)
dataset = dataset.repeat()
dataset = dataset.batch(batch_size)
这些用于模型拟合:
OCR.model.fit(x=training_generator,validation_data=validation_generator,steps_per_epoch=steps_per_epoch,epochs=epochs,use_multiprocessing=True,callbacks=callbacks,workers=workers,verbose=verbose)
导致以下错误:
/user/.../python3.8/site-packages/tensorflow/python/keras/engine/data_adapter.py,line 739,in _validate_args raise ValueError(
ValueError: When providing an infinite dataset,you must specify the number of
steps to run (if you did not intend to create an infinite dataset,make sure to
not call `repeat()` on the dataset).
[date time]: W tensorflow/core/kernels/data/generator_dataset_op.cc:107] Error
occurred when finalizing GeneratorDataset iterator: Failed precondition: Python
interpreter state is not initialized. The process may be terminated.
>· [[{{node PyFunc}}]]
这令人困惑,因为我按照建议指定了重复无限数据集的步数。此外,当我之前使用基于序列的数据生成器时,它以这种方式与以这种方式指定的 steps_per_epoch
一起工作。
解决方法
解决方法很简单,除了validation_steps
函数中的steps_per_epoch
之外,你只需要指定fit
参数即可。