TensorFlow入门深度学习–05.多层感知器实现MNIST数据分类

文章列表
1.TensorFlow入门深度学习–01.基础知识. .
2.TensorFlow入门深度学习–02.基础知识. .
3.TensorFlow入门深度学习–03.softmax-regression实现MNIST数据分类. .
4.TensorFlow入门深度学习–04.自编码器(对添加高斯白噪声后的MNIST图像去噪).
5.TensorFlow入门深度学习–05.多层感知器实现MNIST数据分类.
6.TensorFlow入门深度学习–06.可视化工具TensorBoard.
7.TensorFlow入门深度学习–07.卷积神经网络概述.
8.TensorFlow入门深度学习–08.AlexNet(对MNIST数据分类).
9.TensorFlow入门深度学习–09.tf.contrib.slim用法详解.
10.TensorFlow入门深度学习–10.VGGNets16(slim实现).
11.TensorFlow入门深度学习–11.GoogLeNet(Inception V3 slim实现).

  • TensorFlow入门深度学习–05.多层感知器实现MNIST数据分类

TensorFlow入门深度学习–05.多层感知器实现MNIST数据分类

多层感知器(Multi Layer Perceptron,MLP),又称为全连接神经网络(Fully Connected Network,FCN),详细原理请参考【深度学习基础模型算法原理及编程实现–03.全链接】,但我们这里直接用TensorFlow来实现,由于省去了梯度反向传导计算的过程,工作量小了很多,详细代码如下所示。这里实现了含有一个隐藏层的神经网络,中间层的激活函数采用可解决梯度消失的ReLU,并采样Dropout技巧防止过拟合,输出层激活函数采用复合概率分布的softmax函数,结合交叉熵损失函数可以极大减小计算量。优化器采用具有自适应学习速率的Adagrad。需要注意的是,由于隐藏层的激活函数使用ReLU,为了打破完全对称且避免0梯度,隐藏层权重系数使用具有分布方差的正态分布来初始化,优势还需给偏置项赋一点初值来避免dead neuron,不过这里作用不明显。而输出层的激活函数为softmax,因此直接将输出的权重系数及偏置项都赋为0即可。运行下面的程序发现最后的准确率为0.9802,余前面的softmax-regression分类效果差不多。

from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
mnist = input_data.read_data_sets("./../MNISTDat", one_hot=True)
sess = tf.InteractiveSession()
# Create the model
in_units = 784
h1_units = 300
o_units = 10
batch_size = 100
W1 = tf.Variable(tf.truncated_normal([in_units, h1_units], stddev=0.1))
b1 = tf.Variable(tf.zeros([h1_units]))
W2 = tf.Variable(tf.zeros([h1_units, o_units]))
b2 = tf.Variable(tf.zeros([o_units]))
x = tf.placeholder(tf.float32, [None, in_units])
keep_prob = tf.placeholder(tf.float32)
hidden1 = tf.nn.relu(tf.matmul(x, W1) + b1)
hidden1_drop = tf.nn.dropout(hidden1, keep_prob)
y = tf.nn.softmax(tf.matmul(hidden1_drop, W2) + b2)
# Define loss and optimizer
y_ = tf.placeholder(tf.float32, [None, o_units])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
train_step = tf.train.AdagradOptimizer(0.3).minimize(cross_entropy)
# Train
tf.global_variables_initializer().run()
for i in range(3000):
  batch_xs, batch_ys = mnist.train.next_batch(batch_size)
  train_step.run({x: batch_xs, y_: batch_ys, keep_prob: 0.75})
# Test trained model
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(accuracy.eval({x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))

## 或者Train
#with tf.Session() as sess:
#    tf.global_variables_initializer().run()
#    sess.run(tf.global_variables_initializer())
#    for i in range(3000):
#      batch_xs, batch_ys = mnist.train.next_batch(batch_size)
#      train_step.run({x: batch_xs, y_: batch_ys, keep_prob: 0.75})
#      print(sess.run(W1[5,5:8]))
#    # Test trained model
#    correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
#    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
#    print(accuracy.eval({x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))

TensorFlow实现 :https://pan.baidu.com/s/14A91inmZmSC55dgDv8ZZ3Q

你可能感兴趣的:(深度学习,TensorFlow)