AI - Win10 機(jī)器學(xué)習(xí)環(huán)境 (TensorFlow GPU, JupyterLab, VSCode)

原文:https://makeoptim.com/deep-learning/tensorflow-gpu-on-win10

介紹

  • Win10
  • GTX 1050ti
  • tensorflow 2.4.1

所需軟件

Nvidia GPU driver

前往 https://www.nvidia.com/download/index.aspx?lang=en-us 下載并安裝對應(yīng)的顯卡驅(qū)動(dòng)。

image

重啟并使用以下命令檢查 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 并安裝。

image

cuDNN

前往 https://developer.nvidia.com/rdp/cudnn-archive 下載 cuDNN 并解壓柬帕。

image

將解壓后的 lib、bin、include陷寝,拷貝到 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1 對應(yīng)的目錄下锅很。

image
image
image

復(fù)制 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\bin 下的 cusolver64_11.dll,重命名為 cusolver64_10.dll 盼铁。

image

環(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粗蔚。

image
image

虛擬環(huán)境

打開 Anaconda Powershell Prompt

image

創(chuàng)建一個(gè)名稱為 tensorflow 的虛擬環(huán)境饶火。

$ conda create -n tensorflow python=3.8.5
$ conda activate tensorflow

注:如果使用 Power Sehll鹏控, 需要執(zhí)行 conda init powershellset-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当辐。

image

執(zhí)行 Restart Kernel and Run All Cells

image

當(dāng)訓(xùn)練開始, 檢查 GPU 進(jìn)程鲤看,可以看到 ...nvs\tensorflow\python.exe 表示正在使用 GPU 訓(xùn)練模型缘揪。

image
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 支持义桂。

image

選擇某個(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)境袖裕。

image

VSCode 運(yùn)行 TensorFlow

image

小結(jié)

至此急鳄,開發(fā)環(huán)境已經(jīng)搭建完畢堰酿。大家可以根據(jù)自己的習(xí)慣疾宏,選擇使用命令行触创、JupyterLab 或者 VSCode 進(jìn)行開發(fā)。

延伸閱讀

參考鏈接

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末岩馍,一起剝皮案震驚了整個(gè)濱河市凌那,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌赦肋,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,013評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件囱井,死亡現(xiàn)場離奇詭異趣避,居然都是意外死亡庞呕,警方通過查閱死者的電腦和手機(jī)程帕,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,205評論 2 382
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來讲逛,“玉大人岭埠,你說我怎么就攤上這事∠郏” “怎么了?”我有些...
    開封第一講書人閱讀 152,370評論 0 342
  • 文/不壞的土叔 我叫張陵混聊,是天一觀的道長蹦掐。 經(jīng)常有香客問我技羔,道長卧抗,這世上最難降的妖魔是什么鳖粟? 我笑而不...
    開封第一講書人閱讀 55,168評論 1 278
  • 正文 為了忘掉前任,我火速辦了婚禮泳秀,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘嗜傅。我一直安慰自己檩赢,他們只是感情好吕嘀,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,153評論 5 371
  • 文/花漫 我一把揭開白布偶房。 她就那樣靜靜地躺著,像睡著了一般棕洋。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上掰盘,一...
    開封第一講書人閱讀 48,954評論 1 283
  • 那天愧捕,我揣著相機(jī)與錄音射众,去河邊找鬼晃财。 笑死,一個(gè)胖子當(dāng)著我的面吹牛断盛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播钢猛,決...
    沈念sama閱讀 38,271評論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼贩绕!你這毒婦竟也來了壶愤?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 36,916評論 0 259
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后勃黍,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,382評論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,877評論 2 323
  • 正文 我和宋清朗相戀三年荆永,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了豆村。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 37,989評論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡欧瘪,死狀恐怖妖碉,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情鱼鸠,我是刑警寧澤愉昆,帶...
    沈念sama閱讀 33,624評論 4 322
  • 正文 年R本政府宣布扮授,位于F島的核電站嚎尤,受9級特大地震影響乏梁,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜落萎,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,209評論 3 307
  • 文/蒙蒙 一念祭、第九天 我趴在偏房一處隱蔽的房頂上張望粱坤。 院中可真熱鬧枚驻,春花似錦再登、人聲如沸锉矢。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,199評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至崩掘,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背辑畦。 一陣腳步聲響...
    開封第一講書人閱讀 31,418評論 1 260
  • 我被黑心中介騙來泰國打工硬贯, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留鸵赖,地道東北人肾砂。 一個(gè)月前我還...
    沈念sama閱讀 45,401評論 2 352
  • 正文 我出身青樓列赎,卻偏偏與公主長得像,于是被迫代替她去往敵國和親包吝。 傳聞我的和親對象是個(gè)殘疾皇子源葫,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,700評論 2 345