python - 停止迭代 : generator_output = next(output_generator)

标签 python numpy keras

我重写了以下代码以处理大规模数据集。我正在使用 Python 生成器根据逐批生成的数据拟合模型。

def subtract_mean_gen(x_source,y_source,avg_image,batch):
    batch_list_x=[]
    batch_list_y=[]
    for line,y in zip(x_source,y_source):
        x=line.astype('float32')
        x=x-avg_image
        batch_list_x.append(x)
        batch_list_y.append(y)
        if len(batch_list_x) == batch:
            yield (np.array(batch_list_x),np.array(batch_list_y))
            batch_list_x=[]
            batch_list_y=[] 

model = resnet.ResnetBuilder.build_resnet_18((img_channels, img_rows, img_cols), nb_classes)
model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

val = subtract_mean_gen(X_test,Y_test,avg_image_test,batch_size)
model.fit_generator(subtract_mean_gen(X_train,Y_train,avg_image_train,batch_size), steps_per_epoch=X_train.shape[0]//batch_size,epochs=nb_epoch,validation_data = val,
                    validation_steps = X_test.shape[0]//batch_size)

我收到以下错误:

239/249 [===========================>..] - ETA: 60s - loss: 1.3318 - acc: 0.8330Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/usr/local/lib/python2.7/dist-packages/keras/utils/data_utils.py", line 560, in data_generator_task
    generator_output = next(self._generator)
StopIteration

240/249 [===========================>..] - ETA: 54s - loss: 1.3283 - acc: 0.8337Traceback (most recent call last):
  File "cifa10-copy.py", line 125, in <module>
    validation_steps = X_test.shape[0]//batch_size)
  File "/usr/local/lib/python2.7/dist-packages/keras/legacy/interfaces.py", line 87, in wrapper
    return func(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1809, in fit_generator
    generator_output = next(output_generator)
StopIteration

我调查了一个类似的问题 here但是,我无法解决引发 StopIteration 的错误。

最佳答案

keras 的生成器必须是无限的:

def subtract_mean_gen(x_source,y_source,avg_image,batch):
    while True:
        batch_list_x=[]
        batch_list_y=[]
        for line,y in zip(x_source,y_source):
            x=line.astype('float32')
            x=x-avg_image
            batch_list_x.append(x)
            batch_list_y.append(y)
            if len(batch_list_x) == batch:
                yield (np.array(batch_list_x),np.array(batch_list_y))
                batch_list_x=[]
                batch_list_y=[] 

错误的发生是因为 keras 试图获得一个新的批处理,但是你的生成器已经结束了。 (即使您定义了正确数量的步骤,keras 也有一个队列将尝试从生成器获取更多批处理,即使您处于最后一步。)

显然,您有一个默认队列大小,它是 10(异常出现在结束前 10 个批处理,因为队列试图在结束后获取一个批处理)。

关于python - 停止迭代 : generator_output = next(output_generator),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48709839/

相关文章:

python - 使用 openpyxl 在 python 中对齐单元格的正确方法

python - 如何在 Django 中编写一个以 URL 作为参数的自定义管理命令?

python - 混合两个列表python

python - 根据另一列的函数在一列中查找值

python - SVM - 如何向量化核化克矩阵?

python - 'SparseTensor' 对象不是可订阅的 keras

python - 在 SQLAlchemy 中处理插入时重复的主键(声明式样式)

python - NumPy - 计算直方图交集

python - 使用 Keras 模型进行强化学习

python - 对 keras 中两个模型的输出进行矩阵乘积