從頭搭建神經(jīng)網(wǎng)絡(luò)模型環(huán)境

本文檔記錄了從頭開(kāi)始搭建環(huán)境的過(guò)程,主要是中間遇到的問(wèn)題以及解決方法。
本文檔安裝的程序會(huì)盡量帶上版本號(hào),因?yàn)楹芏鄦?wèn)題都是版本不匹配引起的。
使用環(huán)境是Windows10系統(tǒng)。

安裝python3.7(略)

安裝pip(略)

安裝jupyter notebook

pip install jupyter==1.0.0

啟動(dòng)命令:

jupyter notebook

安裝tensorflow

pip install tensorflow-gpu==2.10.1 pandas

支持GPU

前提是你要有一塊Nvidia的高性能顯卡。主要是安裝CUDA和cuDNN,可以按照你訪問(wèn)網(wǎng)站時(shí)的最新版本下載安裝,兩個(gè)程序版本是配套的就行。我這里安裝的版本是可以和tensorflow 2.10.1 正常配合使用的。

安裝CUDA 12.1

https://developer.nvidia.com/cuda-downloads
安裝目錄默認(rèn)在C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.1

下載 cuDNN 8.8.1 for CUDA 12.x

https://developer.nvidia.com/rdp/cudnn-download
cuDNN的版本要和CUDA配套。解壓出的bin目錄內(nèi)容復(fù)制進(jìn)入C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.1\bin目錄中。其實(shí)就是將所有的dll文件放入CUDA的bin目錄中。

問(wèn)題匯總

啟動(dòng)jupyter notebook后出現(xiàn)錯(cuò)誤:ImportError: cannot import name 'soft_unicode' from 'markupsafe'

解決方法:

pip install MarkupSafe==2.0.1

出現(xiàn)各種dll動(dòng)態(tài)庫(kù)無(wú)法加載的問(wèn)題。

解決方法:
1、將C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.1\bin里面的所有dll都復(fù)制到C:\Windows\System32目錄下
2、將cuDNN壓縮包里面bin目錄下的dll文件都復(fù)制到C:\Windows\System32目錄下

出現(xiàn)錯(cuò)誤:error: Can't find libdevice directory ${CUDA_DIR}/nvvm/libdevice

解決方法:
設(shè)置系統(tǒng)環(huán)境變量 XLA_FLAGS=--xla_gpu_cuda_data_dir="C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.1"
注意里面的引號(hào)不能少。

驗(yàn)證GPU是否能工作

識(shí)別GPU

import tensorflow as tf
device_gpu = tf.config.list_physical_devices('GPU')
print(device_gpu)
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]

如果數(shù)據(jù)不是空數(shù)組,說(shuō)明GPU識(shí)別成功。

調(diào)用GPU計(jì)算

import tensorflow as tf
gpus = tf.config.list_physical_devices("GPU")
print(gpus)

import time
from contextlib import contextmanager

@contextmanager
def timer():
    start_time = time.perf_counter()
    yield
    end_time = time.perf_counter()
    print(f"Code block took {end_time - start_time:.6f} s to run")


with timer():
    with tf.device("/gpu:0"):
        tf.random.set_seed(0)
        a = tf.random.uniform((10000,100),minval = 0,maxval = 3.0)
        b = tf.random.uniform((100,100000),minval = 0,maxval = 3.0)
        c = a@b
        tf.print(tf.reduce_sum(tf.reduce_sum(c,axis = 0),axis=0))


with timer():
    with tf.device("/cpu:0"):
        tf.random.set_seed(0)
        a = tf.random.uniform((10000,100),minval = 0,maxval = 3.0)
        b = tf.random.uniform((100,100000),minval = 0,maxval = 3.0)
        c = a@b
        tf.print(tf.reduce_sum(tf.reduce_sum(c,axis = 0),axis=0))
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
2.24953778e+11
Code block took 0.443041 s to run
2.24953778e+11
Code block took 5.125858 s to run

如果這個(gè)代碼能正常執(zhí)行,說(shuō)明你的環(huán)境已經(jīng)準(zhǔn)備好了。

GPU相關(guān)的命令行

命令行 說(shuō)明
nvidia-smi 提供監(jiān)控GPU使用情況和更改GPU狀態(tài)的功能。GPU之nvidia-smi命令詳解
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容