为什么 tensorflow 会消耗这么多内存?

Why is tensorflow consuming this much memory?

TensorFlow 消耗的内存比可用内存多(显然会导致程序崩溃)。

我的问题是:为什么 TensorFlow 需要这么多内存才能 运行 我的网络?我不明白是什么占用了这么多 space(也许多次缓存数据以优化卷积计算?保存所有隐藏的输出用于反向传播目的?)。有没有办法防止 TensorFlow 消耗这么多内存?

旁注:

如你所说:

All my convolutions are 5x5 windows, 1x1 stride, with (from 1st one to last one) 32, 64, 128 and 256 features. I am using leaky ReLUs and 2x2 max pooling. FC layers are composed of 64 and 3 neurones.

因此,您的网络内存消耗如下:

Input: 640x640x3 = 1200(以 KB 为单位)

C1: 636x636x32 = 12.5 MB(步幅=1 有效)

P1: 635x635x32 = 12.3 MB(步幅=1 有效)

C2: 631x631x64 = 24.3MB

P2: 630x630x64 = 24.2MB

C3: 626x626x128 = 47.83 MB

P3: 625x625x128 = 47.68 MB

C4: 621x621x256 = 94.15 MB

P4: 620x620x256 = 93.84 MB

FC1: 64 = 0.0625 KB(可忽略不计)

FC2: 3 = 0.003 KB(可忽略不计)

Total for one image = ~ 358 MB

For batch of 56 image = 56 x 358 ~19.6 GB

这就是为什么您的网络在 6 GB 上没有 运行 的原因。尝试用一些 higher stridelower sized image 将其调整为 6 GB space。 它应该有效。

您可以参考以更好地了解内存消耗计算。