pip を使用して TensorFlow をインストールする

このガイドは、TensorFlow の最新の安定バージョンを対象としています。プレビュー ビルド(夜間)の場合は、 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'))"

マックOS

# 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ネイティブ

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])))"

毎晩

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 対応デバイスがサポートされています。

  • CUDA® アーキテクチャ 3.5、5.0、6.0、7.0、7.5、8.0 以降を搭載した NVIDIA® GPU カード。 CUDA® 対応 GPU カードのリストを参照してください。
  • サポートされていない CUDA® アーキテクチャを備えた GPU の場合、PTX からの JIT コンパイルを回避する場合、または異なるバージョンの NVIDIA® ライブラリを使用する場合は、ソース ガイドからの Linux ビルドを参照してください。
  • パッケージには、サポートされている最新の CUDA® アーキテクチャを除き、PTX コードは含まれていません。したがって、 CUDA_FORCE_PTX_JIT=1が設定されている場合、TensorFlow は古い GPU でのロードに失敗します。 (詳細については、 「アプリケーションの互換性」を参照してください。)

システム要求

  • Ubuntu 16.04 以降 (64 ビット)
  • macOS 10.12.6 (Sierra) 以降 (64 ビット) (GPU サポートなし)
  • Windows ネイティブ - 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のセットアップ

TensorFlow を CPU 上でのみ実行する場合は、このセクションをスキップできます。

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])))"

tensor が返された場合、TensorFlow は正常にインストールされています。

GPU の設定を確認します。

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

GPU デバイスのリストが返された場合、TensorFlow は正常にインストールされています。

マックOS

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])))"

tensor が返された場合、TensorFlow は正常にインストールされています。

Windowsネイティブ

1. システム要件

  • Windows 7以降(64ビット)

2. Microsoft Visual C++ 再頒布可能パッケージをインストールします。

Visual Studio 2015、2017、および 2019 の Microsoft Visual C++ 再頒布可能パッケージをインストールします。 TensorFlow 2.1.0 バージョン以降、このパッケージにはmsvcp140_1.dllファイルが必要になります (古い再頒布可能パッケージでは提供されない場合があります)。再頒布可能ファイルはVisual Studio 2019に付属していますが、個別にインストールすることもできます。

  1. Microsoft Visual C++ ダウンロードに移動します。
  2. ページを下にスクロールして、「Visual Studio 2015、2017、および 2019」セクションを表示します。
  3. ご使用のプラットフォームに合わせて、Visual Studio 2015、2017、および 2019 用の Microsoft Visual C++ 再頒布可能パッケージをダウンロードしてインストールします。

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のセットアップ

TensorFlow を CPU 上でのみ実行する場合は、このセクションをスキップできます。

まだNVIDIA GPU ドライバーをインストールしていない場合は、まずインストールします。

次に、CUDA、cuDNN を conda でインストールします。

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])))"

tensor が返された場合、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のセットアップ

TensorFlow を CPU 上でのみ実行する場合は、このセクションをスキップできます。

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])))"

tensor が返された場合、TensorFlow は正常にインストールされています。

GPU の設定を確認します。

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

GPU デバイスのリストが返された場合、TensorFlow は正常にインストールされています。

パッケージの場所

いくつかのインストール メカニズムでは、TensorFlow Python パッケージの URL が必要です。指定する値は、Python のバージョンによって異なります。

バージョンURL
Linux
Python 3.9 GPUのサポートhttps://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-2.15.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Python 3.9 CPU のみhttps://storage.googleapis.com/tensorflow/linux/cpu/tensorflow_cpu-2.15.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Python 3.10 GPU のサポートhttps://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-2.15.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Python 3.10 CPU のみhttps://storage.googleapis.com/tensorflow/linux/cpu/tensorflow_cpu-2.15.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Python 3.11 GPU のサポートhttps://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-2.15.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Python 3.11 CPU のみhttps://storage.googleapis.com/tensorflow/linux/cpu/tensorflow_cpu-2.15.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
macOS (CPU のみ)
Python 3.9 https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-2.15.0-cp39-cp39-macosx_10_15_x86_64.whl
Python 3.10 https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-2.15.0-cp310-cp310-macosx_10_15_x86_64.whl
Python 3.11 https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-2.15.0-cp311-cp311-macosx_10_15_x86_64.whl
ウィンドウズ
Python 3.9 CPU のみhttps://storage.googleapis.com/tensorflow/windows/cpu/tensorflow_cpu-2.15.0-cp39-cp39-win_amd64.whl
Python 3.10 CPU のみhttps://storage.googleapis.com/tensorflow/windows/cpu/tensorflow_cpu-2.15.0-cp310-cp310-win_amd64.whl
Python 3.11 CPU のみhttps://storage.googleapis.com/tensorflow/windows/cpu/tensorflow_cpu-2.15.0-cp311-cp311-win_amd64.whl