首頁 > 軟體

win10+RTX3050ti+TensorFlow+cudn+cudnn設定深度學習環境的方法

2022-06-21 14:02:08

避坑1:RTX30系列顯示卡不支援cuda11.0以下版本,具體上限版本可自行查閱:

方法一,在cmd中輸入nvidia-smi檢視

方法二:

由此可以看出本電腦最高適配cuda11.2.1版本;

注意需要版本適配,這裡我們選擇TensorFlow-gpu = 2.5,cuda=11.2.1,cudnn=8.1,python3.7

接下來可以下載cudn和cundnn:

官網:https://developer.nvidia.com/cuda-toolkit-archive

 下載對應版本exe檔案開啟預設安裝就可;

驗證是否安裝成功:

官網:cuDNN Archive | NVIDIA Developer

把下載檔案進行解壓把bin+lib+include檔案複製到C:Program FilesNVIDIA GPU Computing ToolkitCUDAv11.2檔案下;

進入環境變數設定(cuda會自動設定,如果沒有的補全):

檢視是否安裝成功:

cd C:Program FilesNVIDIA GPU Computing ToolkitCUDAv11.2extrasdemo_suite
bandwidthTest.exe

 安裝tensorflow-gpu:

pip install tensorflow-gpu==2.5

最後我們找相關程式來驗證一下:

第一步:

import tensorflow as tf
print(tf.__version__)
print('GPU', tf.test.is_gpu_available())

第二步:

# _*_ coding=utf-8 _*_
'''
@author: crazy jums
@time: 2021-01-24 20:55
@desc: 新增描述
'''
# 指定GPU訓練
import os
os.environ["CUDA_VISIBLE_DEVICES"]="0"  ##表示使用GPU編號為0的GPU進行計算
import numpy as np
from tensorflow.keras.models import Sequential  # 採用貫序模型
from tensorflow.keras.layers import Dense, Dropout, Conv2D, MaxPool2D, Flatten
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.callbacks import TensorBoard
import time
def create_model():
    model = Sequential()
    model.add(Conv2D(32, (5, 5), activation='relu', input_shape=[28, 28, 1]))  # 第一折積層
    model.add(Conv2D(64, (5, 5), activation='relu'))  # 第二折積層
    model.add(MaxPool2D(pool_size=(2, 2)))  # 池化層
    model.add(Flatten())  # 平鋪層
    model.add(Dropout(0.5))
    model.add(Dense(128, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(10, activation='softmax'))
    return model
def compile_model(model):
    model.compile(loss='categorical_crossentropy', optimizer="adam", metrics=['acc'])
    return model
def train_model(model, x_train, y_train, batch_size=32, epochs=10):
    tbCallBack = TensorBoard(log_dir="model", histogram_freq=1, write_grads=True)
    history = model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, shuffle=True, verbose=2,
                        validation_split=0.2, callbacks=[tbCallBack])
    return history, model
if __name__ == "__main__":
    import tensorflow as tf
    print(tf.__version__)
    from tensorflow.python.client import device_lib
    print(device_lib.list_local_devices())
    (x_train, y_train), (x_test, y_test) = mnist.load_data()  # mnist的資料我自己已經下載好了的
    print(np.shape(x_train), np.shape(y_train), np.shape(x_test), np.shape(y_test))
    x_train = np.expand_dims(x_train, axis=3)
    x_test = np.expand_dims(x_test, axis=3)
    y_train = to_categorical(y_train, num_classes=10)
    y_test = to_categorical(y_test, num_classes=10)
    print(np.shape(x_train), np.shape(y_train), np.shape(x_test), np.shape(y_test))
    model = create_model()
    model = compile_model(model)
    print("start training")
    ts = time.time()
    history, model = train_model(model, x_train, y_train, epochs=2)
    print("start training", time.time() - ts)

驗證成功。

以上就是win10+RTX3050ti+TensorFlow+cudn+cudnn設定深度學習環境的詳細內容,更多關於win10+RTX3050ti+TensorFlow+cudn+cudnn深度學習的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com