练习编写神经网络对MNIST_data尝试

在学习深度学习的过程中,激起了亲手编写网络的欲望,由于自己的水平所限,只能从简单的网络做起,
下边是我使用tensorflow编写的简单网络,mnist数据集的图片大小是28*28*1,我就设计了两个卷积层,3*3的卷积核,
步长为2,一个步长为2的2*2的最大池化层,这些整完后,我发现网络的输出的长宽大小是4*4,于是就在
后边接了两层的全连接,两层全连接中间使用dropout,防止过拟合;损失函数采用平方损失,优化器是选
用随机梯度下降优化(0.1),这样简易的小网络就写出来了。最后生成网络结构可以使用tensorboard查看。
#导入包
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

#导入数据
mnist = input_data.read_data_sets("MNIST_data/",one_hot=True) 
batch_size = 100
n_batch = mnist.train.num_examples // batch_size



#定义输入变量
with tf.name_scope("input_data"):
    x_input = tf.placeholder(tf.float32,[None,784],name="x")
    y_input = tf.placeholder(tf.float32,[None,10],name="y")
    keep_pro = tf.placeholder(tf.float32,name="keep_pro")
    #将输入的数据重塑成图片,大小为28*28*1
    with tf.name_scope("x_images"):
        x_images = tf.reshape(x_input,[-1,28,28,1],name="x_images")
        
        
        
        

#定义变量
def variables_summary(var):
    with name_scope("summary"):
        mean=tf.reduce_mean(var,name="mean")
        tf.summary.scalar("mean",mean)
        with tf.name_scope("stddev"):
            stddev = tf.sqrt(tf.reduce_mean(tf.square(mean-var)))
        tf.summary.scalar("stddev",stddev)
        tf.summary.scalar("max",tf.reduce_max(var))
        tf.summary.scalar("min",tf.reduce_min(var))
        tf.summary.histogram("histogram",var)
        
#定义权重函数
def weights(shape,name):
	#用截断正态分布为权值做初始赋值
    init_w = tf.truncated_normal(shape,stddev=0.1)
    return tf.Variable(init_w,name=name)

#定义偏置
def bias(shape,name):
	#初始赋值
    init_bias = tf.constant(0.1,shape=shape)
    return tf.Variable(init_bias,name=name)
                
#定义卷积
def conv2d(x,w,name):
    return tf.nn.conv2d(x,w,strides=[1,2,2,1],padding="SAME",name=name)
        
#定义池化层
def max_pool(value,name):
    return tf.nn.max_pool(value,ksize=[1,2,2,1],strides=[1,2,2,1],padding="SAME",name=name)
        


    
#定义第一层卷积操作
with tf.name_scope("conv_1"):
    with tf.name_scope("weight_1"):
        ws_1 = weights([3,3,1,50],name="weight_11")
    with tf.name_scope("bias_1"):
        bias_1 = bias([50],name="bias_11")
    with tf.name_scope("conv_11"):
        conv_1 = conv2d(x_images,ws_1,name="conv_11")+bias_1
    with tf.name_scope("relu"):
        relu_1 = tf.nn.relu(conv_1)
        
        
#定义第一层池化层
with tf.name_scope("pool_1"):
    pool_1 = max_pool(relu_1,name="pool_11")
        
        
        
        
#定义第二层卷积层

with tf.name_scope("cov_2"):
    with tf.name_scope("weight_2"):
        ws_2 = weights([3,3,50,150],name="weight_22")
    with tf.name_scope("bias_2"):
        bias_2 = bias([150],'bias_22')
    with tf.name_scope("conv_22"):
        conv_2 = conv2d(pool_1,ws_2,name="conv22")+bias_2
    with tf.name_scope("relu_2"):
        relu_2 = tf.nn.relu(conv_2)
    
            
            
#定义第一个全连接层
with tf.name_scope("fc_1"):
    #将relu_2的输出重塑成[4*4*150]
    with tf.name_scope("relu_2_reshape"):
        relu_2_reshape = tf.reshape(relu_2,[-1,4*4*150])
    with tf.name_scope("fc_w_1"):
        fc_w_1 = weights([4*4*150,500],name="fc_w_11")
    with tf.name_scope("fc_b_1"):
        fc_b_1 = bias([500],name="fc_b_11")
        
    with tf.name_scope("fc_matmul_1"):
        fc_matmul_1 = tf.matmul(relu_2_reshape,fc_w_1)+fc_b_1
    with tf.name_scope("relu"):
        fc1_relu = tf.nn.relu(fc_matmul_1)
    with tf.name_scope("drop_out"):
        fc_drop_out = tf.nn.dropout(fc1_relu,keep_pro,name="drop_out")
            
            
            
#定义第二层全连接
with tf.name_scope("fc_2"):
    with tf.name_scope("fc_w_2"):
        fc_w_2 = weights([500,10],name="fc_w_22")
    with tf.name_scope("fc_b_2"):
        fc_b_2 = bias([10],name="fc_b_22")
    with tf.name_scope("fc_matmul_2"):
        fc_matmul_2 = tf.matmul(fc_drop_out,fc_w_2)+fc_b_2
    with tf.name_scope("softmax"):
        prediction = tf.nn.softmax(fc_matmul_2)
    
                  
            
            
#定义损失函数
with tf.name_scope("cross_entropy"):
    loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=y_input,logits=prediction))
    tf.summary.scalar("loss",loss)        
        
        
#定义优化器和训练
with tf.name_scope("train"):
    train = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

    
#定义准确率
with tf.name_scope("accuracy"):
    with tf.name_scope("correct_prediction"):
        #结果是一个布尔列表
        correct_prediction = tf.equal(tf.argmax(y_input,1),tf.argmax(prediction,1))
    with tf.name_scope("get_accuracy"):
        accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
        tf.summary.scalar("accuracy",accuracy)
        
init = tf.global_variables_initializer()
merged = tf.summary.merge_all()



#定义会话
with tf.Session() as sess:
    sess.run(init)
    train_writer = tf.summary.FileWriter("/logdir/train/",sess.graph)    
    test_writer = tf.summary.FileWriter("/logdir/test/",sess.graph)
    for i in range(2001):
        batch_xs,batch_ys = mnist.train.next_batch(batch_size)
        sess.run(train,feed_dict={x_input:batch_xs,y_input:batch_ys,keep_pro:0.5})
        
        #summary,_ = sess.run([merged,train],feed_dict={x_input:batch_xs,y_input:batch_ys,keep_pro:0.5})
        summary= sess.run(merged,feed_dict={x_input:batch_xs,y_input:batch_ys,keep_pro:0.5})
        train_writer.add_summary(summary,i)
            
        #在测试集上
        batch_test_xs,batch_test_ys = mnist.test.next_batch(batch_size)
        summary = sess.run(merged,feed_dict={x_input:batch_test_xs,y_input:batch_test_ys,keep_pro:0.5})
        test_writer.add_summary(summary,i)
            
                                 
                                        
            #打印准确率
        if i%100==0:
            test_acc = sess.run(accuracy,feed_dict={x_input:mnist.test.images,y_input:mnist.test.labels,keep_pro:0.5})
            train_acc = sess.run(accuracy,feed_dict={x_input:mnist.train.images[:10000],y_input:mnist.train.labels[:10000],keep_pro:0.5})
            print ("Iter " + str(i) + ", Testing Accuracy= " + str(test_acc) + ", Training Accuracy= " + str(train_acc))

  


代码运行后的结果为:

Extracting MNIST_data/train-images-idx3-ubyte.gz
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
Iter 0, Testing Accuracy= 0.0907, Training Accuracy= 0.0899
Iter 100, Testing Accuracy= 0.1351, Training Accuracy= 0.1371
Iter 200, Testing Accuracy= 0.2109, Training Accuracy= 0.205
Iter 300, Testing Accuracy= 0.3097, Training Accuracy= 0.3058
Iter 400, Testing Accuracy= 0.4072, Training Accuracy= 0.4103
Iter 500, Testing Accuracy= 0.4989, Training Accuracy= 0.488
Iter 600, Testing Accuracy= 0.5402, Training Accuracy= 0.5422
Iter 700, Testing Accuracy= 0.5696, Training Accuracy= 0.5699
Iter 800, Testing Accuracy= 0.5989, Training Accuracy= 0.5958
Iter 900, Testing Accuracy= 0.6241, Training Accuracy= 0.6243
Iter 1000, Testing Accuracy= 0.6539, Training Accuracy= 0.6553
Iter 1100, Testing Accuracy= 0.6841, Training Accuracy= 0.6803
Iter 1200, Testing Accuracy= 0.7037, Training Accuracy= 0.6933
Iter 1300, Testing Accuracy= 0.7159, Training Accuracy= 0.7072
Iter 1400, Testing Accuracy= 0.7309, Training Accuracy= 0.7209
Iter 1500, Testing Accuracy= 0.7438, Training Accuracy= 0.7332
Iter 1600, Testing Accuracy= 0.7494, Training Accuracy= 0.7389
Iter 1700, Testing Accuracy= 0.7537, Training Accuracy= 0.7502
Iter 1800, Testing Accuracy= 0.7646, Training Accuracy= 0.7545
Iter 1900, Testing Accuracy= 0.7709, Training Accuracy= 0.7635
Iter 2000, Testing Accuracy= 0.774, Training Accuracy= 0.7663

从结果上来看并不好,但是却是编写网络的第一步,从结果可以看出,编写的网络没有问题,测试误差和训练误差基本相差不大,没有过拟合,结果不太好的原因可能是我训练的次数太少,网络层次比较粗糙。
这是一次伟大的尝试。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值