限制Tensorflow CPU和内存使用

22

我看到了关于使用Tensorflow时GPU内存的几个问题,但是我在没有GPU支持的Pine64上安装了它。

这意味着我只能使用非常有限的资源(仅CPU和RAM),而Tensorflow似乎想要尽可能利用这些资源,导致我的机器完全卡死。


是否有一种方法可以限制分配给Tensorflow的处理能力和内存量?类似于bazel自己的--local_resources标志?

2个回答

16

这将创建一个一次只运行一个操作的会话,并且每个操作只使用一个线程

sess = tf.Session(config=
    tf.ConfigProto(inter_op_parallelism_threads=1,
                   intra_op_parallelism_threads=1))

我不确定是否限制了内存,貌似是按需分配的。我曾经在使用TensorFlow时,当我的神经网络需要100GB的RAM时,导致我的机器死机。所以我的解决方案是使用需要较少RAM的神经网络。


2
这个问题抛出了异常 TypeError: target must be a string, but got <class 'tensorflow.core.protobuf.config_pb2.ConfigProto'> Exception AttributeError: "'Session' object has no attribute '_session'" in <bound method Session.__del__ of <tensorflow.python.client.session.Session object at 0x7f8411bba0d0>> ignored,但是添加 config 关键字(即 sess = tf.Session(config=tf.ConfigProto(inter_op_parallelism_threads=1, intra_op_parallelism_threads=1)))解决了这个问题。 - Laurynas Tamulevičius
我尝试在TF 2.2.0上使用它,但出现错误:模块“tensorflow”没有属性“Session”。通过sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(inter_op_parallelism_threads=1, intra_op_parallelism_threads=1))解决了这个问题,但它并没有解决我的问题:CPU负载达到了100%。 - Alex Ivanov
@AlexIvanov,请查看我的答案。 - Romeo Kienzler

5
在TensorFlow 2.x中,此问题已在以下"thread"中得到解答:thread
在TensorFlow 2.x中不再使用session。直接使用config API在程序开始时设置并行性。
import tensorflow as tf

tf.config.threading.set_intra_op_parallelism_threads(2)
tf.config.threading.set_inter_op_parallelism_threads(2)
with tf.device('/CPU:0'):
    model = tf.keras.models.Sequential([...

https://www.tensorflow.org/api_docs/python/tf/config/threading


3
内存使用情况如何? - hosford42

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接