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