跳转至

TensorFlow 安装

在这篇文章中

TensorFlow 是由 Google 开发的用于机器学习和深度神经网络训练的开源平台。借助 TensorFlow,您可以使用 Python 中用户友好的 API 来创建、训练和利用复杂的神经网络。

该平台既支持传统的机器学习算法(如线性回归和逻辑回归),也支持更高级的架构,包括卷积神经网络(Convolutional Neural Networks, CNN)和循环神经网络(Recurrent Neural Networks, RNN)。

信息

如果您不想手动安装 TensorFlow,请从 marketplace 订购带有现成镜像的服务器——其中已安装 NVIDIA 驱动程序和 TensorFlow。详情请参阅 TensorFlow 一文。

Linux 上的 TensorFlow 安装

本指南适用于 Ubuntu 24.04 (Python 3.12) 和 Ubuntu 22.04 (Python 3.10)。两个版本的命令相同,仅默认的系统 Python 版本不同。

备注

如果您计划使用 GPU 加速,请根据 此指南 安装 NVIDIA 驱动程序。无需单独安装 CUDA Toolkit 和 cuDNN——安装 tensorflow[and-cuda] 软件包时,所需的 CUDA 和 cuDNN 库会自动安装到虚拟环境中。

  1. 安装 Python、虚拟环境模块和 pip:

    sudo apt update
    sudo apt install python3 python3-venv python3-pip
    

    请使用系统默认安装的 Python 版本(Ubuntu 24.04 中为 3.12,Ubuntu 22.04 中为 3.10)。在安装其他版本之前,请在 官方网站 上检查 TensorFlow 是否支持该版本。

    备注

    从 Ubuntu 24.04 和 Debian 12 开始,不允许使用 pip 将软件包安装到系统 Python 中(会出现 externally-managed-environment 错误)。因此,TensorFlow 需要安装在虚拟环境中。

  2. 创建 Python 虚拟环境:

    python3 -m venv venv
    
  3. 激活虚拟环境:

    source venv/bin/activate
    

    成功激活后,提示符中将包含括号内的虚拟环境名称:

    (venv) user@49069:~$
    

    备注

    您可以创建任意数量的虚拟环境并安装不同的库(包括同时安装,但有时这可能会导致冲突)。

  4. 升级 pip 并安装带有 GPU 支持的 TensorFlow 库:

    pip install --upgrade pip
    pip install 'tensorflow[and-cuda]'
    

    如果您不使用 GPU,只需安装不带附加依赖项的软件包即可:pip install tensorflow。

    备注

    以前,在 GPU 上使用 TensorFlow 需要手动安装 TensorRT,并将 CUDA 和 cuDNN 库的路径添加到 LD_LIBRARY_PATH 变量中。当前的 TensorFlow 版本不再需要这样做,并且从 2.18 版本开始,标准 TensorFlow 构建已移除对 TensorRT 的支持。

  5. 退出虚拟环境:

    deactivate
    

运行 TensorFlow

要使用 TensorFlow,只需从主目录的根目录激活虚拟环境:

source venv/bin/activate

之后,您可以启动 Python 控制台或运行您自己的脚本。

验证 TensorFlow 安装

为了验证库的功能和 GPU 支持,请在控制台中启动以下 Python 程序:

import tensorflow as tf
print(tf.reduce_sum(tf.random.normal([1000, 1000])))
print(tf.config.list_physical_devices('GPU'))

安装成功后,您将收到类似以下的输出,显示您的 GPU 使用情况(示例是在 Ubuntu 22.04 和 Python 3.10 上获得的;您的版本、服务消息和 GPU 型号会有所不同):

(venv) user1@49069:~$ python
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf

2024-04-22 23:39:54.472502: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
>>> print(tf.reduce_sum(tf.random.normal([1000, 1000])))

2024-04-22 23:39:55.810888: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1928] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 22282 MB memory:  -> device: 0, name: NVIDIA GeForce RTX 4090, pci bus id: 0000:07:00.0, compute capability: 8.9
tf.Tensor(332.5041, shape=(), dtype=float32)
>>> print(tf.config.list_physical_devices('GPU'))

[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
>>>

此外,您可以从 此 GitHub 存储库 下载并运行用于训练神经网络的测试 Python 脚本 2c_nn_mnist_customtrain.py。

执行完成后,您应该收到类似以下的输出(TensorFlow 服务消息取决于版本):

(venv) user1@49069:~$ python ./2c_nn_mnist_customtrain.py
2024-04-23 10:35:25.832754: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2024-04-23 10:35:28.404381: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1928] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 22282 MB memory:  -> device: 0, name: NVIDIA GeForce RTX 4090, pci bus id: 0000:07:00.0, compute capability: 8.9
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
I0000 00:00:1713861338.172126   21279 service.cc:145] XLA service 0x7f36a3bd2ec0 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:
I0000 00:00:1713861338.172187   21279 service.cc:153]   StreamExecutor device (0): NVIDIA GeForce RTX 4090, Compute Capability 8.9
2024-04-23 10:35:38.180648: I tensorflow/compiler/mlir/tensorflow/utils/dump_mlir_util.cc:268] disabling MLIR crash reproducer, set env var `MLIR_CRASH_REPRODUCER_DIRECTORY` to enable.
2024-04-23 10:35:38.207205: I external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:465] Loaded cuDNN version 8902
I0000 00:00:1713861338.355523   21279 device_compiler.h:188] Compiled cluster using XLA!  This line is logged at most once for the lifetime of the process.
1874/1875 |||||||||||||||||||||||||||||||| acc: 0.9022 time: 14.9 test-acc: 0.932 (error: 6.75%)
1874/1875 |||||||||||||||||||||||||||||||| acc: 0.9474 time: 4.74 test-acc: 0.953 (error: 4.71%)
1874/1875 |||||||||||||||||||||||||||||||| acc: 0.9628 time: 4.83 test-acc: 0.963 (error: 3.67%)
1874/1875 |||||||||||||||||||||||||||||||| acc: 0.9734 time: 4.86 test-acc: 0.968 (error: 3.25%)
1874/1875 |||||||||||||||||||||||||||||||| acc: 0.9798 time: 4.9 test-acc: 0.97 (error: 3.04%)