Tensorflw快速入门二

手写数字识别

  • 运用逻辑回归算法对手写数字进行识别

上篇博文我们用tensorflow实现了线性回归
Tensorflow快速入门——线性回归
今天我们运用逻辑回归算法对手写数字进行识别
本文具体数据集与源代码可从我的GitHub地址获取
https://github.com/liuzuoping/Deep_Learning_note

MNIST 数据集简介

手写数字识别也是比较常见的例子了,这个数据集包含有60000张用于训练的手写数字的图片,10000张用于测试的图片,所有图片的尺寸都已经被标准化了,并且,尺寸为28 * 28,每个像素值0~1区间内的值,其中0代表白色,1代表黑色,区间内的值表示灰色。为了简化,每张图片已经被转换成一个1728的一维数组,表示784个特征(2828)。

MNIST Dataset

数据集下载:http://yann.lecun.com/exdb/mnist/

加载mnist数据集
import tensorflow as tf


from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
print(mnist)

设置参数

learning_rate = 0.01
training_epochs = 100
batch_size = 100
display_step = 1
tf Graph的输入
x = tf.placeholder(tf.float32, [None, 784]) # mnist data image of shape 28*28=784
y = tf.placeholder(tf.float32, [None, 10]) # 0-9 digits recognition => 10 classes
设置权重和偏置
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
设定运行模型
pred = tf.nn.softmax(tf.matmul(x, W) + b) # Softmax
设置cost function为cross entropy
cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred), reduction_indices=1))
梯度下降
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
初始化所有参数
init = tf.global_variables_initializer()

开始训练


with tf.Session() as sess:
    sess.run(init)
    
    for epoch in range(training_epochs):
        avg_cost = 0.
        total_batch = int(mnist.train.num_examples/batch_size)
        # 遍历每个batch
        for i in range(total_batch):
            batch_xs, batch_ys = mnist.train.next_batch(batch_size)
            # 把每个batch的数据放进去训练
            _, c = sess.run([optimizer, cost], feed_dict={x: batch_xs,
                                                          y: batch_ys})
            # 计算平均损失
            avg_cost += c / total_batch
        # 展示每次迭代的日志
        if (epoch+1) % display_step == 0:
            print("Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(avg_cost))

    print("Optimization Finished!")

    # 测试模型
    correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
    # 计算3000个样本的准确率
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    print("Accuracy:", accuracy.eval({x: mnist.test.images[:3000], y: mnist.test.labels[:3000]}))
Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