安装环境:笔记本电脑win10专业版操作系统 + 1060显卡
1、安装python3.5.2
安装anaconda3省事,从清华镜像下载:
https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-4.2.0-Windows-x86_64.exe
2、安装CUDA9
地址:https://developer.nvidia.com/cuda-90-download-archive
安装时会先选择解压位置作为临时存放,可以自己定义。
安装过程中可能会弹出以下警告:
这个是由于安装的显卡驱动版本高于CUDA9所自带的驱动版本号,点击继续即可。安装的时候建议选择
自定义
,不选VS的组件:
自己可以选择路径安装,结束。
3、下载安装CUNNN
2.下载cudnn,下载地址:https://developer.nvidia.com/cudnn,进去之后,找到对应的版本,点击Download,他会要你注册登录,然后填个问卷,才可以下载,这一步自行按照网站提示做就可以了。提交完问卷后,就可以点击下载了。
选择以下的版本均可。
下载完成后解压,把解压后的3个文件夹放入CUDA9安装对应的文件夹中。
这里我的CUDA9路径是自己定义的。
4、安装tensorflow-gpu V1.9.0
tensorflow-gpu 版本过高运行程序时,会报错。实测V1.11.0也是可以正常使用的。
pip install --upgrade --force-reinstall tensorflow-gpu==1.9.0 --user
5、安装Keras
pip install keras
6、测试代码
import numpy
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.utils import np_utils
from keras import backend as K
K.set_image_dim_ordering('th')
seed = 7
numpy.random.seed(seed)
# load data
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# reshape to be [samples][pixels][width][height]
X_train = X_train.reshape(X_train.shape[0], 1, 28, 28).astype('float32')
X_test = X_test.reshape(X_test.shape[0], 1, 28, 28).astype('float32')
X_train = X_train / 255
X_test = X_test / 255
# one hot encode outputs
y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)
num_classes = y_test.shape[1]
def baseline_model():
# create model
model = Sequential()
model.add(Conv2D(32, (5, 5), input_shape=(1, 28, 28), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
# Compile model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
# build the model
model = baseline_model()
# Fit the model
model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=10, batch_size=200, verbose=2)
# Final evaluation of the model
scores = model.evaluate(X_test, y_test, verbose=0)
print("Baseline Error: %.2f%%" % (100-scores[1]*100))
运行结果:
Train on 60000 samples, validate on 10000 samples
Epoch 1/10
- 3s - loss: 0.2256 - acc: 0.9357 - val_loss: 0.0779 - val_acc: 0.9755
Epoch 2/10
- 2s - loss: 0.0711 - acc: 0.9790 - val_loss: 0.0447 - val_acc: 0.9851
Epoch 3/10
- 2s - loss: 0.0509 - acc: 0.9845 - val_loss: 0.0431 - val_acc: 0.9860
Epoch 4/10
- 3s - loss: 0.0391 - acc: 0.9879 - val_loss: 0.0407 - val_acc: 0.9877
Epoch 5/10
- 2s - loss: 0.0326 - acc: 0.9898 - val_loss: 0.0346 - val_acc: 0.9884
Epoch 6/10
- 2s - loss: 0.0267 - acc: 0.9917 - val_loss: 0.0330 - val_acc: 0.9891
Epoch 7/10
- 2s - loss: 0.0218 - acc: 0.9932 - val_loss: 0.0357 - val_acc: 0.9887
Epoch 8/10
- 2s - loss: 0.0192 - acc: 0.9940 - val_loss: 0.0333 - val_acc: 0.9889
Epoch 9/10
- 3s - loss: 0.0154 - acc: 0.9950 - val_loss: 0.0308 - val_acc: 0.9895
Epoch 10/10
- 2s - loss: 0.0139 - acc: 0.9957 - val_loss: 0.0331 - val_acc: 0.9895
Baseline Error: 1.05%
如果数据下载不成功,可以手动下载mnist.npz
,并放在以下文件夹中。
https://s3.amazonaws.com/img-datasets/mnist.npz
此时可以看到确实用了GPU:
补充:
安装Mxnet框架
pip install –pre mxnet-cu90