Tensorflow实战(一)(MNIST手写字体识别TensorFlow实现)

来源:互联网 发布:2017淘宝618活动时间 编辑:程序博客网 时间:2024/06/05 08:37

笔者开始连载tensorflow教程,从MNIST实现开始,重点是在代码的实现,个人感觉把代码实现一遍,比看更加学得更加明白。

import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_datamnist = input_data.read_data_sets('./data/',one_hot=True)print(mnist.validation.num_examples)print(mnist.train.num_examples)print(mnist.test.num_examples)x = tf.placeholder(tf.float32,shape=[None,784])labels = tf.placeholder(tf.float32, [None, 10])x_input = tf.reshape(x, [-1,28,28,1])w1 = tf.Variable(tf.truncated_normal(shape=[3,3,1,32],stddev=0.05),name='w1')b1 = tf.Variable(tf.zeros(32),name='b1')conv1 = tf.nn.conv2d(x_input,w1,strides=[1,1,1,1],padding='SAME',name='conv1')relu1 = tf.nn.relu(tf.add(conv1,b1),name='relu1')w2 = tf.Variable(tf.truncated_normal(shape=[3,3,32,64],stddev=0.05),name='w2')b2 = tf.Variable(tf.zeros(64),name='b2')conv2 = tf.nn.conv2d(conv1,w2,strides=[1,2,2,1],padding='SAME',name='conv2')relu2 = tf.nn.relu(conv2+b2,name='relu2')w3 = tf.Variable(tf.truncated_normal(shape=[3,3,64,128],mean=0,stddev=0.05),name='w3')b3 = tf.Variable(tf.zeros(128),name='b3')conv3 = tf.nn.conv2d(conv2,w3,strides=[1,2,2,1],padding='SAME',)relu3 = tf.nn.relu(conv3+b3,name='relu3')x_flat = tf.reshape(relu3,shape=[-1,7*7*128])w_fc1=tf.Variable(tf.truncated_normal(shape=[7*7*128,1024],stddev=0.05,mean=0),name='w_fc1')b_fc1 = tf.Variable(tf.zeros(1024),name='b_fc1')fc1 = tf.add(tf.matmul(x_flat,w_fc1),b_fc1)relu_fc1 = tf.nn.relu(fc1,name='relu_fc1')keep_prob = tf.placeholder(tf.float32)drop_1 = tf.nn.dropout(relu_fc1,keep_prob=keep_prob,name='drop_1')bn_fc1 = tf.layers.batch_normalization(drop_1,name='bn_fc1')w_fc2 = tf.Variable(tf.truncated_normal(shape=[1024,512],stddev=0.05,mean=0),name='w_fc2')b_fc2 = tf.Variable(tf.zeros(512),name='b_fc2')fc2 = tf.add(tf.matmul(bn_fc1,w_fc2),b_fc2)relu_fc2 = tf.nn.relu(fc2,name='relu_fc2')drop_2 = tf.nn.dropout(relu_fc2,keep_prob=keep_prob,name='drop_2')bn_fc2 = tf.layers.batch_normalization(drop_2,name='bn_fc3')w_fc3 = tf.Variable(tf.truncated_normal(shape=[512,10],stddev=0.05,mean=0),name='w_fc3')b_fc3 = tf.Variable(tf.zeros(10),name='b_fc3')fc3 = tf.add(tf.matmul(bn_fc2,w_fc3),b_fc3)model_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=fc3,labels=labels))train_opt = tf.train.AdamOptimizer(learning_rate=0.1).minimize(model_loss)correct_prediction = tf.equal(tf.argmax(fc3,1), tf.argmax(labels,1))accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))num_batches = 800batch_size = 64learning_rate = 0.002keep_pro = 0.75train_opt = tf.train.AdamOptimizer(learning_rate).minimize(model_loss)with tf.Session(config=tf.ConfigProto(device_count={'gpu':0})) as sess:    sess.run(tf.global_variables_initializer())    for batch_i in range(num_batches):        batch_xs,batch_ys = mnist.train.next_batch(batch_size)        sess.run(train_opt,{x:batch_xs,labels:batch_ys,keep_prob:keep_pro})        if batch_i % 100 == 0:            loss,acc = sess.run([model_loss,accuracy],{x:mnist.validation.images,labels:mnist.validation.labels,                                                      keep_prob:keep_pro})            print('Batch: {:>2}: Training loss: {:>3.5f}, Training accuracy: {:>3.5f}'.format(batch_i, loss, acc))        print('Final validation accuracy: {:>3.5f}'.format(acc))        acc = sess.run(accuracy, {x: mnist.validation.images,labels: mnist.validation.labels,keep_prob:keep_pro})        print('Final test accuracy: {:>3.5f}'.format(acc))        correct = 0        for i in range(100):            correct += sess.run(accuracy,feed_dict={x: [mnist.test.images[i]],labels: [mnist.test.labels[i]]                                                    ,keep_prob:keep_pro})        print("Accuracy on 100 samples:", correct/100)
阅读全文
0 0
原创粉丝点击