import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data/', one_hot=True) def compute_accuracy(v_xs,v_ys):
global prediction
y_pre=sess.run(prediction,feed_dict={xs:v_xs,keep_prob:})
correct_prediction=tf.equal(tf.argmax(y_pre,),tf.argmax(v_ys,))
accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
result=sess.run(accuracy,feed_dict={xs:v_xs,ys:v_ys})
return result def weight_varirable(shape):
inital=tf.truncated_normal(shape,stddev=0.1)
return tf.Variable(inital) 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=[,,,],padding='SAME') def max_poo_(x):
return tf.nn.max_pool(x, ksize=[,,,], strides=[,,,], padding='SAME') xs=tf.placeholder(tf.float32,[None,])
ys=tf.placeholder(tf.float32,[None,])
keep_prob=tf.placeholder(tf.float32) x_image=tf.reshape(xs,[-,,,]) W_conv1=weight_varirable([,,,])
b_conv1=bias_variable([])
h_conv1=tf.nn.relu(conv2d(x_image,W_conv1)+b_conv1)
h_pool1=max_poo_(h_conv1) W_conv2=weight_varirable([,,,])
b_conv2=bias_variable([])
h_conv2=tf.nn.relu(conv2d(h_pool1,W_conv2)+b_conv2)
h_pool2=max_poo_(h_conv2) W_fc1=weight_varirable([**,])
b_fc1=bias_variable([]) h_pool2_flat=tf.reshape(h_pool2,[-,**])
h_fcl=tf.nn.relu(tf.matmul(h_pool2_flat,W_fc1)+b_fc1)
h_fc1_drop=tf.nn.dropout(h_fcl,keep_prob) W_fc2=weight_varirable([,])
b_fc2=bias_variable([]) prediction=tf.nn.softmax(tf.matmul(h_fc1_drop,W_fc2)+b_fc2) cross_entropy=tf.reduce_mean(
-tf.reduce_sum(ys*tf.log(prediction),
reduction_indices=[])) train_step=tf.train.AdamOptimizer(1e-).minimize(cross_entropy) sess=tf.Session() sess.run(tf.global_variables_initializer()) for i in range():
batch_xs, batch_ys = mnist.train.next_batch()
sess.run(train_step, feed_dict={xs: batch_xs, ys: batch_ys,keep_prob:0.5})
if i % == :
print(compute_accuracy(
mnist.test.images, mnist.test.labels))

如果有同学没有MINST数据,请到http://wiki.jikexueyuan.com/project/tensorflow-zh/tutorials/mnist_download.html下载,或者QQ问我

最新文章

  1. Redis 的安装与使用(linux)
  2. JQuery图片切换动画效果
  3. 阿里云ecs云服务器安装wdcp控制面板教程
  4. SQL Server 最小化日志操作解析,应用
  5. hdu 2489 最小生成树状态压缩枚举
  6. Java [Leetcode 319]Bulb Switcher
  7. CICS定时
  8. maya绝招(21--40)
  9. Stack by pointer
  10. C++反汇编第五讲,认识多重继承,菱形继承的内存结构,以及反汇编中的表现形式.
  11. android企业级商城源码、360°全景图VR源码、全民直播源码等
  12. java虚拟机的内存分配与回收机制
  13. 关于ORACLE的SQL语句拼接、替换、截取、排序,联表等...~持续汇总~
  14. Python--抽象类接口类
  15. C#控件之ComboBox控件使用
  16. Mysql函数大全以及存储过程、函数、触发器、游标等等
  17. 计算机组成原理——主存与cache的映射关系
  18. 37 【kubernetes】搭建dashboard
  19. (网页)人人都会的35个Jquery小技巧
  20. 洛谷 P4151 [WC2011]最大XOR和路径 解题报告

热门文章

  1. Java之RabbitMQ(二)多mq配置
  2. nginx实用配置用例
  3. 最难的工作 /// SPFA模板 oj1396
  4. ztree 树状图——例
  5. java笔试之完全数计算
  6. netty 使用Java序列化
  7. vue中 给router-view 组件的 绑定 key 的原因
  8. Ubuntu vi命令
  9. Android开发 ShapeDrawable详解
  10. 转载别人的ftp,觉得目录结构不错,学习