赞
踩
本文主要讲解:交叉熵损失(categorical_crossentropy)和focal_loss对比-BP神经网络
主要思路:
需要请私聊
focal_loss主要用于解决正负样本不平衡的问题
Focal Loss的形式为:
对比的过程和结果已录制视频,已上传到B站
focal_loss
# -*- 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)
# -*- 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)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。