透過 pip 安裝 TensorFlow

本指南適用於最新穩定版 TensorFlow。如需預覽版本 (nightly),請使用名為 tf-nightly 的 pip 套件。如需舊版 TensorFlow 版本需求,請參閱這些表格。如需僅限 CPU 的版本,請使用名為 tensorflow-cpu 的 pip 套件。

以下是安裝指令的快速版本。向下捲動以取得逐步操作說明。

Linux

python3 -m pip install tensorflow[and-cuda]
# Verify the installation:
python3 -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

MacOS

# There is currently no official GPU support for MacOS.
python3 -m pip install tensorflow
# Verify the installation:
python3 -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

Windows Native

conda install -c conda-forge cudatoolkit=11.2 cudnn=8.1.0
# Anything above 2.10 is not supported on the GPU on Windows Native
python -m pip install "tensorflow<2.11"
# Verify the installation:
python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

Windows WSL2

python3 -m pip install tensorflow[and-cuda]
# Verify the installation:
python3 -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

CPU

python3 -m pip install tensorflow
# Verify the installation:
python3 -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

Nightly

python3 -m pip install tf-nightly
# Verify the installation:
python3 -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

硬體需求

支援下列啟用 GPU 的裝置

  • NVIDIA® GPU 卡,搭載 CUDA® 架構 3.5、5.0、6.0、7.0、7.5、8.0 和更高版本。請參閱啟用 CUDA® 的 GPU 卡清單。
  • 針對不支援 CUDA® 架構的 GPU,或避免從 PTX 進行 JIT 編譯,或使用不同版本的 NVIDIA® 程式庫,請參閱 從原始碼建構 Linux 版本指南。
  • 套件不包含 PTX 程式碼,但最新支援的 CUDA® 架構除外;因此,當設定 CUDA_FORCE_PTX_JIT=1 時,TensorFlow 無法在舊版 GPU 上載入。(詳細資訊請參閱應用程式相容性。)

系統需求

  • Ubuntu 16.04 或更高版本 (64 位元)
  • macOS 10.12.6 (Sierra) 或更高版本 (64 位元) (不支援 GPU)
  • Windows Native - Windows 7 或更高版本 (64 位元) (TF 2.10 之後不支援 GPU)
  • Windows WSL2 - Windows 10 19044 或更高版本 (64 位元)

軟體需求

下列 NVIDIA® 軟體僅為 GPU 支援所必需。

逐步操作說明

Linux

1. 系統需求

  • Ubuntu 16.04 或更高版本 (64 位元)

TensorFlow 僅正式支援 Ubuntu。但是,下列說明也可能適用於其他 Linux 發行版。

2. GPU 設定

如果您只在 CPU 上執行 TensorFlow,則可以跳過此章節。

如果您尚未安裝 NVIDIA GPU 驅動程式,請安裝。您可以使用下列指令驗證是否已安裝。

nvidia-smi

3. 安裝 TensorFlow

TensorFlow 需要最新版本的 pip,因此請升級您的 pip 安裝,以確保您執行的是最新版本。

pip install --upgrade pip

然後,透過 pip 安裝 TensorFlow。

# For GPU users
pip install tensorflow[and-cuda]
# For CPU users
pip install tensorflow

4. 驗證安裝

驗證 CPU 設定

python3 -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

如果傳回張量,則表示您已成功安裝 TensorFlow。

驗證 GPU 設定

python3 -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

如果傳回 GPU 裝置清單,則表示您已成功安裝 TensorFlow。

MacOS

1. 系統需求

  • macOS 10.12.6 (Sierra) 或更高版本 (64 位元)

目前,macOS 上執行 TensorFlow 沒有官方 GPU 支援。下列說明適用於在 CPU 上執行。

2. 檢查 Python 版本

檢查您的 Python 環境是否已設定

python3 --version
python3 -m pip --version

3. 安裝 TensorFlow

TensorFlow 需要最新版本的 pip,因此請升級您的 pip 安裝,以確保您執行的是最新版本。

pip install --upgrade pip

然後,透過 pip 安裝 TensorFlow。

pip install tensorflow

4. 驗證安裝

python3 -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

如果傳回張量,則表示您已成功安裝 TensorFlow。

Windows Native

1. 系統需求

  • Windows 7 或更高版本 (64 位元)

2. 安裝 Microsoft Visual C++ Redistributable

安裝適用於 Visual Studio 2015、2017 和 2019 的 Microsoft Visual C++ Redistributable。從 TensorFlow 2.1.0 版開始,此套件需要 msvcp140_1.dll 檔案 (舊版 Redistributable 套件可能未提供)。Redistributable 隨附於 Visual Studio 2019,但可以個別安裝

  1. 前往 Microsoft Visual C++ 下載
  2. 向下捲動頁面至Visual Studio 2015、2017 和 2019章節。
  3. 下載並安裝適用於 Visual Studio 2015、2017 和 2019 的 Microsoft Visual C++ Redistributable 以適用於您的平台。

確認 Windows 上已啟用長路徑

3. 安裝 Miniconda

Miniconda 是建議用於安裝具備 GPU 支援之 TensorFlow 的方法。它會建立個別的環境,以避免變更系統中任何已安裝的軟體。這也是安裝所需軟體 (特別是 GPU 設定) 最簡單的方式。

下載 Miniconda Windows 安裝程式。按兩下已下載的檔案,並按照螢幕上的指示操作。

4. 建立 conda 環境

使用下列指令建立名為 tf 的新 conda 環境。

conda create --name tf python=3.9

您可以使用下列指令停用和啟用環境。

conda deactivate
conda activate tf

確保其已啟用以進行其餘安裝。

5. GPU 設定

如果您只在 CPU 上執行 TensorFlow,則可以跳過此章節。

