#load MNIST data
import tensorflow.examples.tutorials.mnist.input_data as input_data
mnist = input_data.read_data_sets("MNIST_data/",one_hot=True) #start tensorflow interactiveSession
import tensorflow as tf
sess = tf.InteractiveSession() #weight initilization
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial) def bias_variable(shape):
initial = tf.constant(0.1, shape= shape)
return tf.Variable(initial) #convolution
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1,1,1,1], padding='SAME') #pooling
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1,2,2,1],strides=[1,2,2,1], padding='SAME') #Create the model
#placeholder
x = tf.placeholder("float",[None, 784])
y_ = tf.placeholder("float", [None, 10]) #variable
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10])) y = tf.nn.softmax(tf.matmul(x,W) +b) #first convolutional layer
w_conv1 = weight_variable([5,5,1,32])
b_conv1 = bias_variable([32]) x_image = tf.reshape(x,[-1,28,28,1]) h_conv1 =tf.nn.relu(conv2d(x_image,w_conv1) + b_conv1)
h_pool1 =max_pool_2x2(h_conv1) #second convolutional layer
w_conv2 = weight_variable([5,5,32,64])
b_conv2 = bias_variable([64]) h_conv2 =tf.nn.relu(conv2d(h_pool1, w_conv2) + b_conv2)
h_pool2 =max_pool_2x2(h_conv2) #densely connected layer
w_fc1 = weight_variable([7*7*64, 1024])
b_fc1 = bias_variable([1024]) h_pool2_flat = tf.reshape(h_pool2, [-1,7*7*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, w_fc1) + b_fc1) #dropout
keep_prob = tf.placeholder("float")
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) #readout layer
w_fc2 = weight_variable([1024,10])
b_fc2 = bias_variable([10]) y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop,w_fc2) + b_fc2) #train and evaluate the model
cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))
#train_step = tf.train.AdagradOptimizer(1e-4).minimize(cross_entropy)
train_step = tf.train.GradientDescentOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
sess.run(tf.initialize_all_variables())
for i in range(5000):
batch = mnist.train.next_batch(50)
if i%100 ==0:
train_accuracy = accuracy.eval(feed_dict={x:batch[0],y_:batch[1], keep_prob:1.0})
print "step %d, train accuracy %g " %(i,train_accuracy)
train_step.run(feed_dict={x:batch[0],y_:batch[1], keep_prob:0.5}) print "test accuracy %g" % accuracy.eval(fedd_dict={x:mnist.test.images, y_:mnist.test.labels, keep_prob:1.0})

同样是极客学院的课程,其实也是翻译的国外的robot-ai博客上的内容,但是这个博客,现在打不开了,可能是墙的问题?没有太深究。

按照作者的说法,是采用自适应下降的方式,在train阶段能达到99%的正确率,但是,我的结果只有93%左右,修改梯度步长到1e-4也只有94% 。因此尝试换用原来的梯度下降方式,反而能获得97.61%的正确率,在训练中还达到过98%,这个问题比较无奈,修改步长的结果提升也并不明显。有人在评论中说在不同的平台上测试的值不同,比如在纯CPU环境,和我的结果比较相似。在K20环境中能达到99%,这个问题留待以后探索。代码参考至:文章链接: http://blog.csdn.net/yhl_leo/article/details/50624471

最新文章

  1. Sprint评审会议不是Sprint演示会议
  2. Swift 自定义Subscript
  3. Entity Framework 第一篇
  4. DB Create and Insert
  5. React表单组件自定义-可控及不可控组件
  6. 再次复习数据结构:c语言链表的简单操作
  7. 10分钟了解JSON Web令牌(JWT)
  8. markdown test2
  9. Vue插件plugins的基本操作
  10. windows SysinternalsSuite
  11. node启动服务报错Node.js Error: Cannot find module express
  12. Linux使用NFS服务实现远程共享
  13. wireshark抓取本地数据包
  14. Android NDK学习(3)使用Javah命令生成JNI头文件 .
  15. [转]RedHat Enterprise Linux 7关闭防火墙方法
  16. 【模考】2018.04.08 Travel
  17. js常用的事件对象
  18. 关闭window端口445
  19. Django教程:[33]从数据库生成模型
  20. Git 命令 操作

热门文章

  1. php改变header头返回值
  2. oracle中删除某个用户下的所有表
  3. JDK压缩指针
  4. 四、SpringBoot出现报错:java.lang.NoSuchMethodError: org.springframework.http.MediaType.equalsTypeAndSubtype(Lorg/springframework/util/MimeType;)Z
  5. C#类型转换类(通用类)
  6. Dev中GridView——背景颜色改变
  7. 一张图搞懂Ajax原理
  8. linux常用命令(15)whereis命令
  9. ELK 日志平台构建
  10. iOS实现渐变颜色