Python 深度学习环境配置指南:PyTorch 安装、源配置、GPU 检查及 matplotlib 使用
前言
在 Python 深度学习开发中,常会遇到包下载慢、PyTorch 安装报错、GPU 是否可用验证、可视化库安装等基础问题。本文汇总了实际操作中高频问题的解决方案,涵盖 pip 源配置、PyTorch 安装(含清华源适配)、GPU 可用性检查、matplotlib(plt)安装与使用,适用于新手快速搭建开发环境。
本文介绍 Python 深度学习环境配置,包括 pip 清华源配置加速下载、PyTorch 安装(含 CUDA 版本适配与报错处理)、GPU 可用性验证方法(PyTorch/TensorFlow)以及 matplotlib 可视化库的安装与基础绘图示例。涵盖常见问题汇总,帮助新手快速搭建开发环境并排查 GPU 识别失败等问题。
在 Python 深度学习开发中,常会遇到包下载慢、PyTorch 安装报错、GPU 是否可用验证、可视化库安装等基础问题。本文汇总了实际操作中高频问题的解决方案,涵盖 pip 源配置、PyTorch 安装(含清华源适配)、GPU 可用性检查、matplotlib(plt)安装与使用,适用于新手快速搭建开发环境。
国内使用默认 pip 源(国外服务器)下载包时速度较慢,将默认源改为清华源可显著提升下载效率,支持永久配置和临时使用两种方式。
mkdir -p ~/.config/pip
nano ~/.config/pip/pip.conf
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple # 清华源地址
[install]
trusted-host = pypi.tuna.tsinghua.edu.cn # 信任该源(避免 SSL 验证报错)
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host = pypi.tuna.tsinghua.edu.cn
若无需永久修改,可在每次 pip install 时通过-i 参数指定清华源,示例:
pip install 包名 -i https://pypi.tuna.tsinghua.edu.cn/simple
安装任意包(如 numpy),查看终端输出,若显示以下内容则源配置生效:
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
PyTorch 安装需匹配CUDA 版本(GPU 用户)或选择 CPU 版本,常见问题为'找不到兼容版本',以下是完整解决方案。
原官方安装命令(指定 CUDA 12.8):
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu128
清华源主要加速 PyPI 通用包,PyTorch 预编译包(含 CUDA 版本)仍需结合官方 whl 源,命令如下:
# 格式:--extra-index-url 指定官方 CUDA 源,-i 指定清华源(加速其他依赖)
pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu121 -i https://pypi.tuna.tsinghua.edu.cn/simple
注意:需将 cu121 替换为你的显卡支持的 CUDA 版本(如 cu118、cu124),不可直接使用不存在的 cu128(当前 PyTorch 暂不支持)。
python --version # 若版本不在 3.8~3.11,需升级/降级 Python(推荐用 conda 创建虚拟环境)
nvidia-smi # NVIDIA 显卡命令(AMD 用 rocm-smi)
终端输出中'CUDA Version'即为显卡支持的最高 CUDA 版本(如 12.1),需选择不高于该版本的 PyTorch CUDA 版本(如 cu121)。
pip3 install --upgrade pip
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
import torch
print(torch.cuda.is_available()) # True=GPU 可用,False=仅 CPU
print(torch.version.cuda) # 输出 CUDA 版本(如 12.1)
print(torch.cuda.get_device_name(0)) # 输出 GPU 型号(如 NVIDIA GeForce RTX 4090)
GPU 是深度学习训练的核心加速硬件,需确认框架(如 PyTorch、TensorFlow)是否识别到 GPU。
import torch
# 1. 检查 GPU 是否可用
print("GPU 是否可用:", torch.cuda.is_available()) # 返回 True/False
# 2. 查看可用 GPU 数量
print("可用 GPU 数量:", torch.cuda.device_count())
# 3. 查看当前使用的 GPU 编号(默认 0)
print("当前 GPU 编号:", torch.cuda.current_device())
# 4. 查看 GPU 型号
print("GPU 型号:", torch.cuda.get_device_name(0)) # 0 为 GPU 编号,多 GPU 可改 1、2 等
# 5. 验证 GPU 能否创建张量(避免'驱动不兼容'等隐性问题)
if torch.cuda.is_available():
x = torch.tensor([1.0, 2.0], device="cuda") # 在 GPU 上创建张量
print("GPU 张量:", x)
else:
print("无可用 GPU,使用 CPU")
import tensorflow as tf
# 1. 查看 GPU 设备列表
print("GPU 设备列表:", tf.config.list_physical_devices("GPU"))
# 2. 旧版本兼容(TensorFlow 2.x 中 tf.test.is_gpu_available() 已弃用,建议用上面的命令)
# print("GPU 是否可用(旧 API):", tf.test.is_gpu_available())
plt 是 matplotlib.pyplot 的常用别名,用于绘制折线图、柱状图等,是数据分析和深度学习结果展示的核心工具。
pip install matplotlib # 已配置清华源则自动从清华源下载
import matplotlib.pyplot as plt
# 1. 准备数据
x = [1, 2, 3, 4, 5] # x 轴数据
y = [1, 4, 9, 16, 25] # y 轴数据(x 的平方)
# 2. 绘制折线图
plt.plot(x, y, color="red", linewidth=2, label="y = x²") # 设置颜色、线宽、标签
# 3. 添加图表元素
plt.title("Square Function Plot") # 标题
plt.xlabel("X Axis") # x 轴标签
plt.ylabel("Y Axis") # y 轴标签
plt.legend() # 显示图例
plt.grid(alpha=0.3) # 显示网格(透明度 0.3)
# 4. 显示图像
plt.show()
需额外添加魔法命令(确保图像内嵌显示):
%matplotlib inline # 魔法命令,仅 Jupyter 需添加
import matplotlib.pyplot as plt
# 后续代码与普通脚本一致
x = [1, 2, 3, 4]
y = [2, 4, 6, 8]
plt.plot(x, y)
plt.show()
| 问题描述 | 解决方案 |
|---|---|
| pip 下载包慢 | 按'第一章'配置清华源 |
| PyTorch 安装报'找不到版本' | 1. 检查 Python 版本(3.8~3.11);2. 确认 CUDA 版本存在;3. 升级 pip |
| GPU 显示不可用(torch.cuda.is_available ()=False) | 1. 检查显卡驱动是否安装;2. 确认 PyTorch 是 GPU 版本;3. 验证 CUDA 版本兼容 |
| matplotlib 绘图不显示 | 1. 普通脚本加 plt.show();2. Jupyter 加%matplotlib inline |
本文覆盖了 Python 深度学习环境搭建的核心操作:从 pip 源配置解决下载效率问题,到 PyTorch 安装的版本适配与报错处理,再到 GPU 可用性验证和 matplotlib 可视化工具的使用,形成了完整的'环境配置→工具安装→问题排查'流程。若遇到其他问题,可根据'常见问题汇总'定位原因,或补充系统版本、Python 版本、显卡型号等信息进一步排查。

微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online
生成新的随机RSA私钥和公钥pem证书。 在线工具,RSA密钥对生成器在线工具,online
基于 Mermaid.js 实时预览流程图、时序图等图表,支持源码编辑与即时渲染。 在线工具,Mermaid 预览与可视化编辑在线工具,online
解析常见 curl 参数并生成 fetch、axios、PHP curl 或 Python requests 示例代码。 在线工具,curl 转代码在线工具,online
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online