二隐层的神经网络实现MNIST数据集分类
传统的人工神经网络包含三部分,输入层、隐藏层和输出层。对于一个神经网络模型的确定需要考虑以下几个方面:
- 隐藏层的层数以及各层的神经元数量
- 各层激活函数的选择
- 输入层输入数据的shape
- 输出层神经元的数量
以上神经网络的骨架确定之后,则相应的权重和偏置所对应的shape也随之确定,即网络结构的确定。
下面的代码是通过二隐层的神经网络实现MNIST手写数字的分类,下图为该神经网络的网络结构:
# 用两隐层神经网络实现手写数字(mnist)分类
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import numpy as np
import matplotlib.pyplot as pltmnist = input_data.read_data_sets('D:\MNIST_data', one_hot=True)n_hidden_1 = 256
n_hidden_2 = 128
n_input = 784
n_classes = 10# INPUT AND OUTPUT
x = tf.placeholder(tf.float32, [None, n_input])
y = tf.placeholder(tf.float32, [None, n_classes])# NETWORK PARAMETERS
stddev = 0.1
weights = {"w1": tf.Variable(tf.random_normal([n_input, n_hidden_1], stddev=stddev)),"w2": tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2], stddev=stddev)),"out": tf.Variable(tf.random_normal([n_hidden_2, n_classes], stddev=stddev))
}
biases = {"b1": tf.Variable(tf.zeros([n_hidden_1], tf.float32)),"b2": tf.Variable(tf.zeros([n_hidden_2], tf.float32)),"out": tf.Variable(tf.zeros([n_classes], tf.float32))
}def network(inputs, weights, biases):layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(inputs, weights['w1']), biases['b1']))layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1, weights['w2']), biases['b2']))pre = tf.matmul(layer_2, weights['out']) + biases['out']return prepred = network(x, weights, biases)cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=pred))
train = tf.train.GradientDescentOptimizer(0.001).minimize(cost)
acc = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(acc, tf.float32))init = tf.global_variables_initializer()train_step = 500
batch_size = 100
display_step = 10with tf.Session() as sess:sess.run(init)for k in range(train_step):loss = 0num_batch = int(mnist.train.num_examples/batch_size)for L in range(num_batch):batch_xs, batch_ys = mnist.train.next_batch(100)sess.run(train, feed_dict={x: batch_xs, y: batch_ys})_loss = sess.run(cost, feed_dict={x: batch_xs, y: batch_ys})loss += _lossif k % display_step == 0:_accuracy = sess.run(accuracy, feed_dict={x: mnist.test.images, y: mnist.test.labels})print('loss:%2f' % loss, ' accuracy:%2f' % _accuracy)
训练500次之后,测试精度为:
loss:167.206619 accuracy:0.916900