zoukankan      html  css  js  c++  java
  • 手动设计神经网络进行MNIST分类

    前言:

    用手工设计的两层神经网络,经过200个epoch,最后得到0.9599,约0.96的精度

    正文

    import tensorflow as tf
    from tensorflow.examples.tutorials.mnist import input_data
    
    #载入数据集
    mnist = input_data.read_data_sets("MNIST_dataMNIST_data",one_hot=True)
    
    #每个批次的大小
    batch_size = 32
    #计算一共有多少个批次
    n_batch = mnist.train.num_examples // batch_size
    
    #定义两个placeholder
    x = tf.placeholder(tf.float32,[None,784])
    y = tf.placeholder(tf.float32,[None,10])
    y_ = tf.cast(tf.argmax(y,axis=1),tf.int32)
    #创建一个简单的神经网络
    W_1 = tf.Variable(tf.random_normal([784,120],dtype=tf.float32))
    b_1 = tf.Variable(tf.zeros([120]))
    h_1 = tf.nn.relu(tf.matmul(x,W_1)+b_1)
    
    W_2 = tf.Variable(tf.random_normal([120,10],dtype=tf.float32))
    b_2 = tf.Variable(tf.zeros([10]))
    prediction = tf.matmul(h_1,W_2)+b_2
    prediction_ = tf.nn.softmax(tf.matmul(h_1,W_2)+b_2)
    
    # #二次代价函数
    #loss = tf.reduce_mean(tf.square(y-prediction))
    #交叉熵损失函数
    #loss = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y_, logits=prediction_)
    loss = tf.losses.sparse_softmax_cross_entropy(labels=y_,logits=prediction)
    #loss = tf.reduce_mean(-tf.reduce_sum(y * tf.log(prediction),reduction_indices=[1]))  
    #使用梯度下降法
    train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)
    
    #初始化变量
    init = tf.global_variables_initializer()
    
    #结果存放在一个布尔型列表中
    correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(prediction_,1)) #返回最大值所在位置,1表示行的维度
    #求准确率
    accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
    
    with tf.Session() as sess:
        sess.run(init)
        for epoch in range(200):
            for batch in range(n_batch):
                batch_xs, batch_ys = mnist.train.next_batch(batch_size)
                sess.run(train_step, feed_dict={x:batch_xs,y:batch_ys})
                
            acc = sess.run(accuracy,feed_dict={x:mnist.test.images, y:mnist.test.labels})
            print("Iter"+str(epoch)+',Testing Accuracy'+str(acc))

    其中要注意的地方应该有:

    loss函数的计算,用了tf.losses.sparse_softmax_cross_entropy这个交叉熵损失函数,其中:

    labels_的输入是样本是真实标签,类似于[1,2,3,4,5,1,1,2....]这种,

    所以,MNIST的样本标签是one-hot形式的,要先用tf.argmax转换成上述形式;

    logits的输入类似于[1.22,4.23,2.45,...]这种,由于该函数会先进行logits-->softmax的计算,所以不用先把logits转换为softmax形式;

    人生苦短,何不用python
  • 相关阅读:
    HearthBuddy投降插件2019-11-01的使用
    正则表达式在线分析 regex online analyzer
    Tips to write better Conditionals in JavaScript
    The fileSyncDll.ps1 is not digitally signed. You cannot run this script on the current system.
    Cannot capture jmeter traffic in fiddler
    JMETER + POST + anti-forgery token
    input type color
    HearthBuddy修改系统时间
    What are all the possible values for HTTP “Content-Type” header?
    UDK性能优化
  • 原文地址:https://www.cnblogs.com/yqpy/p/11157491.html
Copyright © 2011-2022 走看看