win10+RTX3050ti+TensorFlow+cudn+cudnn配置深度學習環境的方法

避坑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 Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2文件下;

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

查看是否安裝成功:

cd C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2\extras\demo_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深度學習的資料請關註WalkonNet其它相關文章!

推薦閱讀: