#硬件配置:
cpu: 锐龙1700x
显卡:GTX 1060
Windows 10 64 企业版
##安装cuda8.0
官网链接:https://developer.nvidia.com/cuda-80-ga2-download-archive
如果官方下载失败,则使用网盘资源:
链接:https://pan.baidu.com/s/1o9uby2m 密码:f2lp
注意!只能安装Cuda 8.0,安装其它版本就会GG:
使用tensorflow会出现类似下面的错误:
% (build_info.cudart_dll_name, build_info.cuda_version_number))
ImportError: Could not find 'cudart64_80.dll'. TensorFlow requires that this DLL be installed in a directory that is named in your %PATH% environment variable. Download and install CUDA 8.0 from this URL: https://developer.nvidia.com/cuda-toolkit
双击exe安装文件 安装即可。
这个是用来加速的包。
官网链接:
https://developer.nvidia.com/rdp/cudnn-download 需要注册NVIDA账号,免费不麻烦。
但是这个网页上下载东西经常gg
换成sourceforget: https://sourceforge.net/projects/cuda-dnn/
解压压缩包,把压缩包
├─bin
├─include
└─lib
└─x64
里面东西放在C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0 相应的文件夹下
把
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\bin
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\lib
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\include
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0
添加环境变量中。
1.安装3.5的python版本,在安装过程中勾选安装pip
2.安装tensorflow
pip3 install tensorflow-gpu1.3.0 tensorflow1.3.0 -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com
###mnist 手写体识别:
__author__ = 'jmh081701'
# encoding: utf-8
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial, name="W")
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial, name="bias")
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1,1,1,1], padding="SAME", name="conv2d")
def max_pool(x):
return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1], padding="SAME", name="pooled")
xs = tf.placeholder(tf.float32, [None, 784])
ys = tf.placeholder(tf.float32, [None, 10])
keep_prob = tf.placeholder(tf.float32)
x_image = tf.reshape(xs, [-1, 28,28, 1])
# conv_1 layer
with tf.name_scope('conv-layer-1'):
W_conv1 = weight_variable([5,5,1,32]) # outsize=32 : convolutions units
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) # 28 * 28 * 32
h_pooled_1 = max_pool(h_conv1) # 14*14*32
# conv_2 layer
with tf.name_scope('conv-layer-2'):
W_conv2 = weight_variable([5,5,32,64]) # outsize=64
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pooled_1, W_conv2) + b_conv2) # 14 * 14 *64
h_pooled_2 = max_pool(h_conv2) # 7 * 7 * 64
# func1 layer
with tf.name_scope('nn-layer-1'):
W_fun1 = weight_variable([7*7*64, 1024])
b_fun1 = bias_variable([1024])
h_pool2_flat = tf.reshape(h_pooled_2, [-1, 7*7*64])
h_fun2 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fun1) + b_fun1)
h_fun2_drop = tf.nn.dropout(h_fun2, keep_prob)
# func2 layer
with tf.name_scope('nn-layer-2'):
W_fun2 = weight_variable([1024, 10])
b_fun2 = bias_variable([10])
prediction = tf.nn.softmax(tf.matmul(h_fun2_drop, W_fun2) + b_fun2)
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction)))
train_step = tf.train.AdamOptimizer(1e-04).minimize(cross_entropy)
## accuracy
correct_prediction = tf.equal(tf.argmax(prediction, 1), tf.argmax(ys, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
import time
n_epochs = 15
batch_size = 100
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
st = time.time()
for epoch in range(n_epochs):
n_batch = int(mnist.train.num_examples / batch_size)
for i in range(n_batch):
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
s,l=sess.run([train_step,cross_entropy], feed_dict={xs: batch_xs, ys: batch_ys, keep_prob:0.6})
print(i,l)
print( 'epoch', 1+epoch, 'accuracy:', sess.run(accuracy, feed_dict={keep_prob:1.0, xs: mnist.test.images, ys: mnist.test.labels}))
end = time.time()
print ('*' * 30)
print ('training finish.\ncost time:', int(end-st) , 'seconds;\naccuracy:', sess.run(accuracy, feed_dict={keep_prob:1.0, xs: mnist.test.images, ys: mnist.test.labels}))
###正确运行结果:
epoch 1 accuracy: 0.959
epoch 2 accuracy: 0.9747
epoch 3 accuracy: 0.9806
epoch 4 accuracy: 0.9852
epoch 5 accuracy: 0.9865
epoch 6 accuracy: 0.9874
epoch 7 accuracy: 0.9888
epoch 8 accuracy: 0.9894
epoch 9 accuracy: 0.9896
epoch 10 accuracy: 0.9903
epoch 11 accuracy: 0.9893
epoch 12 accuracy: 0.9906
epoch 13 accuracy: 0.9904
epoch 14 accuracy: 0.9914
epoch 15 accuracy: 0.9906
******************************
training finish.
cost time: 84 seconds;
accuracy: 0.9906
果然牛逼!
注意!tensorflow不同版本需要使用不同版本的CUDA,安装时要注意这一点。