安装教程总结:在 Windows 上配置 TensorFlow-GPU 环境
环境
- 操作系统: Windows 10 (版本 10.0.19045.5487)
- Python 版本: 3.8.20 (Anaconda)
- GPU: NVIDIA GeForce MX250
- 目标: 配置 TensorFlow-GPU 2.3.0,支持 CUDA 和 cuDNN。
步骤
-
初始化 Conda 环境
- 运行
conda init
,初始化 Conda 环境,未修改现有配置。 - 激活已有环境:
conda activate tsinghua
。
- 运行
-
检查 Python 版本
- 命令:
python -V
- 输出:
Python 3.8.20
,确认环境正确。
- 命令:
-
安装 CUDA Toolkit
- 查询可用版本:
conda search cudatoolkit
- 安装特定版本:
conda install cudatoolkit=10.1.243
- 下载大小:约 300.3 MB
- 查询可用版本:
-
安装 cuDNN
- 查询可用版本:
conda search cudnn
- 安装特定版本:
conda install cudnn=7.6.5
- 下载大小:约 179.1 MB
- 兼容 CUDA 10.1,安装成功。
- 查询可用版本:
-
安装 TensorFlow-GPU
- 使用清华镜像加速:
pip install tensorflow-gpu==2.3.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
- 初始安装因 SSL 错误失败,重试后成功。
- 下载并安装依赖包(如
numpy<1.19.0
,protobuf
,h5py
等),总大小约 344.2 MB。
- 使用清华镜像加速:
-
解决 Protobuf 版本冲突
- 运行
python main.py
时,报错提示protobuf
版本过高(5.29.3)。 - 卸载现有版本:
pip uninstall protobuf -y
- 安装兼容版本:
pip install protobuf==3.20.3 -i https://pypi.tuna.tsinghua.edu.cn/simple
- 运行
-
验证 TensorFlow-GPU 配置
- 启动 Python 解释器:
python
- 检查 TensorFlow:
import tensorflow as tf print("TensorFlow version:", tf.__version__) # 输出: 2.3.0 print("GPU available:", tf.test.is_gpu_available()) # 输出: True print("GPU device:", tf.test.gpu_device_name()) # 输出: /device:GPU:0
- 确认 GPU(NVIDIA GeForce MX250)可用,内存 1342 MB。
- 启动 Python 解释器:
-
运行测试脚本
- 切换目录:
cd C:\Users\xiaoming\Downloads\python_cpu_ceshi
- 运行
cputest.py
,测试 GPU 矩阵乘法:- 输出:
Matrix multiplication on GPU took: ~0.52 seconds
- 结果形状:
(1000, 1000)
- 输出:
- 确认 GPU 加速正常工作。
- 切换目录:
注意事项
- 依赖版本匹配:
- TensorFlow-GPU 2.3.0 需要 CUDA 10.1 和 cuDNN 7.6.5。
- Protobuf 需降级至 3.20.3 以避免兼容性问题。
- 网络问题:若遇到 SSL 错误,可多次尝试或更换镜像源。
- 测试脚本:
cputest.py
中若调用tf.test.gpu_device_name(i)
会报错,因该函数不接受参数,应直接使用tf.test.gpu_device_name()
。
最终环境
- Python 3.8.20
- TensorFlow-GPU 2.3.0
- CUDA Toolkit 10.1.243
- cuDNN 7.6.5
- Protobuf 3.20.3
- GPU 支持正常运行。