逻辑回归主要用于处理二分类问题(Binary Classification)。事实上,逻辑回归与 Softmax 回归可以看做是最简单的神经网络。它的模型是:
其中,sigmoid 函数为:
其代价函数为:
这个代价函数事实上就是交叉熵,TensorFlow 也提供了直接计算这个代价的工具函数,可以直接使用,只是需要将 表示为独热码(one-hot)。在本文中,将会手写实现这个代价函数。
上面就是编写逻辑回归所需要的表达式。为了便于有个直观的理解,在这篇博文的实现代码中加入了绘图相关的代码,可以将数据可视化。
实现过程,源代码可以在我的 GitHub 上检出:
-
导入库:
import numpy as np import tensorflow as tf import matplotlib.pyplot as plt -
生成数据点。需要说明的是,为了便于演示,本文生成了两组线性可分的数据点,以 为分界,位于上方的为正例(positive example),位于下方的为负例(negative example)。
data_X = np.random.randn(50, 2) data_Y = np.where((data_X[:, 1] - data_X[:, 0]) > 0, 1, 0).reshape(50, 1) -
将数据点可视化。本文绘图主要使用了 matplotlib 中的
pyplotAPI。这个 API 的使用方法和 MatLab 颇为相似。pos = np.where(data_Y == 1) neg = np.where(data_Y == 0) plt.plot(data_X[pos, 0], data_X[pos, 1], 'k+') plt.plot(data_X[neg, 0], data_X[neg, 1], 'ko') plt.plot([-2, 2], [-2, 2]) plt.show() -
定义变量与占位符:
W = tf.Variable([[-.1], [.1]], dtype=tf.float32) b = tf.Variable([0], dtype=tf.float32) X = tf.placeholder(tf.float32, [None, 2]) Y = tf.placeholder(tf.float32, [None, 1]) -
定义模型:
def model(W, b, X): return tf.sigmoid(tf.matmul(X, W) + b) -
生成模型、代价函数;定义优化器与训练步骤。代码中以注释的形式写出了 TensorFlow 自带的交叉熵损失函数。需要注意的是,要把
Y_hat与Y改为独热码才可用。Y_hat = model(W, b, X) # J = tf.nn.softmax_cross_entropy_with_logits(logits=Y_hat, labels=Y) J = tf.reduce_mean(-Y * tf.log(Y_hat) - (1 - Y) * tf.log(1 - Y_hat)) optimizer = tf.train.GradientDescentOptimizer(0.01) train_step = optimizer.minimize(J) -
执行训练过程。最后打印出在训练集上的准确率(accuracy):
with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for i in range(1000): sess.run(train_step, {X: data_X, Y: data_Y}) # print(sess.run(J, {X: data_X, Y: data_Y})) print(np.mean(data_Y == (sess.run(Y_hat, {X: data_X}) > 0.5)))