首先,如果您尚未安裝 NVIDIA GPU 驅動程式,請安裝。

然後,透過 conda 安裝 CUDA、cuDNN。

conda install -c conda-forge cudatoolkit=11.2 cudnn=8.1.0

6. 安裝 TensorFlow

TensorFlow 需要最新版本的 pip,因此請升級您的 pip 安裝,以確保您執行的是最新版本。

pip install --upgrade pip

然後,透過 pip 安裝 TensorFlow。

# Anything above 2.10 is not supported on the GPU on Windows Native
pip install "tensorflow<2.11" 

7. 驗證安裝

驗證 CPU 設定

python -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

如果傳回張量,則表示您已成功安裝 TensorFlow。

驗證 GPU 設定

python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

如果傳回 GPU 裝置清單,則表示您已成功安裝 TensorFlow。

Windows WSL2

1. 系統需求

  • Windows 10 19044 或更高版本 (64 位元)。這對應於 Windows 10 21H2 版,即 2021 年 11 月更新。

請參閱下列文件以

2. GPU 設定

如果您只在 CPU 上執行 TensorFlow,則可以跳過此章節。

如果您尚未安裝 NVIDIA GPU 驅動程式,請安裝。您可以使用下列指令驗證是否已安裝。

nvidia-smi

3. 安裝 TensorFlow

TensorFlow 需要最新版本的 pip,因此請升級您的 pip 安裝,以確保您執行的是最新版本。

pip install --upgrade pip

然後,透過 pip 安裝 TensorFlow。

# For GPU users
pip install tensorflow[and-cuda]
# For CPU users
pip install tensorflow

4. 驗證安裝

驗證 CPU 設定

python3 -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

如果傳回張量,則表示您已成功安裝 TensorFlow。

驗證 GPU 設定

python3 -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

如果傳回 GPU 裝置清單,則表示您已成功安裝 TensorFlow。

套件位置

部分安裝機制需要 TensorFlow Python 套件的 URL。您指定的數值取決於您的 Python 版本。

版本網址
Linux x86
Python 3.9 GPU 支援 https://storage.googleapis.com/tensorflow/versions/2.16.1/tensorflow-2.16.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Python 3.9 僅限 CPU https://storage.googleapis.com/tensorflow/versions/2.16.1/tensorflow_cpu-2.16.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Python 3.10 GPU 支援 https://storage.googleapis.com/tensorflow/versions/2.16.1/tensorflow-2.16.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Python 3.10 僅限 CPU https://storage.googleapis.com/tensorflow/versions/2.16.1/tensorflow_cpu-2.16.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Python 3.11 GPU 支援 https://storage.googleapis.com/tensorflow/versions/2.16.1/tensorflow-2.16.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Python 3.11 僅限 CPU https://storage.googleapis.com/tensorflow/versions/2.16.1/tensorflow_cpu-2.16.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Python 3.12 GPU 支援 https://storage.googleapis.com/tensorflow/versions/2.16.1/tensorflow-2.16.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Python 3.12 僅限 CPU https://storage.googleapis.com/tensorflow/versions/2.16.1/tensorflow_cpu-2.16.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Linux Arm64 (僅限 CPU)
Python 3.9 https://storage.googleapis.com/tensorflow/versions/2.16.1/tensorflow-2.16.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Python 3.10 https://storage.googleapis.com/tensorflow/versions/2.16.1/tensorflow-2.16.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Python 3.11 https://storage.googleapis.com/tensorflow/versions/2.16.1/tensorflow-2.16.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Python 3.12 https://storage.googleapis.com/tensorflow/versions/2.16.1/tensorflow-2.16.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
macOS x86 (僅限 CPU)
Python 3.9 https://storage.googleapis.com/tensorflow/versions/2.16.1/tensorflow-2.16.1-cp39-cp39-macosx_10_15_x86_64.whl
Python 3.10 https://storage.googleapis.com/tensorflow/versions/2.16.1/tensorflow-2.16.1-cp310-cp310-macosx_10_15_x86_64.whl
Python 3.11 https://storage.googleapis.com/tensorflow/versions/2.16.1/tensorflow-2.16.1-cp311-cp311-macosx_10_15_x86_64.whl
Python 3.12 https://storage.googleapis.com/tensorflow/versions/2.16.1/tensorflow-2.16.1-cp312-cp312-macosx_10_15_x86_64.whl
macOS Arm64 (僅限 CPU)
Python 3.9 https://storage.googleapis.com/tensorflow/versions/2.16.1/tensorflow-2.16.1-cp39-cp39-macosx_12_0_arm64.whl
Python 3.10 https://storage.googleapis.com/tensorflow/versions/2.16.1/tensorflow-2.16.1-cp310-cp310-macosx_12_0_arm64.whl
Python 3.11 https://storage.googleapis.com/tensorflow/versions/2.16.1/tensorflow-2.16.1-cp311-cp311-macosx_12_0_arm64.whl
Python 3.12 https://storage.googleapis.com/tensorflow/versions/2.16.1/tensorflow-2.16.1-cp312-cp312-macosx_12_0_arm64.whl
Windows (僅限 CPU)
Python 3.9 https://storage.googleapis.com/tensorflow/versions/2.16.1/tensorflow-2.16.1-cp39-cp39-win_amd64.whl
Python 3.10 https://storage.googleapis.com/tensorflow/versions/2.16.1/tensorflow-2.16.1-cp310-cp310-win_amd64.whl
Python 3.11 https://storage.googleapis.com/tensorflow/versions/2.16.1/tensorflow-2.16.1-cp311-cp311-win_amd64.whl
Python 3.12 https://storage.googleapis.com/tensorflow/versions/2.16.1/tensorflow-2.16.1-cp312-cp312-win_amd64.whl