Windows安裝GPU環(huán)境CUDA、深度學習框架Tensorflow和Pytorch
1、未安裝CUDA使用tensorflow報錯
import tensorflow as tf
2022-03-06 15:14:38.869955: W tensorflow/stream_executor/platform/default/dso_loader.cc:60] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
2022-03-06 15:14:38.870236: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
2、CUDA介紹
首先需要安裝GPU環(huán)境,包括cuda和cudnn。
深度學習本質(zhì)上就是訓練深度卷積神經(jīng)網(wǎng)絡。
cuda:顯卡能夠完成并行計算任務,所有的操作是比較底層的、復雜的。
cudnn:在cuda之上有一個專門用于深度神經(jīng)網(wǎng)絡的SDK庫來加速完成相關特定的深度學習操作,是用于深度神經(jīng)網(wǎng)絡的GPU加速庫。它強調(diào)性能、易用性和低內(nèi)存開銷。NVIDIA cuDNN可以集成到更高級別的機器學習框架中,如caffe、tensorflow、pytorch、mxnet等。cudnn簡單的插入式設計可以讓開發(fā)人員專注于設計和實現(xiàn)神經(jīng)網(wǎng)絡模型,而不是調(diào)整性能,同時還可以在GPU上實現(xiàn)高性能現(xiàn)代并行計算。
cuda就是用來定義顯卡并行運算的一些列底層GPU操作庫,cudnn則是在cuda基礎上專門正對深度學習定制的高級GPU操作庫。
這里我們匹配的版本是CUDA 11.0
3、安裝CUDA
根據(jù) Nvidia 的說法,CUDA 內(nèi)核現(xiàn)在提供浮點和整數(shù)運算的并發(fā)執(zhí)行,以提高現(xiàn)代游戲計算密集型工作負載的性能。
查詢Tensorflow版本與CUDA的匹配關系
tensorflow_gpu-2.4.0
3.1 下載CUDA
https://developer.nvidia.com/cuda-downloads
選擇Windows,打開cmd查看windows版本
安裝完,在Anaconda中,輸入nvcc -V 進行測試
Anaconda的使用教程可以查看之前的文章:
Python如何使用和配置Anaconda入門
nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2022 NVIDIA Corporation
Built on Thu_Feb_10_19:03:51_Pacific_Standard_Time_2022
Cuda compilation tools, release 11.6, V11.6.112
Build cuda_11.6.r11.6/compiler.30978841_0
CUDA的下載鏈接:https://developer.nvidia.com/cuda-toolkit-archive
cudnn的下載鏈接:https://developer.nvidia.com/cuda-downloads
3.2 CUDA下安裝Tensorflow
激活Anaconda虛擬環(huán)境
conda activate tfenv_py37
conda install tensorflow-gpu
Python 3.7.4 (default, Aug 9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
import tensorflow as tf
2022-03-06 16:21:03.223773: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudart64_110.dll
Cannot dlopen some GPU libraries.Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...
安裝了最新的,版本不匹配,需要根據(jù)顯卡的CUDA版本,選擇對應的CUDA
3.3 測試Tensorflow
import tensorflow as tf
a = tf.constant(2)
Could not load dynamic library 'cudnn64_8.dll'; dlerror: cudnn64_8.dll not found
下載地址:https://developer.nvidia.com/rdp/cudnn-download
3.4 安裝CUDNN
下載鏈接:https://developer.nvidia.com/rdp/cudnn-archive
解壓
復制到C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0
3.5 再次通過Tensorflow測試CUDA
import tensorflow as tf
tf.test.gpu_device_name() # 顯示顯卡型號
print(tf.test.is_gpu_available()) # 提示True
Not creating XLA devices, tf_xla_enable_xla_devices not set
解決方案
os.environ['TF_XLA_FLAGS'] = '--tf_xla_enable_xla_devices' os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
其實這是由于Tensorflow 2.4版本新特性所致,可以**直接忽略,**看看2.4版本的release就一目了然,并不是很多博客說的版本對應問題,回退到老版本治標不治本。
如果需要用XLA,添加TF_XLA_FLAGS=–tf_xla_enable_xla_devices即可解決該warning。
4、安裝pytorch
PyTorch 的速度表現(xiàn)勝過 TensorFlow和Keras 等框架。PyTorch 是所有的框架中面向?qū)ο笤O計的最優(yōu)雅的一個。文章來源:http://www.zghlxwxcb.cn/news/detail-426057.html
PyTorch主要用來進行深度學習算法建模和推理,為了加快算法訓練速度,一般情況下需要使用帶GPU的電腦進行Pytoch安裝,為了讓PyToch能夠使用GPU資源,需要安裝GPU環(huán)境,包括CUDA和CUDNN。
安裝Pytorch文章來源地址http://www.zghlxwxcb.cn/news/detail-426057.html
conda install pytorch torchvision torchaudio cudatoolkit=11.0 -c pytorch
# CUDA 11.0
conda install pytorch==1.7.1 torchvision==0.8.2 torchaudio==0.7.2 cudatoolkit=11.0
pip install torch==1.7.1+cu110 torchvision==0.8.2+cu110 torchaudio==0.7.2 -f https://download.pytorch.org/whl/torch_stable.html
到了這里,關于Windows安裝GPU環(huán)境CUDA、深度學習框架Tensorflow和Pytorch的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!