原文:https://makeoptim.com/deep-learning/tensorflow-gpu-on-win10
- 介紹
- 所需軟件
- Nvidia GPU driver
- CUDA ToolKit
- cuDNN
- 環(huán)境變量
- Miniconda
- 虛擬環(huán)境
- 安裝 TensorFlow
- 安裝 JupyterLab 和 matplotlib
- 安裝 pywin32
- 在 JupyterLab 中運(yùn)行 TensorFlow
- 安裝 VSCode
- VSCode 運(yùn)行 TensorFlow
- 小結(jié)
- 延伸閱讀
- 參考鏈接
介紹
- Win10
- GTX 1050ti
- tensorflow 2.4.1
所需軟件
- NVIDIA? GPU drivers:CUDA? 11.0 需要 450.x 或更高版本辅鲸。
- CUDA? Toolkit:TensorFlow 支持 CUDA? 11(TensorFlow 2.4.0 及更高版本)
- CUDA? Toolkit附帶的CUPTI端壳。
- cuDNN SDK 8.0.4cuDNN 版本).
- (可選)TensorRT 6.0割岛,可縮短用某些模型進(jìn)行推斷的延遲時(shí)間并提高吞吐量。
Nvidia GPU driver
前往 https://www.nvidia.com/download/index.aspx?lang=en-us 下載并安裝對應(yīng)的顯卡驅(qū)動(dòng)。
重啟并使用以下命令檢查 GPU 是否可見。
PS C:\Users\Administrator> nvidia-smi.exe
Wed Apr 28 22:28:23 2021
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 466.11 Driver Version: 466.11 CUDA Version: 11.3 |
|-------------------------------+----------------------+----------------------+
| GPU Name TCC/WDDM | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|===============================+======================+======================|
| 0 NVIDIA GeForce ... WDDM | 00000000:01:00.0 On | N/A |
| 30% 34C P0 N/A / 75W | 455MiB / 4096MiB | 1% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
| 0 N/A N/A 1532 C+G C:\Windows\System32\dwm.exe N/A |
| 0 N/A N/A 2800 C+G ...Central\Razer Central.exe N/A |
| 0 N/A N/A 4576 C+G C:\Windows\explorer.exe N/A |
| 0 N/A N/A 5104 C+G ...5n1h2txyewy\SearchApp.exe N/A |
| 0 N/A N/A 6112 C+G ...lPanel\SystemSettings.exe N/A |
| 0 N/A N/A 7588 C+G ...me\Application\chrome.exe N/A |
| 0 N/A N/A 8568 C+G ...arp.BrowserSubprocess.exe N/A |
| 0 N/A N/A 9076 C+G ...nputApp\TextInputHost.exe N/A |
| 0 N/A N/A 9096 C+G ... Host\Razer Synapse 3.exe N/A |
| 0 N/A N/A 10152 C+G ...y\ShellExperienceHost.exe N/A |
+-----------------------------------------------------------------------------+
CUDA ToolKit
前往 http://developer.nvidia.com/cuda-downloads 下載 CUDA Toolkit 11.3 并安裝。
cuDNN
前往 https://developer.nvidia.com/rdp/cudnn-archive 下載 cuDNN 并解壓柬帕。
將解壓后的 lib、bin、include
陷寝,拷貝到 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1
對應(yīng)的目錄下锅很。
復(fù)制 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\bin
下的 cusolver64_11.dll
,重命名為 cusolver64_10.dll
盼铁。
環(huán)境變量
SET PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\bin;%PATH%
SET PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\extras\CUPTI\lib64;%PATH%
SET PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\include;%PATH%
SET PATH=C:\tools\cuda\bin;%PATH%
Miniconda
前往 https://docs.conda.io/en/latest/miniconda.html 下載并安裝 Miniconda
粗蔚。
虛擬環(huán)境
打開 Anaconda Powershell Prompt
。
創(chuàng)建一個(gè)名稱為 tensorflow
的虛擬環(huán)境饶火。
$ conda create -n tensorflow python=3.8.5
$ conda activate tensorflow
注:如果使用 Power Sehll鹏控, 需要執(zhí)行
conda init powershell
和set-executionpolicy remotesigned
, 否則會(huì)出現(xiàn)無法activate
成功地問題,詳見 https://stackoverflow.com/questions/62082873/conda-not-activate-in-power-shell
PS C:\Users\Administrator> conda init powershell
PS C:\Users\Administrator> set-executionpolicy remotesigned
安裝 TensorFlow
(tensorflow) PS C:\Users\Administrator> pip install tensorflow==2.4.1
驗(yàn)證安裝
(tensorflow) PS C:\Users\Administrator> python -c "import tensorflow as tf;print('Num GPUs Available: ', len(tf.config.list_physical_devices('GPU')))"
2021-05-05 21:38:12.335369: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudart64_110.dll
2021-05-05 21:38:14.569229: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set
2021-05-05 21:38:14.571864: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library nvcuda.dll
2021-05-05 21:38:14.605855: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1720] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: NVIDIA GeForce GTX 1050 Ti computeCapability: 6.1
coreClock: 1.392GHz coreCount: 6 deviceMemorySize: 4.00GiB deviceMemoryBandwidth: 104.43GiB/s
2021-05-05 21:38:14.606025: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudart64_110.dll
2021-05-05 21:38:14.682846: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cublas64_11.dll
2021-05-05 21:38:14.682987: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cublasLt64_11.dll
2021-05-05 21:38:14.721429: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cufft64_10.dll
2021-05-05 21:38:14.732614: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library curand64_10.dll
2021-05-05 21:38:14.818913: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cusolver64_10.dll
2021-05-05 21:38:14.842604: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cusparse64_11.dll
2021-05-05 21:38:14.848834: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudnn64_8.dll
2021-05-05 21:38:14.849000: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1862] Adding visible gpu devices: 0
Num GPUs Available: 1
安裝 JupyterLab 和 matplotlib
(tensorflow) PS C:\Users\Administrator> pip install jupyterlab matplotlib
安裝 pywin32
(tensorflow) PS C:\Users\Administrator> cd 'C:\ProgramData\Miniconda3\Scripts\'
(tensorflow) PS C:\ProgramData\Miniconda3\Scripts> python pywin32_postinstall.py -install
Parsed arguments are: Namespace(destination='C:\\ProgramData\\Miniconda3\\envs\\tensorflow\\Lib\\site-packages', install=True, quiet=False, remove=False, silent=False, wait=None)
pywin32_postinstall.py:164: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
import imp
Copied pythoncom38.dll to C:\WINDOWS\system32\pythoncom38.dll
Copied pywintypes38.dll to C:\WINDOWS\system32\pywintypes38.dll
Registered: Python.Interpreter
Registered: Python.Dictionary
Registered: Python
-> Software\Python\PythonCore\3.8\Help[None]=None
-> Software\Python\PythonCore\3.8\Help\Pythonwin Reference[None]='C:\\ProgramData\\Miniconda3\\envs\\tensorflow\\Lib\\site-packages\\PyWin32.chm'
Pythonwin has been registered in context menu
Creating directory C:\ProgramData\Miniconda3\envs\tensorflow\Lib\site-packages\win32com\gen_py
Can't install shortcuts - 'C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Python 3.8' is not a folder
The pywin32 extensions were successfully installed.
在 JupyterLab 中運(yùn)行 TensorFlow
jupyter lab
JupyterLab
將自動(dòng)在瀏覽器打開肤寝。
從 https://www.tensorflow.org/tutorials/images/cnn 下載并導(dǎo)入 CNN notebook
当辐。
執(zhí)行 Restart Kernel and Run All Cells
。
當(dāng)訓(xùn)練開始, 檢查 GPU 進(jìn)程鲤看,可以看到 ...nvs\tensorflow\python.exe
表示正在使用 GPU 訓(xùn)練模型缘揪。
PS C:\Users\Administrator> nvidia-smi.exe
Wed May 5 21:47:50 2021
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 466.11 Driver Version: 466.11 CUDA Version: 11.3 |
|-------------------------------+----------------------+----------------------+
| GPU Name TCC/WDDM | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|===============================+======================+======================|
| 0 NVIDIA GeForce ... WDDM | 00000000:01:00.0 On | N/A |
| 30% 39C P0 N/A / 75W | 3695MiB / 4096MiB | 50% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
| 0 N/A N/A 1008 C+G C:\Windows\explorer.exe N/A |
| 0 N/A N/A 3620 C+G ...8bbwe\WindowsTerminal.exe N/A |
| 0 N/A N/A 6936 C+G ...nputApp\TextInputHost.exe N/A |
| 0 N/A N/A 7024 C+G ...5n1h2txyewy\SearchApp.exe N/A |
| 0 N/A N/A 9648 C+G ...me\Application\chrome.exe N/A |
| 0 N/A N/A 12128 C+G C:\Windows\System32\dwm.exe N/A |
| 0 N/A N/A 12248 C+G ...y\ShellExperienceHost.exe N/A |
| 0 N/A N/A 14384 C ...nvs\tensorflow\python.exe N/A |
+-----------------------------------------------------------------------------+
安裝 VSCode
前往官網(wǎng)下載并安裝 VSCode
。
打開 VSCode
并安裝 Python
支持义桂。
選擇某個(gè)文件夾(這里以 ~/tensorflow-notebook/01-hello
為例)找筝,新建文件 hello.ipynb
。
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
hello.numpy()
使用 VSCode
打開剛才創(chuàng)建的 ~/tensorflow-notebook/01-hello/hello.ipynb
慷吊,并選擇 Python
為創(chuàng)建的虛擬環(huán)境袖裕。
VSCode 運(yùn)行 TensorFlow
小結(jié)
至此急鳄,開發(fā)環(huán)境已經(jīng)搭建完畢堰酿。大家可以根據(jù)自己的習(xí)慣疾宏,選擇使用命令行触创、JupyterLab
或者 VSCode 進(jìn)行開發(fā)。
延伸閱讀
- Mac 機(jī)器學(xué)習(xí)環(huán)境 (TensorFlow, JupyterLab, VSCode)
- Mac M1 機(jī)器學(xué)習(xí)環(huán)境 (TensorFlow, JupyterLab, VSCode)
- Ubuntu 機(jī)器學(xué)習(xí)環(huán)境 (TensorFlow GPU, JupyterLab, VSCode)