安装 tensorflow GPU(使用 conda)

本文最后更新于 2024年4月26日 凌晨

环境:windows 11

1. 快速开始

运行以下命令,直接新建一个包含 tensorflow (gpu) 以及其他常用库的新环境。

1
2
3
4
# 采用下方「方法二」创建一个包含 tensorflow 与常用包的环境
conda create -n tensorflow-38 python=3.8.13 matplotlib scikit-learn ipykernel pandas seaborn tensorflow-gpu -c conda-forge -c anaconda
conda activate tensorflow-38
# pip install opencv-python

2. 「方法一」半自动安装

  • 先安装 cudnn 和 cudatoolkit 包,再安装 tensorflow 包
1
2
3
4
5
6
conda create -n tensorflow-38-1 python=3.8.13
conda activate tensorflow-38-1

# 会自动安装 cudatoolkit
conda install -c conda-forge cudnn
pip install tensorflow

3. 「方法二」全自动安装

  • 缺点是版本相对 pypi 稍低
  • 如:目前 pypi 版本为 2.8,conda 的版本为 2.6
  • conda install tensorflow-gpupip install tensorflow-gpu 安装的内容是不同的
1
2
3
4
5
conda create -n tensorflow-38-2 python=3.8.13
conda activate tensorflow-38-2

# conda-forge channel 的 tensorflow-gpu 仅支持 Linux
conda install tensorflow-gpu -c anaconda

4. 验证

1
2
3
4
5
6
7
8
9
10
11
12
import tensorflow as tf
# 查看 tensorflow 版本
print(tf.__version__)
# 判断是否使用 GPU 构建
print(tf.test.is_built_with_cuda())
# 查看GPU列表
print(tf.config.list_physical_devices('GPU'))
# 查看驱动名称
if tf.test.gpu_device_name():
print('Default GPU Device: {}'.format(tf.test.gpu_device_name()))
else:
print("Please install GPU version of TF")

5. 提示

在后期修改环境的时候,留意不要把 tensorflow-gpu 从 gpu 版本换成 cpu 版本,如下图所示:

tensorflow 降级

6. 附


安装 tensorflow GPU(使用 conda)
https://blog.cc01cc.cn/2022/12/06/use-conda-tensorflow/
作者
零一/cc01cc(zeo)
发布于
2022年12月6日
更新于
2024年4月26日
许可协议