install tensorflow_gpu & pytorch for python 3.10 to windows11 & NVIDIA GeForce RTX3060(GDDR6 12GB)
cuda, cuDNN や tensorflow_gpu , pytorch のインストールは、 各バージョンの整合性が難しく、また、よく忘れので、メモ
目次
- 参考url
- インストール先の環境
- 事前準備 - Visual Studio Community , Git for win
- CUDA 11.2
- cuDNN 8.9.2 for CUDA 11
- 環境変数の設定
- conda create & conda activate
- install tensorflow_gpu & pytorch
- pythonがGPUを認識していることの確認
参考url
インストール先の環境
| 項目 | バージョン |
|---|---|
| OS | Windows 11 Pro |
| GPU | NVIDIA GeForce RTX3060(GDDR6 12GB) |
| Python | Miniconda |
事前準備 - Visual Studio Community , Git for win
Visual Studio Community
既に Visual Studio Build Tools 2022や Visual Studio Community 2019 を インストール済でしたので、今回用にはインストールしていません

Git for win
Git for win も既にインストール済でしたので、 今回用にはインストールしていません
DOS> git --version git version 2.37.0.windows.1
CUDA 11.2
https://www.tensorflow.org/install/source_windows?hl=ja

上記のtensorflowのドキュメントによれば、 tensorflow_gpu for win の最新は、CUDA 11.2 のようですので、 https://developer.nvidia.com/cuda-toolkit-archive より CUDA Toolkit 11.2 ( cuda_11.2.2_461.33_win10.exe )を ダウンロード & インストールします。

インストール後、以下のようにバージョン情報が表示されれば、OKです。
DOS> C:\Users\end0t>nvcc -V nvcc: NVIDIA (R) Cuda compiler driver Copyright (c) 2005-2021 NVIDIA Corporation Built on Sun_Feb_14_22:08:44_Pacific_Standard_Time_2021 Cuda compilation tools, release 11.2, V11.2.152 Build cuda_11.2.r11.2/compiler.29618528_0
cuDNN 8.9.2 for CUDA 11
先程、インストールしたCUDA 11は cuDNN 8.9.2 のようですので、 https://developer.nvidia.com/rdp/cudnn-download より Download cuDNN v8.9.2 (June 1st, 2023), for CUDA 11.x ( cudnn-windows-x86_64-8.9.2.26_cuda11-archive.zip )をダウンロードします。
次にダウンロードしたzipを解凍し、CUDAのインストールdirにコピーします。
DOS> unzip cudnn-windows-x86_64-8.9.2.26_cuda11-archive.zip
DOS> cp cudnn-windows-x86_64-8.9.2.26_cuda11-archive/* \
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2/
環境変数の設定
CUDA関連の環境変数は、CUDAのインストーラにより自動設定されますので ここでは、「CUDNN_PATH = %CUDA_PATH%」のみ設定します。

conda create & conda activate
DOS> conda create -n cuda python=3.10 DOS> conda activate cuda
install tensorflow_gpu & pytorch
tensorflow_gpu
https://www.tensorflow.org/install/source_windows?hl=ja に記載の通り、tensorflow_gpu for win の最新は、2.10.0 ですので これをインストールします。
DOS> pip install tensorflow_gpu==2.10.0
pytorch
pytorch のインストールコマンドは、 https://pytorch.org/get-started/locally/ に記載されていますが、 CUDA 11.7 と 11.8 のものしかないようですので、 今回は11.7をインストールします。
DOS> pip3 install torch torchvision torchaudio \
--index-url https://download.pytorch.org/whl/cu117

pip list
DOS> pip list Package Version ---------------------------- ------------ : tensorboard 2.10.1 tensorboard-data-server 0.6.1 tensorboard-plugin-wit 1.8.1 tensorflow-estimator 2.10.0 tensorflow-gpu 2.10.0 tensorflow-io-gcs-filesystem 0.31.0 : torch 2.0.1+cu117 torchaudio 2.0.2+cu117 torchvision 0.15.2+cu117
pythonがGPUを認識していることの確認
以下のように「True」や「name: "/device:GPU:0"」が 表示されれば、OKです。
# -*- coding: utf-8 -*- import torch from tensorflow.python.client import device_lib; print( torch.cuda.is_available() ) print( torch.version.cuda ) print( torch.cuda.device_count() ) print( torch.cuda.get_device_name() ) print( "" ) print( device_lib.list_local_devices() )
True
11.7
1
NVIDIA GeForce RTX 3060
2023-07-08 14:48:32.607364: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2023-07-08 14:48:33.160368: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1616] Created device /device:GPU:0 with 9616 MB memory: -> device: 0, name: NVIDIA GeForce RTX 3060, pci bus id: 0000:30:00.0, compute capability: 8.6
[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 1672408663635776930
xla_global_id: -1
, name: "/device:GPU:0"
device_type: "GPU"
memory_limit: 10083106816
locality {
bus_id: 1
links {
}
}
incarnation: 4611575803439250516
physical_device_desc: "device: 0, name: NVIDIA GeForce RTX 3060, pci bus id: 0000:30:00.0, compute capability: 8.6"
xla_global_id: 416903419
]