码迷,mamicode.com
首页 > 其他好文 > 详细

MNIST

时间:2018-04-19 15:42:09      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:开始   example   参数   迭代   mini   active   flat   span   ada   

 1 from tensorflow.examples.tutorials.minist import input_data
 2 import tensorflow as tf 
 3 minist=input_data.read_data_sets("MNIST_data/",one_hot=True)
 4 sess=tf.InteractiveSession()
 5 
 6 def weight_variable(shape):
 7     initial = tf.truncated_nomal(shape,stddev=0.1)
 8     return tf.Variable(initial)
 9 
10 def bias_variable(shape):
11     initial=tf.constant(0.1,shape=shape)
12     return tf.Variable(initial)
13 
14 def conv2d(x, W):#x代表输入,W代表卷积的参数
15     return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding=SAME)
16 
17 def max_pool_2x2(x):
18    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding=SAME)  
19 
20 x=tf.placeholder(tf.float32,[None,784])
21 y_=tf.placeholder(tf.float32,[None,10])
22 x_image=tf.reshape(x,[-1,28,28,1])
23 
24 W_conv1=weight_variable([5,5,1,32])#卷积核尺寸为5*5,,一个颜色通道,32个不同的卷积核,代表32个feature map
25 b_conv1=bias_variable([32])
26 h_conv1=tf.nn.relu(conv2d(x_image,W_conv1)+b_conv1)
27 h_pool1=max_pool_2*2(h_conv1)
28 
29 W_conv2=weight_variable([5,5,32,64])#卷积核尺寸为5*5,,一个颜色通道,64个不同的卷积核,代表64个feature map
30 b_conv2=bias_variable([64])
31 h_conv2=tf.nn.relu(conv2d(h_pool1,W_conv2)+b_conv2)
32 h_pool2=max_pool_2*2(h_conv2)  #此时输出的tensor为7*7*64
33 
34 W_fc1=weight_variable([7*7*64,1024])#FC层隐含节点为1024
35 b_fc1=bias_variable([1024])
36 h_pool2_flat=tf.reshape(h_pool2,[-1,7*7*64])#对第二个卷积层的输出tensor进行变形,将其转化为1D向量,然后连接fc1
37 h_fc1=tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1)+ b_fc1)
38 
39 keep_prob= tf.placeholder(tf.float32)
40 h_fc1_drop= tf.nn.dropout(h_fc1,keep_prob)
41 
42 #将dropout层的输出连接一个softmax层,得到最后的概率输出
43 W_fc2= weight_variable([1024,10])
44 b_fc2= bias_variable([10])
45 y_conv= tf.nn.softmax(tf.matmul(h_fc1_drop,W_fc2)+b_fc2)
46 #我们定义损失函数为cross entropy,优化器使用Adam,并给予一个比较小的学习速率1e-4
47 cross_entropy= tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y_conv),
48                                    reduction_indices=[1]))
49 train_step= tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
50 
51 correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
52 accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
53 
54 #下面开始训练过程
55 tf.global_variables_initializer().run()
56 for i in range(20000):   #共进行2000次训练迭代
57     batch= mnist.train.next_batch(50)    #mini_batch大小为50
58     if i%100==0:
59         train_accuracy= accuracy.eval(feed_dict={x:batch[0],y_:batch[1],
60                                                  keep_prob:1.0      })
61         print("step %d, training accuracy %g" %(i,train_accuracy)) 
62     train_step.run(feed_dict={x:batch[0],y_:batch[1],keep_prob:0.5})
63 print("test accuracy %g"%accuracy.eval(feed_dict={
64                         x:minist.test.image,y_:minist.test.labels,keep_prob:1.0}) 

 

MNIST

标签:开始   example   参数   迭代   mini   active   flat   span   ada   

原文地址:https://www.cnblogs.com/miya1028/p/8883220.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!