MNIST for beginner
实质:单层softmax进行10分类
单张图片大小为28x28,拉成784维的列向量。对应的标签为10维的列向量,表示该张图片所属的类别(如果图片表示数字9,属于第十类,则第十维为1,其他九维都为0)。然后我们对输入图片的每个像素点都进行线性加权并加上偏置。所有参数及对应的维度如下所示:
- 输入: x (784, 1)
- 权重:
W (10, 784)- 偏置: b (10, 1)
- 标签:
y′ (10, 1)
具体实现细节
对于单张图片,表示数字i的证据为
evidencei=∑jWi,jxj+bi
将所有证据转化成概率
y=softmax(Wx+b)
其中softmax定义为 softmax(x)=exp(xi)∑jexp(xj)
误差表示——交叉熵
cross_entropy=−∑iy′ilogyi
batch loss(多样本)
loss=mean(∑jcross_entropyj)
最后采用随机梯度下降法训练,代码如下:
import tensorflow as tf
# import data
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
# model
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, W) + b)
y_ = tf.placeholder(tf.float32, [None, 10])
# loss function & training method
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices = [1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
# 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))
# train
sess = tf.Session() # launch the session
sess.run(tf.initialize_all_variables()) # initialize
for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict = {x: batch_xs, y_: batch_ys})
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
训练环境
name: TITAN X (Pascal)
major: 6 minor: 1 memoryClockRate (GHz) 1.531
pciBusID 0000:02:00.0
Totamory: 11.90GiB
Free memory: 11.75GiB
结果(测试准确率)
0.9165