简体   繁体   English

当在 keras 层中指定 `input_shape` 时,Tensorflow 2.0 会泄漏内存

[英]Tensorflow 2.0 leaking memory when `input_shape` is specified in keras layer

I'm using Tensorflow 2.0 with keras APIs.我将 Tensorflow 2.0 与 keras API 一起使用。 There are situations where I need to generate multiple models within the same program execution (say, a cross-validation type of situation).在某些情况下,我需要在同一个程序执行中生成多个模型(例如,交叉验证类型的情况)。 When I generate these models without specifying the input shape, as in the following code snippet, there is no memory leak.当我在不指定输入形状的情况下生成这些模型时,如以下代码片段所示,没有内存泄漏。

import time

from tensorflow import keras


for _ in range(100):
    model = keras.model.Sequential()
    model.add(keras.kayers.Dense(120, activation='relu'))
    model.compile(loss='binary_crossentropy',
                  optimizer=keras.optimizers.SGD())
    time.sleep(0.1)

未指定输入形状的总内存

However, if I specify the input shape when adding the first layer of the model, it seems like models are piling up in memory, without being destroyed when not needed.但是,如果我在添加模型的第一层时指定输入形状,则模型似乎在内存中堆积,而不是在不需要时被销毁。 It also seems like the execution time increases quite a lot.执行时间似乎也增加了很多。

import time

from tensorflow import keras


for _ in range(100):
    model = keras.model.Sequential()
    model.add(keras.kayers.Dense(120, activation='relu', input_shape=(10, 10)))
    model.compile(loss='binary_crossentropy',
                  optimizer=keras.optimizers.SGD())
    time.sleep(0.1)

指定输入形状的总内存

I also generated a list of all the objects for the 2 code snippets, using Pympler .我还使用Pympler生成了 2 个代码片段的所有对象的列表。

from pympler import muppy
from pympler import summary


...

all_objects = muppy.get_objects()
occupancy = summary.summarize(all_objects)
summary.print_(occupancy)

For the models generated without input shape, here is the result:对于没有输入形状生成的模型,结果如下:

                                                                   types |   # objects |   total size
======================================================================== | =========== | ============
                                                             <class 'str |       79278 |     14.02 MB
                                                            <class 'dict |       14468 |      6.93 MB
                                                            <class 'code |       25252 |      3.49 MB
                                                            <class 'type |        2582 |      2.57 MB
                                                            <class 'list |        8573 |    944.06 KB
                                                           <class 'tuple |       12079 |    796.21 KB
                                                             <class 'set |         732 |    466.12 KB
                                                         <class 'weakref |        4475 |    349.61 KB
                                                     <class 'abc.ABCMeta |         341 |    346.27 KB
                     <class 'tensorflow.core.framework.op_def_pb2.ArgDef |        3822 |    328.45 KB
  <class 'google.protobuf.pyext.cpp_message.GeneratedProtocolMessageType |         369 |    320.16 KB
                                                            <class 'cell |        5921 |    277.55 KB
                                                     function (__init__) |        1694 |    224.98 KB
                                                        <class 'property |        2466 |    192.66 KB
                                              <class 'wrapper_descriptor |        2353 |    183.83 KB

And here is the snapshot for the models generated with the input shape.这是使用输入形状生成的模型的快照。

                                                           types |   # objects |   total size
================================================================ | =========== | ============
                                                   <class 'tuple |      296304 |     26.77 MB
                                                    <class 'dict |       71296 |     16.14 MB
                                                     <class 'str |       84828 |     14.54 MB
                                                     <class 'int |      395032 |     10.55 MB
                                                    <class 'list |       98027 |      9.89 MB
                                                    <class 'code |       25281 |      3.49 MB
                                                    <class 'type |        2587 |      2.58 MB
                                                     <class 'set |        2944 |    975.00 KB
               <class 'tensorflow.python.framework.ops.Operation |       14100 |    771.09 KB
    <class 'tensorflow.python.framework.ops.Operation._InputList |       14100 |    771.09 KB
                  <class 'tensorflow.python.framework.ops.Tensor |       14100 |    771.09 KB
  <class 'tensorflow.python.pywrap_tensorflow_internal.TF_Output |       13200 |    721.88 KB
                                            <class 'SwigPyObject |       14100 |    660.94 KB
                                 <class 'collections.OrderedDict |        1262 |    622.22 KB
                                                 <class 'weakref |        6127 |    478.67 KB

Is there anything wrong I'm doing?我做错了什么吗? According with the relevant keras documentation , the input shape should be somehow provided to the layer (the Tensorflow documentation about input shape says that if the layer has multiple inputs, the shape is not needed), however, my code still works fine without specifying the input shape.根据相关的keras 文档,输入形状应该以某种方式提供给层(关于输入形状的Tensorflow 文档说,如果层有多个输入,则不需要该形状),但是,我的代码在没有指定的情况下仍然可以正常工作输入形状。 I'm afraid this might be a bug in Tensorflow 2, but honestly I don't really know what's wrong.恐怕这可能是 Tensorflow 2 中的一个错误,但老实说,我真的不知道出了什么问题。

Notes笔记

  • Memory graphs are generated with the memory_profiler package.内存图是使用memory_profiler包生成的。
  • Memory snapshots are generated with the Pympler package.内存快照是使用Pympler包生成的。
  • Python 3.7.1 is used.使用 Python 3.7.1。
  • OS is Linux Elementary (Ubuntu).操作系统是 Linux Elementary (Ubuntu)。

Thank you for your support.感谢您的支持。

This is a long time after the post, but I found that my massive memory leak with TensorFlow.Keras was solved by just adding the line:这是帖子后很长一段时间,但我发现我使用 TensorFlow.Keras 的大量内存泄漏只需添加以下行即可解决:

tf.keras.backend.clear_session()

To the start of each iteration.到每次迭代的开始。 TensorFlow 2.0, Windows, Python 3.6/Anaconda TensorFlow 2.0、Windows、Python 3.6/Anaconda

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM