使用tensorflow1.4版本来用卷积神经网络实现mnist数据集分类

因为实验需要,必须练习1.4版本,没办法

注释都加在里面了

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse
import tensorflow as tf
import sys
import tempfile
from tensorflow.examples.tutorials.mnist import input_data

FLAGS=None

#定义的初始化函数
#权重值初始化函数
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')

#定义池化,选用2*2的窗口
def max_pool_2x2(x):
    return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')



#定义卷积神经网络
#分4步,输入层,隐藏层,输出层,定义损失函数
#隐藏层构建 卷积层1-池化层1-卷积层2-池化层2-全连接层-舍弃层
def CNN_mnist(x):
    with tf.name_scope('reshape'):
        x_image=tf.reshape(x,[-1,28,28,1]) #-1的意思是样本数量不固定
    #定义卷积层1和池化层1
    with tf.name_scope('conv1'):
        w_conv1=weight_Variable([3,3,1,32]) #3*3的卷积核,黑白图为1,卷积核个数一般为2的幂,这里选择32
        b_conv1=bias_Variable([32])
        h_conv1=tf.nn.relu(conv2d(x_image,w_conv1)+b_conv1)
    with tf.name_scope('pool1'):
        h_pool1=max_pool_2x2(h_conv1)
    

    #定义卷积层2和池化层2
    with tf.name_scope('conv2'):
        w_conv2=weight_Variable([3,3,32,64]) #32是指这里的输入值是池化层1的输出值,卷积核个数增加为64个
        b_conv2=bias_Variable([64])
        h_conv2=tf.nn.relu(conv2d(h_pool1,w_conv2)+b_conv2)
    with tf.name_scope('pool2'):
        h_pool2=max_pool_2x2(h_conv2)

    #构建全连接神经网络,将两次池化后的特征图转换为1D向量,隐含节点1024由自己定义
    with tf.name_scope('fc1'):
        w_fc1=weight_Variable([7*7*64,1024]) #1024是假定后续dropout的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)

    #构建dropout层
    with tf.name_scope('dropout'):
        keep_prob=tf.placeholder(tf.float32)
        h_fc1_drop=tf.nn.dropout(h_fc1,keep_prob)

    #输出层
    with tf.name_scope('fc2'):
        w_fc2=weight_Variable([1024,10]) #输入值为dropout层的输出值,有1024个节点,输出是mnist0-9共10个分类
        b_fc2=bias_Variable([10])
        y_conv=tf.matmul(h_fc1_drop,w_fc2)+b_fc2
        print('CNN READY')
    return y_conv,keep_prob

def main(_):
    #加载mnist数据
    mnist = input_data.read_data_sets('./data/',one_hot=True)

    trainimg=mnist.train.images
    trainlabel=mnist.train.labels
    testimg=mnist.test.images
    testlabel=mnist.test.labels
    print('mnist ready')

    sess = tf.InteractiveSession()
    n_input=784 #28*28像素
    n_output=10 #0-9个分类

    x = tf.placeholder(tf.float32, [None, n_input]) 
    y_ = tf.placeholder(tf.float32, [None, n_output])

    y_conv, keep_prob = CNN_mnist(x)

    #定义损失函数
    with tf.name_scope('loss'):
        cross_entropy=tf.nn.softmax_cross_entropy_with_logits(logits=y_conv,labels=y_)
         #logits就是预测的,输出层输出的,labels就是本来的标签
        cross_entropy=tf.reduce_mean(cross_entropy)

    #定义优化器
    with tf.name_scope('adam_optimizer'):
        train_step=tf.train.AdamOptimizer(0.001).minimize(cross_entropy)

    #定义评估准确率的操作
    with tf.name_scope('accuracy'):
        correct_prediction=tf.equal(tf.argmax(y_conv,1),tf.argmax(y_,1))
        correct_prediction=tf.cast(correct_prediction,tf.float32)
        accuracy=tf.reduce_mean(correct_prediction)
    
    #初始化所有参数
    init=tf.global_variables_initializer()
    with tf.Session() as sess:
        sess.run(init)
        train_epoch=1000
        batch_size=100 #每进行1次迭代选择100个样本
        display_step=100
        for i in range(1000):
            avg_cost=0
            total_batch=int(mnist.train.num_examples/batch_size)
            batch=mnist.train.next_batch(batch_size) #通过next_batch()就可以一个一个batch的拿数据
            train_step.run(feed_dict={x:batch[0],y_:batch[1],keep_prob:0.5})

            #每进行100次训练,就使用测试集对准确率进行测试
            if i%display_step==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))        
                test_accuracy = accuracy.eval(feed_dict={x:mnist.test.images, 
                                                 y_:mnist.test.labels, keep_prob:1.0})
                print('test accuracy %g' % (test_accuracy))

if __name__ == '__main__':
  parser = argparse.ArgumentParser()
  parser.add_argument('--data_dir', type=str,
                      default='/tmp/tensorflow/mnist/input_data',
                      help='Directory for storing input data')
  FLAGS, unparsed = parser.parse_known_args()
  tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值