tensorflow - 使用 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
代码有效,但我需要验证。
解决方案
由于没有人回答这个问题,我可以为您提供一种解决方法。您可以将 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 的情况下运行更大的网络。
希望能帮助到你。
推荐阅读
- swift - PromiseKit 6、Alamofire、Xcode 10.2/Swift 5
- javascript - 如何在javascript中舍入十进制数
- maya - Mel Button Color ScriptJob?
- knockout.js - 如何使用 KnockoutJs 更改我的语言内容?
- sql - 根据条件获取列的总和
- python - 如何确保函数按特定顺序运行?
- python - 如何使用 Mechanical Soup 在 textarea 中设置文本?
- java - 无法设置正确的文件路径
- google-cloud-platform - 如何在 Google Cloud Function 上使用 Node.js 按名称返回整个 Datastore 表
- c# - 具有参数是类的属性的类型