武漢市市政建設(shè)集團(tuán)網(wǎng)站網(wǎng)站搜索排名靠前
文章目錄
- 簡介
- 運(yùn)行硬件
- cuda和cuddn
- tensorflow安裝。
- tensorflow版本
- 安裝Anaconda
- 創(chuàng)建python環(huán)境
- 安裝tensorflow-gpu
- pycharm配置
- 配置conda環(huán)境
- 配置juypternotebook
- 安裝cuda
- 安裝cudnn
- 安裝blas
- 云服務(wù)器運(yùn)行
- 云服務(wù)器選擇
- pycharm配置
- 代碼自動(dòng)同步
- 遠(yuǎn)程interpreter
簡介
TensorFlow是一種端到端開源機(jī)器學(xué)習(xí)平臺(tái),它提供了一個(gè)全面而靈活的生態(tài)系統(tǒng),包含各種工具、庫和社區(qū)資源,能夠助力研究人員推動(dòng)先進(jìn)機(jī)器學(xué)習(xí)技術(shù)的發(fā)展。在TensorFlow機(jī)器學(xué)習(xí)框架下,開發(fā)者能夠輕松地構(gòu)建和部署由機(jī)器學(xué)習(xí)提供支持的應(yīng)用。[2]
Keras是一個(gè)高層次神經(jīng)網(wǎng)絡(luò) API,適用于快速構(gòu)建原型、高級(jí)研究和生產(chǎn)。它作為TensorFlow的一個(gè)接口,可以兼容多種深度學(xué)習(xí)框架。Keras 的核心數(shù)據(jù)結(jié)構(gòu)是 model,一種組織網(wǎng)絡(luò)層的方式。最簡單的模型是 Sequential 順序模型,它由多個(gè)網(wǎng)絡(luò)層線性堆疊。對(duì)于更復(fù)雜的結(jié)構(gòu),你應(yīng)該使用 Keras 函數(shù)式 API,它允許構(gòu)建任意的神經(jīng)網(wǎng)絡(luò)圖。 Keras最開始是為研究人員開發(fā)的,其目的在于快速實(shí)驗(yàn),具有相同的代碼可以在CPU或GPU上無縫切換運(yùn)行的特點(diǎn),同時(shí)也具有用戶友好的API,方便快速地開發(fā)深度學(xué)習(xí)模型的原型。
Keras使用類sklearn的api接口來調(diào)用tensorflow,從sklearn機(jī)器學(xué)習(xí)中切換過來,更加容易上手。
Tenforflow2.0后直接內(nèi)置可keras。
運(yùn)行硬件
TensorFlow支持在CPU和GPU上運(yùn)行。GPU(圖形處理單元)是一種專門用于加速計(jì)算的硬件,它可以大大提高深度學(xué)習(xí)模型的訓(xùn)練速度。相對(duì)而言,CPU(中央處理器)的每個(gè)核心具有更強(qiáng)大的處理能力,但它們的數(shù)量通常非常有限,因此在處理大數(shù)據(jù)時(shí)它們表現(xiàn)不佳。
TensorFlow GPU和CPU的主要區(qū)別在于如何使用硬件來處理計(jì)算任務(wù),以及處理速度的差異。在CPU上,TensorFlow利用所有可用的CPU內(nèi)核并將任務(wù)分配給它們,這可能需要幾分鐘或幾小時(shí)來完成。在GPU上,TensorFlow使用CUDA(Compute Unified Device Architecture)技術(shù)來利用GPU進(jìn)行并行計(jì)算并加速訓(xùn)練過程,因?yàn)镚PU擁有數(shù)百到數(shù)千個(gè)小型核心,這比CPU的幾十個(gè)核心要多得多。這使得TensorFlow能夠在GPU上實(shí)現(xiàn)更快的訓(xùn)練速度和更高的吞吐量,尤其是在處理大規(guī)模的深度學(xué)習(xí)任務(wù)時(shí)。
另外需要注意的是,如果你的計(jì)算機(jī)沒有安裝專門的GPU,則無法使用TensorFlow GPU。在這種情況下,TensorFlow會(huì)使用CPU作為默認(rèn)選項(xiàng),但是訓(xùn)練過程會(huì)比在GPU上慢得多。因此,如果你需要進(jìn)行大量的深度學(xué)習(xí)訓(xùn)練任務(wù),建議使用具有至少一張GPU的計(jì)算機(jī)來加速訓(xùn)練。
總之,TensorFlow GPU和CPU之間的區(qū)別在于它們的硬件架構(gòu)、并行計(jì)算能力以及處理速度等方面。當(dāng)進(jìn)行大規(guī)模的深度學(xué)習(xí)訓(xùn)練時(shí),使用GPU可以顯著提高訓(xùn)練速度和吞吐量,而對(duì)于較小的任務(wù)或者沒有專門GPU的計(jì)算機(jī),則應(yīng)該使用CPU。
cuda和cuddn
CUDA(Compute Unified Device Architecture)是一種由NVIDIA公司開發(fā)的并行計(jì)算平臺(tái)和編程模型,它允許開發(fā)人員使用標(biāo)準(zhǔn)C/C++語言編寫基于GPU的高性能應(yīng)用程序。CUDA包括一個(gè)可編程的內(nèi)核語言(CUDA C/C++),一個(gè)并行計(jì)算庫(CUDA Toolkit),以及驅(qū)動(dòng)程序和硬件架構(gòu),支持對(duì)NVIDIA GPU進(jìn)行高性能并行計(jì)算。與CPU相比,GPU在并行處理任務(wù)時(shí)的性能要高得多,因此CUDA被廣泛用于深度學(xué)習(xí)、科學(xué)計(jì)算和高性能計(jì)算等領(lǐng)域。[2]
cuDNN(CUDA Deep Neural Network library)是NVIDIA CUDA的一個(gè)加速庫,它提供了一組高度優(yōu)化的本地函數(shù),用于加速深度神經(jīng)網(wǎng)絡(luò)模型的訓(xùn)練和推理。cuDNN主要用于卷積神經(jīng)網(wǎng)絡(luò)(CNNs)和遞歸神經(jīng)網(wǎng)絡(luò)(RNNs)等深度學(xué)習(xí)模型的優(yōu)化,從而實(shí)現(xiàn)更快的訓(xùn)練和推理速度。cuDNN支持多種深度學(xué)習(xí)框架,包括TensorFlow,PyTorch和Caffe等。[1]
因此,CUDA是一種GPU計(jì)算平臺(tái)和編程模型,cuDNN是其中一個(gè)加速庫,專門用于加速深度學(xué)習(xí)模型的訓(xùn)練和推理。這兩個(gè)技術(shù)結(jié)合起來,可以實(shí)現(xiàn)對(duì)GPU的高性能并行計(jì)算和深度學(xué)習(xí)模型的優(yōu)化,從而提高深度學(xué)習(xí)任務(wù)的整體性能。
不同的tensorflow版本需要不同的cuda和cuddn版本,google官網(wǎng)可查看
https://tensorflow.google.cn/install/source_windows?hl=en
如果電腦有g(shù)pu建議安裝tensorflow-gpu,如果電腦沒有g(shù)pu安裝性能較差的tensorflow
打開任務(wù)管理器-性能,查看你是否支持gpu
從這里我們可以看到我的cpu是英偉達(dá)(nvidia)的gtx1050
如果電腦已經(jīng)安裝顯卡驅(qū)動(dòng),cuda肯定是自帶的,我們可以使用nvidia-smi命令查看
tensorflow安裝。
tensorflow版本
tensorflow-gpu需要至少4GB的GPU顯存才能運(yùn)行,并且在訓(xùn)練模型時(shí)需要大量的計(jì)算資源。而tensorflow-cpu則是專門為CPU設(shè)計(jì)的版本,能夠在CPU上高效地運(yùn)行,同時(shí)不需要GPU顯存。一般pc電腦的環(huán)境中,建議使用tensorflow-cpu會(huì)更加適合。當(dāng)然,如果你未來有升級(jí)GPU的計(jì)劃,可以考慮使用tensorflow-gpu。
安裝Anaconda
首先我們安裝Anaconda,教程參考:
打開命令行
同時(shí)這里建議使用anaconda的替代方案mamba,因?yàn)閏onda包多了之后安裝超級(jí)慢,會(huì)讓你崩潰的,mamba基本匹配pip的速度,官網(wǎng)。
在window上打開powershell,執(zhí)行web命令下載壓縮包
mkdir d:\green\mamba && cd d:\green\mamba
Invoke-Webrequest -URI https://micro.mamba.pm/api/micromamba/win-64/latest -OutFile micromamba.tar.bz2
tar xf micromamba.tar.bz2
比如我的下載這里,直接將解壓路徑添加到path環(huán)境變量中
其他命令就和conda一樣了,比如
micromamba env list
micomamba activate base
創(chuàng)建python環(huán)境
在其他盤創(chuàng)建一個(gè)環(huán)境,假設(shè)使用python3.7版本
conda create --prefix=d:\condaenv\env_name python=3.7
切換
activate d:\condaenv\env_name
(d:\condaenv\env_name) C:\Users\liaomin>python --version
Python 3.7.4
安裝tensorflow-gpu
通過版本關(guān)系圖
可以確定我們可以使用python3.7版本安裝tensorflow-gpu 2.0.0以上所有版本,我們選擇一個(gè)2.6.0
#設(shè)置鏡像源
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
#安裝tensorflow-gpu
conda install tensorflow-gpu==2.6.0
#或者(建議用mamba)
micomamba install tensorflow-gpu==2.6.0
#如果是cpu版本直接
micomamba install tensorflow==2.6.0
安裝完成我們先不急著安裝cuddn和cuda可以先寫個(gè)helloworld測試下
pycharm配置
配置conda環(huán)境
創(chuàng)建一個(gè)項(xiàng)目pure python項(xiàng)目,點(diǎn)擊project interpreter 選擇Existing interpreter,點(diǎn)擊右側(cè)的…
選擇conda environment
然后點(diǎn)擊ok 在interpreter下拉框中選擇剛新建的那個(gè)
編寫一段helloworld代碼
import tensorflow as tf# 創(chuàng)建一個(gè)常量張量
x = tf.constant([1, 2, 3])# 創(chuàng)建一個(gè)變量張量
y = tf.Variable([3, 2, 1])# 計(jì)算兩個(gè)張量的和
z = tf.add(x, y)# 輸出結(jié)果
print(z.numpy())
運(yùn)行,雖然能輸出結(jié)果[4 4 4],但是有紅色警告
2023-05-06 16:43:35.610912: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
#下面這段是安裝好cuda后報(bào)的錯(cuò)。
2023-05-06 17:37:38.727999: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudnn64_8.dll'; dlerror: cudnn64_8.dll not found
2023-05-06 17:37:38.728345: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1835] 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...
2023-05-06 17:37:38.729310: I tensorflow/core/platform/cpu_feature_guard.cc:142] 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.
顯然缺少cuda和cudnn,注意這里缺少cudart64_110.dll并不是說cuda就是110版本,實(shí)際你11.2安裝后也是這個(gè)dll。
配置juypternotebook
打開anaconda prompt,并且激活你新穿件的環(huán)境,安裝jupyter
conda install jupyter notebook
#或者(建議用mamba)
micomamba install jupyter notebook
在pycharm中右鍵創(chuàng)建一個(gè)notebook
輸入之前的helloword代碼,選擇運(yùn)行或者調(diào)試
右側(cè)輸出結(jié)果
安裝cuda
通過版本關(guān)系圖,我們知道tensorflow-gpu 2.6.0需要安裝11.2的cuda
如果是cpu版本無需安裝
cuda歷史版本下載位置,下載對(duì)應(yīng)版本安裝
這里有三個(gè)版本選擇最高的11.2.2
點(diǎn)擊進(jìn)入后 選擇window版本
默認(rèn)安裝路徑是C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA
打開電腦設(shè)置——>系統(tǒng)——>系統(tǒng)高級(jí)設(shè)置——>環(huán)境變量——>系統(tǒng)變量——>PATH
將C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2\bin加入到環(huán)境變量PATH中
cmd重新執(zhí)行nvidia-smi,發(fā)現(xiàn)版本更新了
發(fā)現(xiàn)之前缺失的cudart64_110.dll確實(shí)在C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2\bin
安裝cudnn
通過版本關(guān)系圖,我們知道tensorflow-gpu 2.6.0需要安裝8.1的cudnn
cudnn歷史版本下載位置,下載對(duì)應(yīng)版本8.1,下載cudnn需要注冊(cè)nvidia
選擇cudnn library for window(x86)點(diǎn)擊下載
打開cudnn文件夾
將上述cudnn里面的文件移動(dòng)或copy到cuda對(duì)應(yīng)文件夾目錄下即可!
此時(shí)在運(yùn)行helloworld程序使用gpu正常運(yùn)行
2023-05-06 19:01:23.403530: I tensorflow/core/platform/cpu_feature_guard.cc:142] 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-05-06 19:01:24.075663: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1510] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 1320 MB memory: -> device: 0, name: GeForce GTX 1050, pci bus id: 0000:01:00.0, compute capability: 6.1
[4 4 4]
安裝blas
TensorFlow 是一個(gè)使用 C++ 編寫的開源機(jī)器學(xué)習(xí)框架,它支持在 CPU 和 GPU 上運(yùn)行計(jì)算。在 TensorFlow 中,BLAS(Basic Linear Algebra Subprograms)庫是用于執(zhí)行線性代數(shù)計(jì)算的關(guān)鍵庫,例如矩陣乘法和向量加法。由于這些操作在機(jī)器學(xué)習(xí)中非常常見,因此 BLAS 庫對(duì)于 TensorFlow 的性能和穩(wěn)定性至關(guān)重要。
切換到你的python環(huán)境,使用以下命令來安裝 BLAS 庫:
conda install blas
#或者(建議用mamba)
micomamba install openblas
安裝完成后,因?yàn)榘惭b的是動(dòng)態(tài)鏈接庫最終仍然會(huì)安裝在你的anaconda的base環(huán)境下,找到動(dòng)態(tài)鏈接庫的位置
C:\Users\你的用戶名\anaconda3\pkgs\blas-2.116-blis\Library\bin
將上面的路徑添加到環(huán)境變量PATH中,如果不添加tensorflow會(huì)報(bào)錯(cuò)找不到blas
執(zhí)行以下代碼測試
python -c "import tensorflow as tf; print(tf.linalg.matmul(tf.ones([1, 2]), tf.ones([2, 2])))"
運(yùn)行結(jié)果
(d:\condaenv\env_name) C:\Users\liaomin>python -c "import tensorflow as tf; print(tf.linalg.matmul(tf.ones([1, 2]), tf.ones([2, 2])))"
2023-05-10 09:39:23.654612: I tensorflow/core/platform/cpu_feature_guard.cc:142] 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-05-10 09:39:24.372107: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1510] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 1320 MB memory: -> device: 0, name: GeForce GTX 1050, pci bus id: 0000:01:00.0, compute capability: 6.1
tf.Tensor([[2. 2.]], shape=(1, 2), dtype=float32)
云服務(wù)器運(yùn)行
云服務(wù)器提供高性能的CPU和GPU運(yùn)算服務(wù)器,對(duì)個(gè)人開發(fā)者來說非常便宜一般幾十塊錢半個(gè)月的秒殺團(tuán)很多,可以提供強(qiáng)大的計(jì)算能力。對(duì)于需要大量計(jì)算資源的TensorFlow程序,使用騰訊云服務(wù)器可以提高計(jì)算效率
云服務(wù)器選擇
我這里選擇騰訊云,對(duì)個(gè)人友好,可以微信登錄,微信支付。
右側(cè)的gpu服務(wù)器是gpu 8gb的適合個(gè)人學(xué)習(xí)來跑神經(jīng)網(wǎng)絡(luò)。
購買的時(shí)候選擇系統(tǒng)為:unbuntu的tensorflow版本,我選擇的是:TensorFlow 2.8.0 Ubuntu 18.04 GPU基礎(chǔ)鏡像(預(yù)裝460驅(qū)動(dòng)),不要選window自己安裝環(huán)境,麻煩,因?yàn)槲覀兊膒ycharm支持遠(yuǎn)程ssh編程,用服務(wù)器跑,結(jié)果顯示在pycharm中。
重置你的ubuntu密碼(系統(tǒng)默認(rèn)的賬號(hào)是ubuntu,不是root),主機(jī)會(huì)給你配個(gè)公網(wǎng)地址
pycharm配置
代碼自動(dòng)同步
我們?cè)?homt/ubuntu用戶目錄下新建一個(gè)deeplearn目錄用于映射本地代碼
點(diǎn)擊tools-deployment-configuation
選擇sftp,輸入賬號(hào)密碼測試連接
點(diǎn)擊mappings目錄映射本地目錄和遠(yuǎn)程目錄
確定后在享有右鍵deployments->upload 上傳代碼
點(diǎn)擊tools-deployments-browe remote host查看遠(yuǎn)程目錄是否上傳(勾上Automatic upload(always)保存代碼自動(dòng)上傳)
遠(yuǎn)程interpreter
點(diǎn)擊File-Settings-Project(項(xiàng)目名)-Project interpreter add一個(gè)
輸入密碼后下一步進(jìn)入配置python目錄,我們可以使用shell登錄到遠(yuǎn)程服務(wù)器執(zhí)行
ubuntu@VM-0-5-ubuntu:~$ which python3
/usr/local/miniconda3/bin//python3
在interpreter上輸入python3的路徑即可
確認(rèn)項(xiàng)目選擇了該interpreter
接下來打開神經(jīng)網(wǎng)絡(luò)代碼,,代碼右鍵運(yùn)行,可看到運(yùn)行是ssh運(yùn)行的
執(zhí)行后的模型實(shí)際上是在遠(yuǎn)程服務(wù)器的可以使用brwoer remote host右鍵下載下來覆蓋本地