本文中的Cuda和Cudnn安装均采用conda安装方法,无需单独下载安装Cuda、Cudnn等软件。
以下教程是建立在已经安装好Anaconda的基础上。
系统:Windows10
Anaconda:2020.07
python 3.8.0
cuda版本:cuda 11.0
cudnn版本:cudnn 8.1
Tensorflow版本:tensorflow-gpu 2.4.0
keras版本:keras-2.4.0
1、在安装的时候勾选添加即可
2、自己配置:电脑->属性->高级系统设置->环境变量->系统变量,找到Path,加入三个文件夹的存储路径,如下图。
这里添加的是阿里的源,free和main是必选,其它可选
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
右键NVIDIA控制面板->帮助->组件。如下图,切记不要超过该版本
conda create -n tensorflow-keras-gpu python=3.8
conda activate tensorflow-keras-gpu
https://tensorflow.google.cn/install/source_windows#gpu
这里安装了cuda11.0和cudnn8.1,由于先前的操作中存在版本的不配套,需要升降不同的版本
conda install cudatoolkit=11.0
conda install cudnn=8.1
1、tensorflow-gpu可以直接通过conda安装。
pip install tensorflow-gpu==2.4.0
2、由于选择了先conda安装cuda和cudnn,再通过whl文件安装tensorflow-gpu(三者的版本要对应,可参考官方测试过的配置)
下载tensorflow_gpu-2.4.0的whl文件后,输入下面代码安装:
pip install + whl文件所在位置,例如
pip install C:\Users\XXX\Desktop\tensorflow_gpu-2.4.0-cp38-cp38-win_amd64.whl
https://tensorflow.google.cn/install/pip#package-location
通过pip或者conda直接安装,注一定要选择版本号,切勿默认,如果错误,可自行升降
pip install keras==2.4.0
conda install keras==2.4.0
网上随机的代码测试部分。
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))
运行界面
***注***可能会出现cusolver64_11.dll找不到的情况
需要将其下载丢入D:\softwares\anaconda3\envs\tensorflow-keras-gpu\Library\bin中
下载地址:
链接:https://pan.baidu.com/s/1gR_y2NdWrGdq5mCnM1gUpA
提取码:ibub
结果如图(本机为1660s)
如出现运行错误,根据报错一一查找原因即可
主要问题还是各版本之间的问题。
https://zhuanlan.zhihu.com/p/358529804