用tensorflow在mnist数据集上做测试
方法:softmax regression
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot = True)
输出:
Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Extracting MNIST_data/train-images-idx3-ubyte.gz
Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
print(mnist.train.images.shape, mnist.train.labels.shape)
print(mnist.test.images.shape, mnist.test.labels.shape)
print(mnist.validation.images.shape, mnist.validation.labels.shape)
(55000, 784) (55000, 10)
(10000, 784) (10000, 10)
(5000, 784) (5000, 10)
placeholder 第一个参数表示类型, 第二个参数中None代表不限条输入,784表示每条输入是一个784维向量
import tensorflow as tf
sess = tf.InteractiveSession()
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)
用交叉熵计算loss
先定义一个placeholder, 输入是真实的label, 用来计算cross-entropy.
1. tf.reduce_mean 对每个 batch 数据求均值
2. tf.reduce_sum 求和
y_ = tf.placeholder(tf.float32, [None, 10])
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)
注意
tf.global_variables_initializer() 是 tensorflow1.0.0 之后开始使用
tf.initialize_all_variables() 是 tensorflow1.0.0 之前的用法
tf.global_variables_initializer().run()
每次从训练集中抽出100个样本, 构成一个mini-batch, 并feed给placeholder, 然后调用train_step 对这些样本进行训练。
优点:
使用一小部分样本进行训练成为随机梯度下降(SGD), 与每次使用全部样本的传统的梯度下降对应。
如果每次训练都是用全部样本,计算量太大,有时也不容易跳出局部最优,因此,对于大部分机器学习问题,我们都只是用一小部分数据进行随机梯度下降, 这种做法绝大多数时候回避全体样本训练的收敛速度很多
for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
train_step.run({x:batch_xs, y_:batch_ys})
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}))
0.9169
流程共分为4部分:
- 定义算法公式
- 定义loss, 选定优化器,并指定优化器优化loss,
- 迭代对数据进行训练,
- 在测试集或验证集上对数据进行评估