python垃圾收集器不会删除tf.keras模型

问题描述

我在其中存储了tf.keras模型

实例化类后,我调用方法来更新模型。

为什么每次调用update()时消耗的内存量都会增加?感觉像垃圾收集器没有删除旧模型

代码:

from tensorflow.keras.models import Model
from tensorflow.keras import layers

from memory_profiler import memory_usage


class CustomModel:
    def __init__(self):
        self.model = self.create_model()

    def create_model(self):
        input_x = layers.Input((64,300),name='fasttext_output')
        lstm = layers.Bidirectional(layers.LSTM(256))(input_x)

        dense_neurons = []
        for i in range(30):
            x = layers.Dense(64,name='intent_{}_layer_1'.format(i))(lstm)
            x = layers.Dense(1,name='intent_{}_layer_2'.format(i),activation='sigmoid')(x)
            dense_neurons.append(x)

        out = layers.Concatenate()(dense_neurons)
        out = layers.Reshape([30])(out)

        model = Model(inputs=[input_x],outputs=[out])
        return model

    def update(self):
        self.model = self.create_model()


custom_model = CustomModel()

custom_model.update()
print(memory_usage())
custom_model.update()
print(memory_usage())
custom_model.update()
print(memory_usage())
custom_model.update()
print(memory_usage())

输出:

[313.734375]
[335.171875]
[356.9765625]
[378.9921875]

P.S。 跟踪通过资源监视器(mac os catalina)和htop(ubuntu 18)消耗的内存时,也会观察到类似情况-我的意思是,问题与memory_profiler模块无关。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...