tf.data.Dataset:在完成 GeneratorDataset 迭代器时发生错误:前提条件失败:Python 解释器状态未初始化

问题描述

我需要将基于 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参数即可。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...