1. 逻辑回归解决二分类问题
1.1 生成数据集
-
'+' 从高斯分布采样 (X, Y) ~ N(3, 6, 1, 1, 0).
-
'o' 从高斯分布采样 (X, Y) ~ N(6, 3, 1, 1, 0)
import tensorflow as tf
import matplotlib.pyplot as plt
from matplotlib import animation, rc
from IPython.display import HTML
import matplotlib.cm as cm
import numpy as np
%matplotlib inline
dot_num = 100
x_p = np.random.normal(3., 1, dot_num)
y_p = np.random.normal(6., 1, dot_num)
y = np.ones(dot_num)
C1 = np.array([x_p, y_p, y]).T
x_n = np.random.normal(6., 1, dot_num)
y_n = np.random.normal(3., 1, dot_num)
y = np.zeros(dot_num)
C2 = np.array([x_n, y_n, y]).T
plt.scatter(C1[:, 0], C1[:, 1], c='b', marker='+')
plt.scatter(C2[:, 0], C2[:, 1], c='g', marker='o')
data_set = np.concatenate((C1, C2), axis=0)
np.random.shuffle(data_set)
1.2 建立模型
- 逻辑函数的交叉熵损失函数:
\[L = -\sum _{i=1}^{n}y_ilog(p_i)+(1-y_i)log(1-p_i) \]
-
\(y_i\) 指 i 的真实值,\(p_i\) 指 i 的预测值。
-
下面loss函数中在预测值pred后面加上了epsilon。
epsilon = 1e-12
class LogisticRegression():
def __init__(self):
self.W = tf.Variable(shape=[2, 1], dtype=tf.float32,
initial_value=tf.random.uniform(shape=[2, 1], minval=-0.1, maxval=0.1))
self.b = tf.Variable(shape=[1], dtype=tf.float32, initial_value=tf.zeros(shape=[1]))
self.trainable_variables = [self.W, self.b]
@tf.function
def __call__(self, inp):
logits = tf.matmul(inp, self.W) + self.b #shape(N, 1)
pred = tf.nn.sigmoid(logits)
return pred
@tf.function
def compute_loss(pred, label):
if not isinstance(label, tf.Tensor):
label = tf.constant(label, dtype=tf.float32)
pred = tf.squeeze(pred, axis=1)
'''===================&