交叉熵损失和focal_loss对比-BP神经网络

21 篇文章 20 订阅
14 篇文章 3 订阅

1、摘要

本文主要讲解:交叉熵损失(categorical_crossentropy)和focal_loss对比-BP神经网络
主要思路:

  1. focal_loss的实现
  2. BP神经网络的实现
  3. 数据标签正负比例占比选择,用于实现对比效果
  4. 选取准确率、召回率、f1做评价指标

2、数据介绍

需要请私聊

3、相关技术

focal_loss主要用于解决正负样本不平衡的问题
Focal Loss的形式为:
在这里插入图片描述

4、完整代码和步骤

对比的过程和结果已录制视频,已上传到B站

focal_loss

交叉熵损失(categorical_crossentropy) 程序,已经经过网格搜索
# -*- coding: utf-8 -*-
import os
import warnings

import numpy as np
import pandas as pd
from sklearn.metrics import accuracy_score, classification_report
from sklearn.model_selection import train_test_split
from tensorflow.python.keras.layers.core import Dense, Dropout, Activation
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.utils.np_utils import to_categorical
from tensorflow.python.keras.layers import BatchNormalization

warnings.filterwarnings("ignore")

np.random.seed(2222)

# nor开头的样本取全部  剩下的取0.1  0.05   0.02模拟样本不平衡  一共要生成三个数据集
nor_file = 'data/normal.xls'
other_dir = 'data\\'
files = os.listdir(other_dir)
for i in [0.1, 0.05, 0.02]:
    data = pd.read_excel(nor_file, sheet_name=0)  # pandas以DataFrame的格式读入excel表
    data.loc[:, 'label'] = 0  # 设置nor开头的标签为0
    for file in files:
        if file != 'normal.xls':
            data_other_all = pd.read_excel(other_dir + file, sheet_name=0)  # pandas以DataFrame的格式读入excel表
            data_other01 = data_other_all.sample(int(i * data.shape[0]))
            data_other01.loc[:, 'label'] = files.index(file) + 1  # 设置非正常的标签为1-7
            data = pd.concat([data, data_other01], axis=0)

    feature = ['TEI', 'TEO', 'TCI', 'TCO', 'TRE', 'TRC', 'kW', 'TRC_sub', 'Tsh_suc', 'PO_feed',
               'TCA']  # 影响因素11个
    label = ['label']  # 标签一个,即需要进行预测的值
    # 2 数据预处理和标注
    data_mean = data.mean()
    data_std = data.std()
    data_train = (data - data_mean) / data_std  # 数据标准化
    x_train = data[feature].values  # 特征数据
    y_train = to_categorical(data[label])  # 标签数据
    x_train, x_valid, y_train, y_valid = train_test_split(x_train, y_train, test_size=.2)
    # 3 建立一个简单BP神经网络模型
    # 神经网络超参数的选择   已经经过网格搜索,确定了最优的参数
    for unit in [512]:
        for dropout in [0.15]:
            for epochs in [64]:
                for batch_size in [64]:
                    model = Sequential()  # 层次模型
                    model.add(Dense(unit, activation='relu', input_dim=11))  # 输入层,Dense表示BP层
                    model.add(Dropout(dropout))
                    model.add(Dense(256, activation='relu'))
                    model.add(Dropout(dropout))
                    model.add(BatchNormalization(momentum=0.8))
                    model.add(Dense(y_train.shape[1], activation='softmax'))
                    # 用focal_loss做损失函数,希望准确率和召回率比使用交叉熵损失函数高
                    model.compile(loss='categorical_crossentropy', optimizer='rmsprop')  # 编译模型
                    history = model.fit(x_train, y_train, epochs=1500, batch_size=x_train.shape[0], verbose=0)

                    # 显示训练结果,损失图
                    plt.figure(figsize=(16, 8))
                    plt.plot(history.history['loss'], label='train loss')
                    plt.legend(loc='best')
                    plt.show()

                    # 4 预测,并还原结果。
                    y_pre = model.predict(x_valid)
                    # 准确率和召回率
                    y_valid = y_valid.round()
                    y_pre = y_pre.round()
                    print('剩下的取  ', str(i))
                    print("整体准确率:", accuracy_score(y_valid, y_pre))
                    report = classification_report(y_valid.round(), y_pre.round(), output_dict=True)
                    # 计算其他类别的召回率
                    # print('其他召回率:', report['8']['recall'])
                    print(report)

focal_loss程序,已经经过网格搜索
# -*- coding: utf-8 -*-
import os

