首页 > 解决方案 > 使用 GPU 进行数据验证的 Keras OOM

问题描述

我正在尝试使用 GPU 运行深度模型,并且似乎Keras正在对一批中的整个验证数据集运行验证,而不是在多个批次中进行验证,这导致了内存不足的问题:

tensorflow.python.framework.errors_impl.ResourceExhaustedError: OOM
when allocating tensor with shape[160000,64,64,1] and type double on
/job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc
[Op:GatherV2]

我在 CPU 上运行时没有这个问题,它只是在我在 GPU 上运行时发生,我的 fit 代码看起来像这样

history = model.fit(patches_imgs_train, patches_masks_train, batch_size=8,
                    epochs=10, shuffle=True, verbose=1, validation_split=0.2) 

当我从方法中删除验证参数时,fit代码有效,但我需要验证。

标签: tensorflowmachine-learningkerasout-of-memorygpu

解决方案


由于没有人回答这个问题,我可以为您提供一种解决方法。您可以将 fit() 和 evaluate() 分开并在 CPU 上运行评估。

您必须手动拆分数据以提供 testx 和 testy 来评估()。

for i in range(10):
    with tf.device('/GPU:0'):
       model.fit(x, y, epochs=1)

    with tf.device('/CPU:0'):
        loss, acc = model.evaluate(testx, testy)

如果您想要提前停止,您将需要处理准确度值。

它并不完美,但它可以让您在没有 OOM 的情况下运行更大的网络。

希望能帮助到你。


推荐阅读