第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

為什么 keras model.fit 使用了這么多內(nèi)存,盡管使用了allow_growth

為什么 keras model.fit 使用了這么多內(nèi)存,盡管使用了allow_growth

largeQ 2023-07-05 16:39:24
我最近發(fā)現(xiàn),盡管我使用 set_session 和allow_growth=True,使用 model.fit 仍然意味著所有內(nèi)存都被分配,并且我不能再將它用于程序的其余部分,即使函數(shù)退出并且由于模型是局部變量,因此模型不應(yīng)再分配任何內(nèi)存。下面是一些示例代碼來(lái)演示這一點(diǎn):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()當(dāng)我使用 Pycharm 調(diào)試器運(yùn)行此代碼時(shí),我發(fā)現(xiàn)使用的 GPU RAM 一直保持在 0.1GB 左右,直到我第一次運(yùn)行 model.fit,此時(shí)內(nèi)存使用量在我的 4GB GPU RAM 中飆升至 3.2GB 。我還注意到,第一次運(yùn)行 model.fit 后,內(nèi)存使用量不會(huì)增加,并且如果我從網(wǎng)絡(luò)中刪除卷積層,內(nèi)存使用量根本不會(huì)增加。有人可以解釋一下我的問(wèn)題嗎?更新:將 GPUOptions 中的 per_process_gpu_memory_fraction 設(shè)置為 0.1 有助于限制所包含代碼中的效果,但不會(huì)限制我的實(shí)際程序中的效果。更好的解決方案仍然會(huì)有幫助。
查看完整描述

2 回答

?
FFIVE

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個(gè)贊

我曾經(jīng)面臨過(guò)這個(gè)問(wèn)題。我從一個(gè)我再也找不到的人那里找到了解決方案。我將他的解決方案粘貼在下面。事實(shí)上,我發(fā)現(xiàn)如果你設(shè)置allow_growth=True,tensorflow 似乎會(huì)使用你所有的內(nèi)存。所以你應(yīng)該只設(shè)置你的最大限制。


嘗試這個(gè):


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)


查看完整回答
反對(duì) 回復(fù) 2023-07-05
?
繁華開(kāi)滿(mǎn)天機(jī)

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超4個(gè)贊

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



查看完整回答
反對(duì) 回復(fù) 2023-07-05
  • 2 回答
  • 0 關(guān)注
  • 195 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)