逻辑回归实现多分类任务(python+TensorFlow+mnist)
逻辑回归是统计学中的一种经典方法,虽然叫回归,但在机器学习领域,逻辑回归通常情况下当成一个分类任务,softmax就是由其演变而来,逻辑回归一般用于二分类任务,但通过softmax可以轻易的扩展至多分类任务。
下面的程序,通过逻辑回归实现对手写数字的分类:
- 环境:python3.6、TensorFlow框架
- 数据集:MNIST
# 逻辑回归实现手写数字分类
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import numpy as np
import matplotlib.pyplot as pltmnist = input_data.read_data_sets('D:\MNIST_data', one_hot=True)x = tf.placeholder(tf.float32, [None, 784])
y = tf.placeholder(tf.float32, [None, 10])
W = tf.Variable(tf.zeros([784, 10]), tf.float32)
b = tf.Variable(tf.zeros([10]), tf.float32)
pre = tf.nn.softmax(tf.matmul(x, W) + b)
# cost function
cost = tf.reduce_mean(tf.reduce_sum(-y*tf.log(pre), reduction_indices=1))
train = tf.train.GradientDescentOptimizer(0.01).minimize(cost)
pred = tf.equal(tf.argmax(y, 1), tf.argmax(pre, 1))
# accuracy
acc = tf.reduce_mean(tf.cast(pred, tf.float32))# initializer
init = tf.global_variables_initializer()
batch_size = 100
display_step = 5
step = 100
with tf.Session() as sess:sess.run(init)num_batch = int(mnist.train.num_examples/batch_size)for i in range(step):for j in range(num_batch):batch_xs, batch_ys = mnist.train.next_batch(100)sess.run(train, feed_dict={x: batch_xs, y: batch_ys})if i % display_step == 0:train_acc = sess.run(acc, feed_dict={x: batch_xs, y: batch_ys})test_acc = sess.run(acc, feed_dict={x: mnist.test.images, y: mnist.test.labels})print('Train accuracy:%2f'% train_acc, ' Test accuracy:%2f' % test_acc)
训练100次之后的测试精度为:
Train accuracy:0.940000 Test accuracy:0.921500