对数损失函数+Logistic 函数 判断男女
小记
2022.4.18
 参考张觉非的书的 ch04章,
训练目标
训练一个模型, 给出一个人的身高、体重和体脂率,判断男女。 使用对数损失函数作为损失函数,用Logistic函数作为最后一层的激活函数。
知识
ADLINE模型是先计算线性部分 wx+b, 再对线性部分与类别标签(1/-1)的乘积施加感知机损失,从而得到损失值。感知机损失是当 l ∗ ( w ∗ x + b ) l*(w*x+b) l∗(w∗x+b) 大于等于0时模型分类正确,损失值为0,表示不惩罚,否则分类错误,损失值为 ∣ l ∗ ( w ∗ x + b ) ∣ |l*(w*x+b)| ∣l∗(w∗x+b)∣ ,越高表示惩罚越严厉。
同时对它对线性部分施加阶跃函数得到了模型的输出:1为男,0为女。
本篇想做的事情
- 把计算损失函数的方法从 从 感知机损失 换为 对数损失
 改进原因:希望损失值在输入大于0时仍有惩罚,但惩罚较小,即希望训练能够将 l ∗ ( w ∗ x + b ) l*(w*x+b) l∗(w∗x+b) 推得更远离x轴。
 
-  把用来计算预测值的激活函数 从 阶跃函数 换为Logistic函数 p = 1 1 + e − ( w x + b ) p=\frac{1}{1+e^{-(wx+b)}} p=1+e−(wx+b)1 改进原因:这时把p 限制在了0-1之间,可以设0.5为分界线进行判定。 如图所示,Logistic函数可以看成是连续光滑版的阶跃函数   
代码更新
- 在 上篇优化器 的代码上改。
- 增加 了激活函数模块 增加 Logistic类,损失函数模块增加LogLoss类
- 最后预测结果将1/0结果转化成1/-1结果,好与训练标签的约定一致
代码框架

代码
main.py
import numpy as np
from icecream import ic
import node,ope,loss,optimizer
from graph import  default_graph
def make_test():
    # 生产测试数据
    m_h = np.random.normal(171, 6, 500)
    f_h = np.random.normal(158, 5, 500)
    m_w = np.random.normal(70, 10, 500)
    f_w = np.random.normal(57, 8, 500)
    m_bfrs = np.random.normal(16, 2, 500)
    f_bfrs = np.random.normal(22, 2, 500)
    m_labels = [1] * 500
    f_labels = [-1] * 500
    train_set = np.array([np.concatenate((m_h, f_h)),
                          np.concatenate((m_w, f_w)),
                          np.concatenate((m_bfrs, f_bfrs)),
                          np.concatenate((m_labels, f_labels))
                          ]).T
    np.random.shuffle(train_set)
    return  train_set
if __name__=='__main__':
    train_set=make_test()
    x=node.Variable(shape=(3,1))
    w=node.Variable(shape=(1,3),trainable=True)
    b=node.Variable(shape=(1,1),trainable=True)
    label=node.Variable(shape=(1,1))
    w.set_value(np.mat(np.random.normal(0,0.001,(1,3))))
    b.set_value(np.mat(np.random.normal(0,0.001,(1,1))))
    y=ope.Add(ope.MatMul(w,x),b)
    predict=ope.Logistic(y)
    # loss=loss.PerceptionLoss(ope.Multiply(label,y))
    loss=loss.LogLoss(ope.Multiply(label,y))
    learning_rate=0.01
    optimizer=optimizer.Adam(default_graph,loss,learning_rate)
    # optimizer=optimizer.GradientDescent(default_graph, loss,learning_rate)
    cur_batch_size=0  #当前
    bacth_size = 10
    for epoch in range(100):
        for i in  range(len(train_set)):
            # 输入数据
            x.set_value(np.mat(train_set[i, :-1]).T)
            label.set_value(np.mat(train_set[i,-1]))
            optimizer.forward_backward()
            cur_batch_size+=1
            if cur_batch_size==bacth_size:
                optimizer._update()
                cur_batch_size=0
        if epoch%10==0:
            pred = []
            for i in range(len(train_set)):
                # 输入数据
                x.set_value(np.mat(train_set[i, :-1]).T)
                predict.forward()
                pred.append(predict.value[0, 0])  # 模型的预测结果:1男,0女
            # 将1/0结果转化成1/-1结果,好与训练标签的约定一致
            pred=(np.array(pred)>0.5).astype(np.int)*2-1
            accuracy = (train_set[:, -1] == pred).astype(np.int).sum() / len(train_set)
            print("训练次数为:",epoch,"时,准确率为:",accuracy)










