gpt4 book ai didi

python - 如何减少 Tensorflow/Keras 使用的 CPU 数量?

转载 作者:行者123 更新时间:2023-11-28 16:56:25 32 4
gpt4 key购买 nike

我正在使用 Tensorflow 2.0 的 Keras api。

在我的 Keras 模型上调用 fit 时,它会使用所有可用的 CPU。

我想限制使用的 CPU 数量。然而,它在以前版本的 TensorFlow 中的工作方式不能再使用了:

tf.keras.backend.set_session(tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(
intra_op_parallelism_threads=2, inter_op_parallelism_threads=2)))

AttributeError: module 'tensorflow.python.keras.api._v2.keras.backend' has no attribute 'set_session'

我该怎么做?

最佳答案

在 Tensorflow 2.0 中,不再有 session 。在 eager execution 中,直接使用 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

关于python - 如何减少 Tensorflow/Keras 使用的 CPU 数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57925061/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com