Tensorflow-gpu installation + Keras installation + Pytorch-gpu installation experience sharing/Win10 (conda installs Cuda and Cudnn) with test code

Tensorflow-gpu installation + Keras installation + Pytorch-gpu installation experience sharing/Win10 (conda installs Cuda and Cudnn) with test code

Notice:

The installation of Cuda and Cudnn in this article adopts the conda installation method, and there is no need to download and install software such as Cuda and Cudnn separately.
The following tutorials are based on the already installed Anaconda.

System: Windows10

Anaconda:2020.07

python 3.8.0

cuda version: cuda 11.0

cudnn version: cudnn 8.1

Tensorflow version: tensorflow-gpu 2.4.0

keras version: keras-2.4.0

Preparations

Anaconda environment variable configuration

1. Check Add during installation.
2. Configure by yourself: Computer->Properties->Advanced System Settings->Environment Variables->System Variables, find Path, and add the storage paths of three folders, as shown in the figure below.
insert image description here

Add mirror source

What is added here is the source of Ali, free and main are mandatory, others are optional

conda config --add channels https://mirrors.aliyun.com/anaconda/pkgs/free
conda config --add channels https://mirrors.aliyun.com/anaconda/pkgs/main
conda config --add channels https://mirrors.aliyun.com/anaconda/pkgs/msys2
conda config --add channels https://mirrors.aliyun.com/anaconda/pkgs/r

conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/Paddle
conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/auto
conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/biobakery
conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/bioconda
conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/c4aarch64
conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/caffe2
conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/conda-forge
conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/deepmodeling
conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/dglteam
conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/fastai
conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/fermi
conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/idaholab
conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/intel
conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/matsci
conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/menpo
conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/mordred-descriptor
conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/msys2
conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/numba
conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/ohmeta
conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/omnia
conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/plotly
conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/psi4
conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/pytorch
conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/pytorch-test
conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/pytorch3d
conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/pyviz
conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/qiime2
conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/rapidsai
conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/rdkit
conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/simpleitk
conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/stackless
conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/ursky

conda config --set show_channel_urls yes

https://www.cnblogs.com/fang-d/p/aliyun_conda_mirrors.html

Check the CUDA version supported by your graphics card

Right click on NVIDIA Control Panel -> Help -> Components. As shown in the figure below, remember not to exceed this version
insert image description here

TensorFlow (GPU) installation

Create a virtual environment named tensorflow-keras-gpu and activate it

conda create -n tensorflow-keras-gpu python=3.8

conda activate tensorflow-keras-gpu

(Exit the virtual environment: conda deactivate)
insert image description here

https://tensorflow.google.cn/install/source_windows#gpu
cuda11.0 and cudnn8.1 are installed here. Due to the incompatibility of versions in previous operations, different versions need to be upgraded

conda install cudatoolkit=11.0
conda install cudnn=8.1

tensorflow-gpu installation

1. tensorflow-gpu can be installed directly through conda.

pip install tensorflow-gpu==2.4.0

2. Because I chose to install cuda and cudnn with conda first, and then install tensorflow-gpu through the whl file (the versions of the three should correspond, please refer to the official tested configuration)

After downloading the whl file of tensorflow_gpu-2.4.0, enter the following code to install:
pip install + the location of the whl file, for example

pip install C:\Users\XXX\Desktop\tensorflow_gpu-2.4.0-cp38-cp38-win_amd64.whl

insert image description here

https://tensorflow.google.cn/install/pip#package-location

Keras installation

Install directly through pip or conda, be sure to select the version number, do not default, if you make a mistake, you can upgrade it yourself

pip install keras==2.4.0
conda install keras==2.4.0

code testing

Random code testing section on the web.

from keras.datasets import mnist
from keras.utils import to_categorical

train_X, train_y = mnist.load_data()[0]
train_X = train_X.reshape(-1, 28, 28, 1)
train_X = train_X.astype('float32')
train_X /= 255
train_y = to_categorical(train_y, 10)

from keras.models import Sequential
from keras.layers import Conv2D, MaxPool2D, Flatten, Dropout, Dense
from keras.losses import categorical_crossentropy
from keras.optimizers import Adadelta

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'))

model.compile(loss=categorical_crossentropy,
             optimizer=Adadelta(),
             metrics=['accuracy'])

batch_size = 100
epochs = 8
model.fit(train_X, train_y,
         batch_size=batch_size,
         epochs=epochs)

test_X, test_y = mnist.load_data()[1]
test_X = test_X.reshape(-1, 28, 28, 1)
test_X = test_X.astype('float32')
test_X /= 255
test_y = to_categorical(test_y, 10)
loss, accuracy = model.evaluate(test_X, test_y, verbose=1)
print('loss:%.4f accuracy:%.4f' %(loss, accuracy))

***NOTE*** on the running interface
insert image description here
may appear that cusolver64_11.dll cannot be found.
You need to download it and throw it into D:\softwares\anaconda3\envs\tensorflow-keras-gpu\Library\bin.
Download address:
Link: https://pan.baidu.com/s/1gR_y2NdWrGdq5mCnM1gUpA
Extraction code: ibub

The result is as shown in the figure (this machine is 1660s).
insert image description here
If there is a running error, just find the cause one by one according to the error report
. The main problem is the problem between versions.

https://zhuanlan.zhihu.com/p/358529804

Guess you like

Origin blog.csdn.net/Yygj39/article/details/124273828