简体   繁体   English

如何限制 tensorflow 内存使用?

[英]How to limit tensorflow memory usage?

I am new to TensorFlow.我是 TensorFlow 的新手。 I found it took up too much memory when I run a simple script.当我运行一个简单的脚本时,我发现它占用了太多内存。 I do not mean GPU memory, I mean CPU memory.我不是说 GPU 内存,我是说CPU内存。

Here is my script:这是我的脚本:

# -*- coding: utf-8 -*-

import time
import tensorflow as tf
tf_config = tf.ConfigProto()
tf_config.gpu_options.allow_growth = False
with tf.Session(config=tf_config) as sess:
    print('Listening.....')
    time.sleep(100) 

Memory usage of the python program above上面python程序的内存使用情况

According to my observation, 'import tensorflow as tf' takes about 100MB, and tf.Session takes others.根据我的观察,'import tensorflow as tf' 需要大约 100MB,而 tf.Session 需要其他人。

Well, I wonder if there is any way to optimize it?嗯,我想知道有没有什么方法可以优化它?

In recent TensorFlow 2.0 we could specify the required amount of memory explicitly.在最近的 TensorFlow 2.0 中,我们可以明确指定所需的内存量。

import tensorflow as tf
assert tf.version.VERSION.startswith('2.')

gpus = tf.config.experimental.list_physical_devices('GPU')

tf.config.experimental.set_virtual_device_configuration(gpus[0], 
   [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=1024)])

(Source: https://github.com/tensorflow/tensorflow/issues/25138#issuecomment-533936197 ) (来源: https : //github.com/tensorflow/tensorflow/issues/25138#issuecomment-533936197

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

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