安装与配置 #
系统要求 #
支持的操作系统 #
| 操作系统 | 版本要求 |
|---|---|
| Ubuntu | 18.04 / 20.04 / 22.04 |
| Windows | Windows 10 / 11 (64-bit) |
| macOS | 10.12.6 (Sierra) 或更高 |
| CentOS | 7 / 8 |
| RHEL | 7 / 8 |
Python 版本要求 #
text
┌─────────────────────────────────────────────────────────────┐
│ Python 版本支持 │
├─────────────────────────────────────────────────────────────┤
│ │
│ TensorFlow 2.15+ ────► Python 3.9 - 3.12 │
│ TensorFlow 2.12+ ────► Python 3.8 - 3.11 │
│ TensorFlow 2.5+ ────► Python 3.6 - 3.9 │
│ │
│ 推荐:Python 3.10 或 3.11 │
│ │
└─────────────────────────────────────────────────────────────┘
安装方式 #
1. pip 安装(推荐) #
bash
# 创建虚拟环境(推荐)
python -m venv tf-env
source tf-env/bin/activate # Linux/macOS
# tf-env\Scripts\activate # Windows
# 安装最新稳定版
pip install tensorflow
# 安装指定版本
pip install tensorflow==2.15.0
# 安装 CPU 版本(体积更小)
pip install tensorflow-cpu
2. conda 安装 #
bash
# 创建 conda 环境
conda create -n tf-env python=3.10
conda activate tf-env
# 安装 TensorFlow
conda install -c conda-forge tensorflow
# 或使用 pip
pip install tensorflow
3. Docker 安装 #
bash
# CPU 版本
docker pull tensorflow/tensorflow:latest
# GPU 版本
docker pull tensorflow/tensorflow:latest-gpu
# 运行容器
docker run -it -p 8888:8888 tensorflow/tensorflow:latest
GPU 支持 #
CUDA 和 cuDNN 版本匹配 #
text
┌─────────────────────────────────────────────────────────────┐
│ TensorFlow GPU 版本要求 │
├─────────────────────────────────────────────────────────────┤
│ │
│ TensorFlow 2.15 ────► CUDA 12.2, cuDNN 8.9 │
│ TensorFlow 2.12 ────► CUDA 11.8, cuDNN 8.6 │
│ TensorFlow 2.10 ────► CUDA 11.2, cuDNN 8.1 │
│ TensorFlow 2.6 ────► CUDA 11.2, cuDNN 8.1 │
│ │
│ 注意:版本必须严格匹配,否则无法使用 GPU │
│ │
└─────────────────────────────────────────────────────────────┘
安装 CUDA Toolkit #
bash
# Ubuntu 安装 CUDA 12.2
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.0-1_all.deb
sudo dpkg -i cuda-keyring_1.0-1_all.deb
sudo apt update
sudo apt install cuda-12-2
# 添加环境变量
echo 'export PATH=/usr/local/cuda/bin:$PATH' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH' >> ~/.bashrc
source ~/.bashrc
安装 cuDNN #
bash
# 下载 cuDNN(需要 NVIDIA 账号)
# https://developer.nvidia.com/cudnn
# 解压并安装
tar -xvf cudnn-linux-x86_64-8.9.x.x_cudaX.Y-archive.tar.xz
sudo cp cudnn-linux-x86_64-8.9.x.x_cudaX.Y-archive/include/* /usr/local/cuda/include/
sudo cp cudnn-linux-x86_64-8.9.x.x_cudaX.Y-archive/lib64/* /usr/local/cuda/lib64/
验证 GPU 安装 #
python
import tensorflow as tf
# 检查 GPU 是否可用
print("GPU 可用:", tf.config.list_physical_devices('GPU'))
# 详细信息
print("构建信息:", tf.sysconfig.get_build_info())
# 测试 GPU 计算
with tf.device('/GPU:0'):
a = tf.constant([[1.0, 2.0], [3.0, 4.0]])
b = tf.constant([[5.0, 6.0], [7.0, 8.0]])
c = tf.matmul(a, b)
print("GPU 计算结果:", c)
macOS GPU 支持 #
Apple Silicon (M1/M2/M3) #
bash
# 安装 Miniforge
brew install miniforge
# 创建环境
conda create -n tf-env python=3.10
conda activate tf-env
# 安装 Apple TensorFlow 插件
conda install -c apple tensorflow-deps
# 安装 TensorFlow
pip install tensorflow-macos
pip install tensorflow-metal # GPU 加速
验证 Metal GPU #
python
import tensorflow as tf
# 检查 Metal 设备
print("物理设备:", tf.config.list_physical_devices())
# Metal GPU 应该显示为 '/device:GPU:0'
配置优化 #
GPU 内存管理 #
python
import tensorflow as tf
# 方式1: 按需分配内存
gpus = tf.config.list_physical_devices('GPU')
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
# 方式2: 限制 GPU 内存使用
for gpu in gpus:
tf.config.set_logical_device_configuration(
gpu,
[tf.config.LogicalDeviceConfiguration(memory_limit=4096)]
)
# 方式3: 使用环境变量
import os
os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true'
多 GPU 配置 #
python
import tensorflow as tf
# 查看所有 GPU
gpus = tf.config.list_physical_devices('GPU')
print(f"发现 {len(gpus)} 个 GPU")
# 设置可见 GPU
tf.config.set_visible_devices(gpus[0], 'GPU') # 只使用第一个 GPU
# 虚拟 GPU(将一个物理 GPU 分成多个)
tf.config.set_logical_device_configuration(
gpus[0],
[tf.config.LogicalDeviceConfiguration(memory_limit=2048),
tf.config.LogicalDeviceConfiguration(memory_limit=2048)]
)
混合精度训练 #
python
import tensorflow as tf
# 启用混合精度
policy = tf.keras.mixed_precision.Policy('mixed_float16')
tf.keras.mixed_precision.set_global_policy(policy)
# 查看当前策略
print('计算策略:', policy.name)
print('变量数据类型:', policy.variable_dtype)
# 模型输出层需要使用 float32
outputs = tf.keras.layers.Dense(10, dtype='float32')(x)
常见问题 #
1. CUDA 版本不匹配 #
python
# 错误信息
# Could not load dynamic library 'libcudart.so.11.0'
# 解决方案
# 检查 CUDA 版本
!nvcc --version
# 安装正确版本的 CUDA
# 或使用 pip 安装匹配的 TensorFlow 版本
2. cuDNN 找不到 #
bash
# 错误信息
# Could not load dynamic library 'libcudnn.so.8'
# 解决方案
# 检查 cuDNN 安装
ldconfig -p | grep cudnn
# 设置库路径
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
3. 内存不足 #
python
# 解决方案:减少 batch size 或启用内存增长
import tensorflow as tf
gpus = tf.config.list_physical_devices('GPU')
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
4. SSL 证书问题 #
bash
# pip 安装时 SSL 错误
pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org tensorflow
验证安装 #
完整验证脚本 #
python
import tensorflow as tf
import sys
print("=" * 50)
print("TensorFlow 安装验证")
print("=" * 50)
# 版本信息
print(f"TensorFlow 版本: {tf.__version__}")
print(f"Python 版本: {sys.version}")
print(f"Keras 版本: {tf.keras.__version__}")
# GPU 信息
gpus = tf.config.list_physical_devices('GPU')
print(f"可用 GPU 数量: {len(gpus)}")
for i, gpu in enumerate(gpus):
print(f" GPU {i}: {gpu}")
# 构建信息
build_info = tf.sysconfig.get_build_info()
print(f"CUDA 版本: {build_info.get('cuda_version', 'N/A')}")
print(f"cuDNN 版本: {build_info.get('cudnn_version', 'N/A')}")
# 简单计算测试
print("\n计算测试:")
x = tf.constant([[1.0, 2.0], [3.0, 4.0]])
y = tf.matmul(x, x)
print(f"矩阵乘法结果:\n{y.numpy()}")
print("\n✅ TensorFlow 安装成功!")
开发环境推荐 #
Jupyter Notebook #
bash
# 安装 Jupyter
pip install jupyter
# 安装扩展
pip install jupyterlab
# 启动
jupyter lab
Google Colab #
text
Google Colab 提供免费的 GPU 环境:
1. 访问 https://colab.research.google.com
2. 运行时 → 更改运行时类型 → GPU
3. 预装 TensorFlow,无需额外安装
验证 GPU:
!nvidia-smi
VS Code 配置 #
json
// settings.json
{
"python.linting.enabled": true,
"python.linting.pylintEnabled": true,
"python.formatting.provider": "black",
"jupyter.askForKernelRestart": false
}
下一步 #
现在你已经完成了 TensorFlow 的安装配置,接下来学习 张量基础,了解 TensorFlow 的核心数据结构!
最后更新:2026-04-04