先弄懂卷积神经网络的原理,推荐这两篇博客:http://blog.csdn.net/yunpiao123456/article/details/52437794   http://blog.csdn.net/qq_25762497/article/details/51052861#%E6%A6%82%E6%8F%BD

简单的测试程序如下(具体各参数代表什么可以百度):

 from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf mnist=input_data.read_data_sets("MNIST_data/",one_hot=True)
sess=tf.InteractiveSession() 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) def conv2d(x,w):
return tf.nn.conv2d(x,w,strides=[1,1,1,1],padding='SAME') def max_pool_2x2(x):
return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME') x=tf.placeholder(tf.float32,[None,784])
y_=tf.placeholder(tf.float32,[None,10])
x_image=tf.reshape(x,[-1,28,28,1]) w_conv1=weight_variable([5,5,1,32])
b_conv1=bias_variable([32])
h_conv1=tf.nn.relu(conv2d(x_image,w_conv1)+b_conv1)
h_pool1=max_pool_2x2(h_conv1) 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) 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) keep_prob=tf.placeholder(tf.float32)
h_fc1_drop=tf.nn.dropout(h_fc1,keep_prob) 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) cross_entropy=tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y_conv),reduction_indices=[1]))
train_step=tf.train.AdamOptimizer(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,tf.float32)) tf.initialize_all_variables().run()
for i in range(20000):
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,training 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(feed_dict={x:mnist.test.images,y_:mnist.test.labels,keep_prob:1.0}))

运行结果:

最新文章

  1. 替换CENTOS自带的yum源为网易163镜像源
  2. jQuery回调、递延对象总结(下篇) —— 解密jQuery.when方法
  3. ASP.NET后台JS弹框使前台页面样式丢失 解决办法
  4. R(二): http与R脚本通讯环境安装
  5. MFC ListControl用法
  6. Android Studio无法启动 打开, Android Studio gradle下载不了
  7. JAVA加密算法系列-AesEBC
  8. Asp.net MVC 如何对所有用户输入的字符串字段做Trim处理
  9. [CQOI2018]异或序列
  10. 设置LISTControl控件某一行的背景和文字颜色
  11. 个人小爱好:Operating System:three easy pieces---第6章第4节_担心并发问题?
  12. Mp4 to Img
  13. Win7下无法启动sql server fulltext search (mssqlserver)的问题
  14. FIO_工具_专业
  15. java编译 Error: Could not find or load main class java执行包main方法
  16. Unity3D Shader 学习笔记(二):ShaderLab的结构和基本属性
  17. SolrJ API 官方文档最佳实践
  18. VC线程同步方法
  19. hdu 5017
  20. android自定义控件(1)-自定义控件属性

热门文章

  1. lambda,map,filter,reduce
  2. selenium+phantomjs解析JS
  3. codefirst 关系处理
  4. SpringMVC——概述
  5. (BST)升序数组变为BST树
  6. Position Independent Code (PIC) in shared libraries on x64
  7. Debian7安装后的配置(英文环境chromium浏览器中汉字变成方块的问题)
  8. 停止Nginx服务
  9. redis集群如何解决重启不了的问题
  10. birth