TensorFlow-深度学习-08-人工神经网络(ANN)-多层感知器(MLP)

本文与TensorFlow-深度学习-03-梯度下降(反向传播–BP)其实没有什么差别,主要差别在于使用了softmax函数进行梯度下降求解。

实例:

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets("mnist/", one_hot=True)


def ann_demo():
    hidden_num = 50
    x = tf.placeholder(shape=[None, 784], dtype=tf.float32)
    y = tf.placeholder(shape=[None, 10], dtype=tf.float32)

    '''----------第一层隐层----------'''
    w1 = tf.Variable(tf.random_normal(shape=[784, hidden_num]), dtype=tf.float32)
    b1 = tf.Variable(tf.random_normal(shape=[1, hidden_num]), dtype=tf.float32)


    '''---------第二层隐层-----------'''
    w2 = tf.Variable(tf.random_normal(shape=[hidden_num, 10]), dtype=tf.float32)
    b2 = tf.Variable(tf.random_normal(shape=[1, 10]), dtype=tf.float32)

    nn1 = tf.add(tf.matmul(x, w1), b1)
    out1 = tf.nn.softmax(nn1)

    nn2 = tf.add(tf.matmul(out1, w2), b2)
    out = tf.nn.softmax(nn2)  # 使用激活函数输出预测值


    diff = tf.subtract(y, out)  # 真实值和预测值之间的差值
    loss = tf.reduce_sum(tf.square(diff))  # 把差值平方

    step = tf.train.GradientDescentOptimizer(learning_rate=0.05).minimize(loss)

    acc_mat = tf.equal(tf.argmax(y, 1), tf.argmax(out, 1))
    acc_ret = tf.reduce_sum(tf.cast(acc_mat, dtype=tf.float32))
    init = tf.global_variables_initializer()

    with tf.Session() as sess:
        sess.run(init)
        batch_size = 128
        for i in range(20000):
            batch_xs, batch_ys = mnist.train.next_batch(batch_size)
            sess.run(step, feed_dict={x: batch_xs, y: batch_ys})
            if (i + 1) % 2000 == 0:
                curr_acc, curr_loss = sess.run([acc_ret, loss],
                                               feed_dict={x: mnist.test.images[:1000], y: mnist.test.labels[:1000]})
                print("curr_acc:", curr_acc/10,"%", "curr_loss:", curr_loss)


if __name__ == "__main__":
    ann_demo()

运行结果:
在这里插入图片描述
结果其实并不是很尽如人意,因为对于简单的人工神经网络来说,一般3层就是最多的,训练次数也会随着loss函数的左右移动而达到一个稳态,训练就会终止。因此,对于大型的数据集,最好不要使用简单的人工神经网络来训练。

发布了88 篇原创文章 · 获赞 39 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/Daker_Huang/article/details/88725181