import numpy as np
import pandas as pd
import tensorflow.python.keras.backend as K
from sklearn.metrics import accuracy_score, classification_report
from sklearn.model_selection import train_test_split
from tensorflow.python.keras.layers import BatchNormalization
from tensorflow.python.keras.layers.core import Dense, Dropout, Activation
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.utils.np_utils import to_categorical
import warnings

warnings.filterwarnings("ignore")
np.random.seed(2222)


def categorical_focal_loss(alpha, gamma=2.):
    alpha = np.array(alpha, dtype=np.float32)

    def categorical_focal_loss_fixed(y_true, y_pred):
        # Clip the prediction value to prevent NaN's and Inf's
        epsilon = K.epsilon()
        y_pred = K.clip(y_pred, epsilon, 1. - epsilon)
        # Calculate Cross Entropy
        cross_entropy = -y_true * K.log(y_pred)
        # Calculate Focal Loss
        loss = alpha * K.pow(1 - y_pred, gamma) * cross_entropy
        # Compute mean loss in mini_batch
        return K.mean(K.sum(loss, axis=-1))

    return categorical_focal_loss_fixed


# nor开头的样本取全部  剩下的取0.1  0.05   0.02模拟样本不平衡  一共要生成三个数据集
nor_file = 'data/normal.xls'
other_dir = 'data\\'
files = os.listdir(other_dir)
for i in [0.1, 0.05, 0.02]:
    data = pd.read_excel(nor_file, sheet_name=0)  # pandas以DataFrame的格式读入excel表
    data.loc[:, 'label'] = 0  # 设置nor开头的标签为0
    for file in files:
        if file != 'normal.xls':
            data_other_all = pd.read_excel(other_dir + file, sheet_name=0)  # pandas以DataFrame的格式读入excel表
            data_other01 = data_other_all.sample(int(i * data.shape[0]))
            data_other01.loc[:, 'label'] = files.index(file) + 1  # 设置非正常的标签为1-7
            data = pd.concat([data, data_other01], axis=0)

    feature = ['TEI', 'TEO', 'TCI', 'TCO', 'TRE', 'TRC', 'kW', 'TRC_sub', 'Tsh_suc', 'PO_feed',
               'TCA']  # 影响因素11个
    label = ['label']  # 标签一个,即需要进行预测的值
    # 2 数据预处理和标注
    data_mean = data.mean()
    data_std = data.std()
    data_train = (data - data_mean) / data_std  # 数据标准化
    x_train = data[feature].values  # 特征数据
    y_train = to_categorical(data[label])  # 标签数据
    x_train, x_valid, y_train, y_valid = train_test_split(x_train, y_train, test_size=.2)
    # 神经网络超参数的选择  已经经过网格搜索,确定了最优的参数
    for unit in [512]:
        for dropout in [0.15]:
            for gamma in [1]:
                for alpha in [0.1]:
                    #  建立一个简单BP神经网络模型
                    model = Sequential()  # 层次模型
                    model.add(Dense(unit, activation='relu', input_dim=11))  # 输入层,Dense表示BP层
                    model.add(Dropout(dropout))
                    model.add(Dense(256, activation='relu'))
                    model.add(Dropout(dropout))
                    model.add(BatchNormalization(momentum=0.8))
                    model.add(Dense(y_train.shape[1], activation='softmax'))
                    # 用交叉熵损失函数做损失函数,希望准确率和召回率比使用交叉熵损失函数高
                    model.compile(loss=[categorical_focal_loss(alpha=alpha, gamma=gamma)], optimizer='rmsprop')  # 编译模型
                    history = model.fit(x_train, y_train, epochs=1500, batch_size=x_train.shape[0], verbose=0)

                    # 显示训练结果,损失图
                    # plt.figure(figsize=(16, 8))
                    # plt.plot(history.history['loss'], label='train loss')
                    # plt.legend(loc='best')
                    # plt.show()

                    # 4 预测,并还原结果。
                    y_pre = model.predict(x_valid)
                    # 准确率和召回率
                    y_valid = y_valid.round()
                    y_pre = y_pre.round()
                    print('剩下的取  ', str(i))
                    print("整体准确率:", accuracy_score(y_valid, y_pre))
                    report = classification_report(y_valid.round(), y_pre.round(), output_dict=True)
                    # 计算其他类别的召回率
                    # print('其他召回率:', report['8']['recall'])
                    print(report)

5、学习链接

损失函数:Focal Loss

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值