亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么 keras model.fit 使用了這么多內存,盡管使用了allow_growth

為什么 keras model.fit 使用了這么多內存,盡管使用了allow_growth

largeQ 2023-07-05 16:39:24
我最近發現,盡管我使用 set_session 和allow_growth=True,使用 model.fit 仍然意味著所有內存都被分配,并且我不能再將它用于程序的其余部分,即使函數退出并且由于模型是局部變量,因此模型不應再分配任何內存。下面是一些示例代碼來演示這一點:from numpy import arrayfrom keras import Input, Modelfrom keras.layers import Conv2D, Dense, Flattenfrom keras.optimizers import SGD# stops keras/tensorflow from allocating all the GPU's memory immediatelyfrom tensorflow.compat.v1.keras.backend import set_sessionfrom tensorflow.compat.v1 import Session, ConfigProto, GPUOptionstf_config = ConfigProto(gpu_options=GPUOptions(allow_growth=True))session = Session(config=tf_config)set_session(session)# makes the neural networkdef 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 modeldef 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))? ? returnwhile True:? ? main()當我使用 Pycharm 調試器運行此代碼時,我發現使用的 GPU RAM 一直保持在 0.1GB 左右,直到我第一次運行 model.fit,此時內存使用量在我的 4GB GPU RAM 中飆升至 3.2GB 。我還注意到,第一次運行 model.fit 后,內存使用量不會增加,并且如果我從網絡中刪除卷積層,內存使用量根本不會增加。有人可以解釋一下我的問題嗎?更新:將 GPUOptions 中的 per_process_gpu_memory_fraction 設置為 0.1 有助于限制所包含代碼中的效果,但不會限制我的實際程序中的效果。更好的解決方案仍然會有幫助。
查看完整描述

2 回答

?
FFIVE

TA貢獻1797條經驗 獲得超6個贊

我曾經面臨過這個問題。我從一個我再也找不到的人那里找到了解決方案。我將他的解決方案粘貼在下面。事實上,我發現如果你設置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)


查看完整回答
反對 回復 2023-07-05
?
繁華開滿天機

TA貢獻1816條經驗 獲得超4個贊

使用 SGD 進行訓練以及一批中的整個訓練數據可能(取決于您的輸入數據)非常消耗內存。嘗試將您的batch_size尺寸調整為較小的尺寸(例如 8、16、32)



查看完整回答
反對 回復 2023-07-05
  • 2 回答
  • 0 關注
  • 204 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號