【问题标题】:Why does keras model.fit use so much memory despite using allow_growth=True?尽管使用了allow_growth = True,为什么keras model.fit会使用这么多内存?
【发布时间】:2020-12-20 13:08:29
【问题描述】:

我有,感谢this 问题主要能够解决张量流分配我不想分配的内存的问题。然而,我最近发现,尽管我使用 set_session 和 allow_growth=True,但使用 model.fit 仍然意味着所有内存都已分配,我不能再将它用于我的程序的其余部分,即使函数退出并且由于模型是局部变量,因此模型不应再分配任何内存。 以下是一些示例代码:

from numpy import array
from keras import Input, Model
from keras.layers import Conv2D, Dense, Flatten
from keras.optimizers import SGD

# stops keras/tensorflow from allocating all the GPU's memory immediately
from tensorflow.compat.v1.keras.backend import set_session
from tensorflow.compat.v1 import Session, ConfigProto, GPUOptions
tf_config = ConfigProto(gpu_options=GPUOptions(allow_growth=True))
session = Session(config=tf_config)
set_session(session)


# makes the neural network
def make_net():
    input = Input((2, 3, 3))
    conv = Conv2D(256, (1, 1))(input)
    flattened_input = Flatten()(conv)
    output = Dense(1)(flattened_input)
    model = Model(inputs=input, outputs=output)
    sgd = SGD(0.2, 0.9)
    model.compile(sgd, 'mean_squared_error')
    model.summary()
    return model


def make_data(input_data, target_output):
    input_data.append([[[0 for i in range(3)] for j in range(3)] for k in range(2)])
    target_output.append(0)


def main():
    data_amount = 4096
    input_data = []
    target_output = []
    model = make_model()
    for i in range(data_amount):
        make_data(input_data, target_output)
    model.fit(array(input_data), array(target_output),  batch_size=len(input_data))
    return


while True:
    main()

当我使用 Pycharm 调试器运行此代码时,我发现使用的 GPU RAM 保持在 0.1GB 左右,直到我第一次运行 model.fit,此时内存使用量高达 4GB 中的 3.2GB GPU RAM。我还注意到,在第一次运行 model.fit 后,内存使用量并没有增加,如果我从网络中移除卷积层,内存使用量根本不会增加。 有人可以帮我解释一下我的问题吗?

更新:将 GPUOptions 中的 per_process_gpu_memory_fraction 设置为 0.1 有助于限制包含的代码中的效果,但不适用于我的实际程序。更好的解决方案仍然会有所帮助。

【问题讨论】:

  • 我注意到您直接从 keras 导入了一些模块,这些模块在 make_net 中使用,它返回您在 .fit 上运行的模型。当您从 tensorflow.compat.v1.keras 导入这些模块时,内存消耗有何变化?
  • 抱歉,没有任何变化

标签: python tensorflow memory keras


【解决方案1】:

我曾经遇到过这个问题。我从一个我再也找不到的人那里找到了解决方案。我在下面粘贴他的解决方案。其实我发现,如果你设置allow_growth=True,tensorflow似乎会占用你所有的内存。所以你应该设置你的最大限制。

试试这个:

gpus = tf.config.experimental.list_physical_devices("GPU")
if gpus:
    # Restrict TensorFlow to only use the first GPU
    try:
        for gpu in gpus:
            tf.config.experimental.set_memory_growth(gpu, False)
            tf.config.experimental.set_virtual_device_configuration(
                gpu,
                [
                    tf.config.experimental.VirtualDeviceConfiguration(
                        memory_limit=12288  # set your limit
                    )
                ],
            )
        tf.config.experimental.set_visible_devices(gpus[0], "GPU")
        logical_gpus = tf.config.experimental.list_logical_devices("GPU")
        print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPU")
    except RuntimeError as e:
        # Visible devices must be set before GPUs have been initialized
        print(e)

【讨论】:

    【解决方案2】:

    使用 SGD 进行训练和一批中的整个训练数据可能(取决于您的输入数据)非常消耗内存。 尝试将您的 batch_size 调整为较小的尺寸(例如 8、16、32)

    【讨论】:

    • 嗨@YannickFunk,这只是说明我的问题的一段代码。在我的实际代码中,我正在执行数千批 4096。此外,如问题中所述,当我不使用卷积层时,内存增长不再发生。这将我的网络减少到大约 60% 的大小,但现在只需要 0.1GB。这表明主要问题是尽管我将 allow_growth 设置为 true,但 keras 仍在预分配内存。更不用说在 fit 函数完成后内存仍然分配的事实。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    • 2012-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-01
    相关资源
    最近更新 更多