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