【问题标题】:Size of a Keras model increases after a new model is instantiated实例化新模型后,Keras 模型的大小会增加
【发布时间】:2020-05-07 19:44:11
【问题描述】:

标题不言自明,玩具代码如下:

from pympler import asizeof 
from keras.models import Sequential
from keras.layers import Dense

model_1 = Sequential([
  Dense(1, activation='relu', input_shape=(10,)),
])

print('Model 1 size = ', asizeof.asizeof(model_1))

model_2 = Sequential([
  Dense(1, activation='relu', input_shape=(10,)),
])

print('Model 1 size = ', asizeof.asizeof(model_1))
print('Model 2 size = ', asizeof.asizeof(model_2))

Pymler 是一个 Python 内存分析器。代码的输出是:

Model 1 size =  68624
Model 1 size =  92728
Model 2 size =  92728

想要的输出是:

Model 1 size =  68624
Model 1 size =  68624
Model 2 size =  68624

Python 版本:Python 3.6.8

Keras 版本:2.3.1

张量流版本:2.1.0

我怀疑这是一个错误,如果这确实是一个错误,我将在他们的 Github 中提交一个问题。

【问题讨论】:

    标签: python python-3.x tensorflow keras tensorflow2.0


    【解决方案1】:

    在文档中https://pympler.readthedocs.io/en/latest/library/asizeof.html 它说,

    如果全部为真并且没有提供位置参数。调整所有当前 gc 对象的大小,包括模块、全局和堆栈框架对象。

    也许您正在寻找的是basicsize

    from pympler import asizeof 
    import gc
    from keras.models import Sequential
    from keras.layers import Dense
    
    model_1 = Sequential([
      Dense(1, activation='relu', input_shape=(10,)),
    ])
    
    gc.collect()
    print('Model 1 size = ', asizeof.basicsize(model_1))
    
    gc.collect()
    model_2 = Sequential([
      Dense(1, activation='relu', input_shape=(10,)),
    ])
    
    print('Model 1 size = ', asizeof.basicsize(model_1))
    
    print('Model 2 size = ', asizeof.basicsize(model_2))
    

    它们应该给出相同的尺寸。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-16
      • 1970-01-01
      • 2020-04-05
      • 1970-01-01
      • 2014-09-03
      • 1970-01-01
      • 1970-01-01
      • 2019-05-08
      相关资源
      最近更新 更多