【问题标题】:How to disable TensorFlow GPU?如何禁用 TensorFlow GPU?
【发布时间】:2021-04-27 23:07:06
【问题描述】:

我首先在我的 GPU 上使用 python 创建了我的 TensorFlow 代码:

import tensorflow-gpu as tf

我将它用于培训目的,一切都很顺利。但是现在,我想在没有 GPU 的设备上部署我的 python 脚本。所以,我用 pip 卸载了 tensorflow-gpu 并导入了普通的 TensorFlow:

import tensorflow as tf

但是当我运行脚本时,它仍在使用 gpu:

我试过这段代码:

try:
    # Disable all GPUS
    tf.config.set_visible_devices([], 'GPU')
    visible_devices = tf.config.get_visible_devices()
    print(visible_devices)
    for device in visible_devices:
        assert device.device_type != 'GPU'
except Exception as e:
    # Invalid device or cannot modify virtual devices once initialized.
    print(e)

但仍然无法正常工作(即使 gpu 似乎已禁用,如屏幕截图中的白色所示)。

我只想返回没有 GPU 功能的默认 TensorFlow 安装。 我尝试卸载并安装 tensorflow,删除虚拟环境并创建一个新环境,但没有任何效果。

感谢您的帮助!

【问题讨论】:

  • 这能回答你的问题吗? Prevent TensorFlow from accessing the GPU?
  • 不,我没有 CUDA_VISIBLE_DEVICES 环境变量。而且我更愿意安装非 GPU 的 TensorFlow。
  • 你看过其他答案吗?比如 .. sess_cpu = tf.Session(config=tf.ConfigProto(device_count={'GPU': 0}))
  • 或者,您可以使用带有非 GPU TensorFlow 的 virtualenv。
  • sess_cpu = tf.Session(config=tf.ConfigProto(device_count={'GPU': 0})) 不起作用。它引发:AttributeError:模块'tensorflow'没有属性'Session'

标签: python tensorflow gpu tensorflow2.0 cpu


【解决方案1】:

Tensorflow > 2.x 具有默认的 GPU 支持。了解更多请访问Tensorflow网站。

根据上面的截图,它只显示 CPU。 并注意Adding visible GPU devices: 0

如果您仍想仅使用 CPU 启用 Tensorflow,请使用 Tensorflow==1.15

【讨论】:

    猜你喜欢
    • 2017-08-21
    • 2021-08-13
    • 2018-12-20
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    • 2020-01-14
    • 1970-01-01
    • 2020-02-28
    相关资源
    最近更新 更多