Logistic_regression exercise
Logistic Regression Example
生成数据集
‘+’ 从高斯分布采样 (X, Y) ~ N(3, 6, 1, 1, 0).
‘o’ 从高斯分布采样 (X, Y) ~ N(6, 3, 1, 1, 0)
1 import tensorflow as tf 2 import matplotlib.pyplot as plt 3 4 from matplotlib import animation, rc 5 from IPython.display import HTML 6 import matplotlib.cm as cm 7 import numpy as np 8 %matplotlib inline 9 10 dot_num = 100 11 x_p = np.random.normal(3., 1, dot_num) 12 y_p = np.random.normal(6., 1, dot_num) 13 y = np.ones(dot_num) 14 C1 = np.array([x_p, y_p, y]).T 15 16 x_n = np.random.normal(6., 1, dot_num) 17 y_n = np.random.normal(3., 1, dot_num) 18 y = np.zeros(dot_num) 19 C2 = np.array([x_n, y_n, y]).T 20 21 plt.scatter(C1[:, 0], C1[:, 1], c='b', marker='+') 22 plt.scatter(C2[:, 0], C2[:, 1], c='g', marker='o') 23 24 data_set = np.concatenate((C1, C2), axis=0) 25 np.random.shuffle(data_set)

建立模型
建立模型类,定义loss函数,定义一步梯度下降过程函数
填空一:实现sigmoid的交叉熵损失函数(不使用tf内置的loss 函数)
1 epsilon = 1e-12 2 class LogisticRegression(): 3 def __init__(self): 4 self.W = tf.Variable(shape=[2, 1], dtype=tf.float32, 5 initial_value=tf.random.uniform(shape=[2, 1], minval=-0.1, maxval=0.1)) 6 self.b = tf.Variable(shape=[1], dtype=tf.float32, initial_value=tf.zeros(shape=[1])) 7 8 self.trainable_variables = [self.W, self.b] 9 @tf.function 10 def __call__(self, inp): 11 logits = tf.matmul(inp, self.W) + self.b # shape(N, 1) 12 pred = tf.nn.sigmoid(logits) 13 return pred 14 15 @tf.function 16 def compute_loss(pred, label): 17 if not isinstance(label, tf.Tensor): 18 label = tf.constant(label, dtype=tf.float32) 19 pred = tf.squeeze(pred, axis=1) 20 21 losses= -(label*(tf.math.log(pred+epsilon))+(1.-label)*(tf.math.log(1.-pred+epsilon))) 22 loss = tf.reduce_mean(losses) 23 24 pred = tf.where(pred>0.5, tf.ones_like(pred), tf.zeros_like(pred)) 25 accuracy = tf.reduce_mean(tf.cast(tf.equal(label, pred), dtype=tf.float32)) 26 return loss, accuracy 27 @tf.function 28 def train_one_step(model, optimizer, x, y): 29 with tf.GradientTape() as tape: 30 pred = model(x) 31 loss, accuracy = compute_loss(pred, y) 32 33 grads = tape.gradient(loss, model.trainable_variables) 34 optimizer.apply_gradients(zip(grads, model.trainable_variables)) 35 return loss, accuracy, model.W, model.b
实例化一个模型,进行训练
1 if __name__ == '__main__': 2 model = LogisticRegression() 3 opt = tf.keras.optimizers.SGD(learning_rate=0.01) 4 x1, x2, y = list(zip(*data_set)) 5 x = list(zip(x1, x2)) 6 animation_fram = [] 7 8 for i in range(200): 9 loss, accuracy, W_opt, b_opt = train_one_step(model, opt, x, y) 10 animation_fram.append((W_opt.numpy()[0, 0], W_opt.numpy()[1, 0], b_opt.numpy(), loss.numpy())) 11 if i%20 == 0: 12 print(f'loss: {loss.numpy():.4}\t accuracy: {accuracy.numpy():.4}')
loss: 0.7504 accuracy: 0.5
loss: 0.5298 accuracy: 0.98
loss: 0.4236 accuracy: 0.98
loss: 0.356 accuracy: 0.98
loss: 0.3098 accuracy: 0.98
loss: 0.2764 accuracy: 0.98
loss: 0.2511 accuracy: 0.98
loss: 0.2313 accuracy: 0.98
loss: 0.2154 accuracy: 0.98
loss: 0.2023 accuracy: 0.98
结果展示
1 f, ax = plt.subplots(figsize=(6,4)) 2 f.suptitle('Logistic Regression Example', fontsize=15) 3 plt.ylabel('Y') 4 plt.xlabel('X') 5 ax.set_xlim(0, 10) 6 ax.set_ylim(0, 10) 7 8 line_d, = ax.plot([], [], label='fit_line') 9 C1_dots, = ax.plot([], [], '+', c='b', label='actual_dots') 10 C2_dots, = ax.plot([], [], 'o', c='g' ,label='actual_dots') 11 12 13 frame_text = ax.text(0.02, 0.95,'',horizontalalignment='left',verticalalignment='top', transform=ax.transAxes) 14 # ax.legend() 15 16 def init(): 17 line_d.set_data([],[]) 18 C1_dots.set_data([],[]) 19 C2_dots.set_data([],[]) 20 return (line_d,) + (C1_dots,) + (C2_dots,) 21 22 def animate(i): 23 xx = np.arange(10, step=0.1) 24 a = animation_fram[i][0] 25 b = animation_fram[i][1] 26 c = animation_fram[i][2] 27 yy = a/-b * xx +c/-b 28 line_d.set_data(xx, yy) 29 30 C1_dots.set_data(C1[:, 0], C1[:, 1]) 31 C2_dots.set_data(C2[:, 0], C2[:, 1]) 32 33 frame_text.set_text('Timestep = %.1d/%.1d\nLoss = %.3f' % (i, len(animation_fram), animation_fram[i][3])) 34 35 return (line_d,) + (C1_dots,) + (C2_dots,) 36 37 anim = animation.FuncAnimation(f, animate, init_func=init, 38 frames=len(animation_fram), interval=30, blit=True) 39 40 HTML(anim.to_html5_video())


浙公网安备 33010602011771号