[tensorflow]cpu版本和gpu版本(安装)

1,215 阅读5分钟

在Python环境中安装TensorFlow框架的CPU版本和GPU版本,可以通过创建独立的虚拟环境来实现,避免版本冲突。

补充:

其实, 我们在通过tensorflow或者pytorch等深度学习框架来进行深度学习模型训练的时候, 并不一定要用到GPU资源来加速, 当然, 如果你有GPU资源的话, 你当然可以用, 这样更好.

tensorflow这个库是有2种版本的, 一种是纯cpu版本的, 也就是说该版本只支持cpu资源, 不支持gpu, 即使你有gpu资源也没有办法去用

另外一种是既支持cpu也支持gpu的, 当检测到你的系统上没有gpu资源的时候, 会自动的回退到cpu资源, 这一点你不用担心.

以下是详细步骤:


1. 安装CPU版本

方法1:直接安装纯CPU版本(推荐)

# 创建并激活虚拟环境(可选)
python -m venv tf_cpu_env
source tf_cpu_env/bin/activate  # Linux/macOS
# 或 tf_cpu_env\Scripts\activate  # Windows

# 安装纯CPU版本的TensorFlow
pip install tensorflow-cpu  # 这是纯cpu版本的

方法2:安装标准版(自动使用CPU)

pip install tensorflow  # 默认包含CPU支持,无GPU时自动回退

2. 安装GPU版本

前提条件:

安装步骤:

# 创建并激活虚拟环境(可选)
python -m venv tf_gpu_env
source tf_gpu_env/bin/activate  # Linux/macOS
# 或 tf_gpu_env\Scripts\activate  # Windows

# 安装GPU版本的TensorFlow
pip install tensorflow  # 或 tensorflow-gpu(旧版本)

3. 验证安装

验证CPU版本:

以下代码, 亲测可用

import tensorflow as tf
print(tf.__version__)          # 输出版本
print(tf.config.list_physical_devices('CPU'))  # 应显示CPU设备

# 输出如下所示:
2.16.2
[PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU')]

验证GPU版本:

import tensorflow as tf
print(tf.__version__)
print(tf.config.list_physical_devices('GPU'))  # 应显示GPU设备
# 输出如下所示:
2.16.2
[]   # 这代表没有gpu资源

4. 切换环境示例

场景激活CPU环境激活GPU环境
Linux/macOSsource tf_cpu_env/bin/activatesource tf_gpu_env/bin/activate
Windowstf_cpu_env\Scripts\activatetf_gpu_env\Scripts\activate

常见问题解决

  1. GPU版本报错?

    • 检查CUDA/cuDNN版本是否匹配(参考表格)。
    • 更新NVIDIA驱动:nvidia-smi 查看驱动版本。
    • 安装CUDA依赖(Linux示例):
      sudo apt install cuda-11-8  # 根据需求调整版本
      
  2. 性能优化

    • GPU环境:确保TensorFlow检测到GPU(输出GPU device list)。
    • CPU环境:使用tensorflow-cpu减少安装体积。

关键提示

  • 不要在同一环境中混装:避免tensorflowtensorflow-cpu共存。
  • 虚拟环境隔离:使用venvconda创建独立环境。
  • GPU支持:仅限NVIDIA显卡(AMD需转译层如ROCm)。



关于 pip install tensorflow 这个命令,你需要了解以下几点:

  1. 安装的包是什么?

    • TensorFlow 2.x 开始,官方提供的 tensorflow PyPI 包是一个统一包
    • 这个包既包含 CPU 支持,也包含 GPU 支持的代码。你不需要像 TensorFlow 1.x 时代那样单独安装 tensorflow (仅 CPU) 或 tensorflow-gpu (GPU 支持)。
  2. 运行时如何选择设备?

    • 自动检测与优先使用 GPU: 当你运行 TensorFlow 程序时,它会自动检测系统中可用的计算设备(CPU 和 GPU)。
    • 默认优先 GPU: 如果 TensorFlow 检测到满足要求的 NVIDIA GPU 并且所需的 CUDA/cuDNN 驱动和库已正确安装,它会默认优先尝试将操作分配给 GPU 来执行,以获得最佳性能。
    • 自动回退到 CPU: 如果 TensorFlow 没有检测到可用的 GPU(例如,你的机器没有 NVIDIA GPU,或者有 GPU 但未安装或未正确安装 NVIDIA 驱动、CUDA Toolkit、cuDNN),它会无缝地、自动地回退到使用 CPU 来执行所有计算。你不需要做任何额外的代码修改或配置来实现这个回退。程序会继续运行,只是速度会慢很多。
  3. 关键要求(针对 GPU 使用):

    • 虽然 pip install tensorflow 安装了支持 GPU 的 TensorFlow 二进制文件,但要实际使用 GPU,你必须额外满足以下条件:
      • 兼容的 NVIDIA GPU。
      • 正确安装的 NVIDIA GPU 驱动程序。
      • 正确安装的 CUDA Toolkit (版本必须严格匹配你安装的 TensorFlow 版本的要求。可以在 TensorFlow 官网找到兼容性表)。
      • 正确安装的 cuDNN SDK (版本也必须严格匹配 CUDA 版本和 TensorFlow 版本的要求)。
    • 如果这些条件不满足,TensorFlow 会如第 2 点所述,自动回退到 CPU

总结:

  • pip install tensorflow 安装的是一个同时包含 CPU 和 GPU 支持代码的包。
  • 运行时:
    • 有 GPU + 驱动/CUDA/cuDNN 装好了 -> 自动优先用 GPU
    • 没有 GPU 或 驱动/CUDA/cuDNN 没装/没装对 -> 自动无缝回退到 CPU不需要用户干预
  • 重要: 想用上 GPU 的速度优势,仅仅 pip install tensorflow 是不够的必须额外安装正确版本的 NVIDIA 驱动、CUDA Toolkit 和 cuDNN

如何检查 TensorFlow 是否在使用 GPU?

你可以在 Python 中运行以下代码来查看 TensorFlow 检测到的设备:

import tensorflow as tf

print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))
tf.debugging.set_log_device_placement(True)

# 创建一个简单的张量进行计算,查看日志输出设备信息
a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
c = tf.matmul(a, b)
print(c)
  • tf.config.list_physical_devices('GPU') 会列出可用的 GPU 设备。
  • tf.debugging.set_log_device_placement(True) 会让 TensorFlow 在运行时输出每个操作是在哪个设备(CPU 或 GPU)上执行的(查看日志输出,通常在终端或控制台)。

如果输出显示操作在 /device:GPU:0 上执行,说明 GPU 正在被使用。如果只显示 /device:CPU:0,说明在使用 CPU。如果 list_physical_devices('GPU') 返回空列表,则说明 TensorFlow 没有检测到可用的 GPU。