TensorFlow实现Logistic回归

一 问题描述

    MNIST是一个入门级的计算机视觉数据集,其中包含各种手写数字图片。用TensorFlow 训练Logistic 回归能够使模型更稳定,效率更高。

二 数据准备

    MNIST数据集包含4个文件,训练集6万张图片,测试集1万张图片。数字经过预处理、格式化,大小调整并居中,尺寸固定28x28的灰度图,每个像素被转成了0-255,0代表着白色,255代表着黑色。

三 模型建立

1. 参数计算

    TensorFlow需要两个参数,W和b。

W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

2. 预测函数和损失函数

    softmax回归模型用于多分类。损失函数使用交叉熵错误率模型,交叉熵用来刻画预测值和真实值之间的差距。

pred = tf.nn.softmax(tf.matmul(x, W) + b)
cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred), reduction_indices=1))

3. 梯度下降

    交叉熵越小,模型的拟合度越好,所以使用梯度下降法,可直接由TensorFlow调用。

optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

4. 模型建立和测试

    在设置的训练次数不断训练数据,显示每一次训练的代价,tf.argmax(y,1)表明的是对于y这个tensor,求其第一维最大值对应的索引而tf.equal返回的是一个布尔数组转换成0,1后求均值得到正确率。

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)
        for i in range(total_batch):
            batch_xs, batch_ys = mnist.train.next_batch(batch_size)
            _, 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))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    print("Accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))

正确率为0.91

四 总结

    TensorFlow对MNIST数据集做了更高层的封装,使得使用起来更加方便。函数库的调用可以减少代码量。


猜你喜欢

转载自blog.csdn.net/evitachan/article/details/80787923