R实现BP神经网络与参数调优

简介: R实现BP神经网络与参数调优

1 清理环境与准备

################nnet packages single hidden layer BP neural network###################################
##----------------------data loading and cleaning-----------------------------
#clean up enviroment variables loading Sonar, Mines vs. Rocks data
rm(list=ls())
#install.packages("mlbench")
library(mlbench)
data(Sonar)

确定y变量

# Redefine factor level
levels(Sonar$Class)<-c(0,1)

2 确定测试集与训练集

#random sampling
set.seed(1221)
select<-sample(1:nrow(Sonar),nrow(Sonar)*0.7)
train<-Sonar[select,]
test<-Sonar[-select,]

# standardized data
train[,1:60]=scale(train[,1:60])
test[,1:60]=scale(test[,1:60])

3 使用nnet包与参数说明

##---------------------Implementation of BP neural network using packet------------------------
#install.packages("nnet")
library(nnet)
mynnet<-nnet(Class~., linout =F,size=14, decay=0.0076, maxit=200, 
             data = train)
#linout judge whether the output is linear
# size is Number of nodes 
#decay is attenuation rate
#decay and learning rate are similar
#Maximum number of iterations

4 结果评估

##model predict
out<-predict(mynnet, test) 
out[out<0.5]=0
out[out>=0.5]=1
#test$Class=ifelse(test$Class=="M",1,0)
##calculation accuracy
rate<-sum(out==test$Class)/length(test$Class)
rate

在这里插入图片描述

5 绘制ROC曲线

####Predict on the training set and test set respectively
##The construction of ROC curve function ROC () is convenient.
##Note that this function will add columns to the data frame called
ROC<-function(model,train,test,objcolname,ifplot=TRUE){
  library(ROCR,quietly = T)
  train$p<-predict(model, train) 
  test$p<-predict(model, test) 
  
  predTr <- prediction(train$p, train[,objcolname])
  perfTr <- performance(predTr,"tpr","fpr")
  
  predTe <- prediction(test$p, test[,objcolname])
  perfTe <- performance(predTe,"tpr","fpr")
  
  tr_auc<-round(as.numeric(performance(predTr,'auc')@y.values),3)
  te_auc<-round(as.numeric(performance(predTe,'auc')@y.values),3)
  
  if(ifplot==T){
    plot(perfTr,col='green',main="ROC of Models")
    plot(perfTe, col='black',lty=2,add=TRUE);
    abline(0,1,lty=2,col='red')
    
    tr_str<-paste("Tran-AUC:",tr_auc,sep="")
    legend(0.3,0.45,c(tr_str),2:8)
    te_str<-paste("Test-AUC:",te_auc,sep="")
    legend(0.3,0.25,c(te_str),2:8)
  }
  auc<-data.frame(tr_auc,te_auc)
  return(auc)
}
ROC(model=mynnet,train=train,test=test,objcolname="Class",ifplot=T)

在这里插入图片描述

6 循环调整参数

############################Adjusting parameters########################################


##The predictive variables of the input data must be binary. And all variables only include model input and output variables.
##When adjusting parameters, if there are too many variables, the size should not be too large.
##Build the parameter adjustment function network().
network<-function(formula,data,size,adjust,decay=0,maxit=200,scale=TRUE,
                  samplerate=0.7,seed=1,linout=FALSE,ifplot=TRUE){
  library(nnet)
  ##The specification output variable is 0,1
  yvar<-colnames(data)==(all.vars(formula)[1])
  levels(data[,yvar])<-c(0,1)
  ##Establish training set and test set by sampling
  set.seed(seed)
  select<-sample(1:nrow(data),nrow(data)*samplerate)
  train=data[select,]
  test=data[-select,]
  ##Standardize according to given judgment
  if(scale==T){
    xvar<-colnames(data)!=(all.vars(formula)[1])
    train[,xvar]=scale(train[,xvar])
    test[,xvar]=scale(test[,xvar])
  }
  ##Recycle NNET training parameters
  obj<-eval(parse(text = adjust))
  auc<-data.frame()
  for(i in obj){
    if(adjust=="size"){
      mynnet<-nnet(formula,size=i,linout=linout,decay=decay,
                   maxit=maxit,trace=FALSE,data=train)
    }
    else if(adjust=="decay"){
      mynnet<-nnet(formula,size=size,linout=linout,decay=i,
                   maxit=maxit,trace=FALSE,data=train)
    }
    ##Call the previous roc() to get the AUC value of the corresponding parameterֵ
    objcolname<-all.vars(formula)[1]
    auc0<-ROC(model=mynnet,train=train,test=test,
              objcolname=objcolname,ifplot=F)
    ##Output data frames corresponding to different values of specified parameters
    out<-data.frame(i,auc0)
    auc<-rbind(auc,out)
  }
  
  names(auc)<-c(adjust,"Train_auc","Test_auc")
  # if(ifplot==T){
  #   library(plotrix)
  #   # twoord.plot(auc1[,1] , auc1$Train_auc , auc1[,1] , auc1$Test_auc , lcol=4 , rcol=2 , xlab=adjust , 
  #   #             ylab="Train_auc" , rylab="Test_auc" , type=c("l","b"),lab=c(15,5,10))
  # }
  return(auc)
}
auc<-network(Class~.,data=Sonar,size=1:16,adjust="size",
             decay=0.0001,maxit=200,scale=T)
which(auc$Test_auc==max(auc$Test_auc))
auc<-network(Class~.,data=Sonar,size=14,adjust="decay",
             decay=c(0,seq(0.0001,0.01,0.0003)),maxit=200)


plot(auc$decay,auc$Train_auc,ylim=c(0.8,1))
lines(auc$decay,auc$Train_auc)
points(auc$decay,auc$Test_auc)
lines(auc$decay,auc$Test_auc)


# #根据中左侧纵坐标是训练集 auc 值的变化,对应直线,右侧纵坐标是测试集对应点线。可见 size
# 大于 3 时就可以对训练集很好的拟合,而当 size 等于 14 是测试集的 ROC 指标较高,故这里我们选
# 取 size=14。
# 选取 size 后对指定一系列 decay 数值进行比较,这里选择的是从 0.0001 到 0.01 的等差数列差值
# 是 0.0003,同时包括 0。得到的结果如下:(运行时间较长, decay 的范围可以选取多个区间比较)

## according to the change of AUC value of the training set, the left ordinate corresponds to the straight line, and the right ordinate corresponds to the point line of the test set. Visible size
#When it is greater than 3, the training set can be well fitted. When the size is equal to 14, the ROC index of the test set is high, so we choose here
#Take size = 14.
#Select size and compare the specified series of decimal values. Here, select the difference of the arithmetic sequence from 0.0001 to 0.01
#Is 0.0003, including 0. The results are as follows: (the running time is long, and multiple intervals can be selected for the range of decay)
目录
相关文章
|
机器学习/深度学习 算法 调度
14种智能算法优化BP神经网络(14种方法)实现数据预测分类研究(Matlab代码实现)
14种智能算法优化BP神经网络(14种方法)实现数据预测分类研究(Matlab代码实现)
906 0
|
机器学习/深度学习 算法
采用蚁群算法对BP神经网络进行优化
使用蚁群算法来优化BP神经网络的权重和偏置,克服传统BP算法容易陷入局部极小值、收敛速度慢、对初始权重敏感等问题。
730 5
|
机器学习/深度学习 并行计算 算法
【CPOBP-NSWOA】基于豪冠猪优化BP神经网络模型的多目标鲸鱼寻优算法研究(Matlab代码实现)
【CPOBP-NSWOA】基于豪冠猪优化BP神经网络模型的多目标鲸鱼寻优算法研究(Matlab代码实现)
417 8
|
机器学习/深度学习 传感器 算法
【表面粗糙度】基于粒子群PSO算法优化-BP神经网络的表面粗糙度研究(Matlab代码实现)
【表面粗糙度】基于粒子群PSO算法优化-BP神经网络的表面粗糙度研究(Matlab代码实现)
532 7
|
机器学习/深度学习 编解码 并行计算
【创新未发表!】基于BKA算法优化-BP、HO算法优化-BP、CP算法优化-BP、GOOSE算法优化-BP、NRBO算法优化-BP神经网络回归预测比较研究(Matlab代码)
【创新未发表!】基于BKA算法优化-BP、HO算法优化-BP、CP算法优化-BP、GOOSE算法优化-BP、NRBO算法优化-BP神经网络回归预测比较研究(Matlab代码)
665 0
|
机器学习/深度学习 数据采集 存储
概率神经网络的分类预测--基于PNN的变压器故障诊断(Matlab代码实现)
概率神经网络的分类预测--基于PNN的变压器故障诊断(Matlab代码实现)
1848 92
|
机器学习/深度学习 数据采集 传感器
【故障诊断】基于matlab BP神经网络电机数据特征提取与故障诊断研究(Matlab代码实现)
【故障诊断】基于matlab BP神经网络电机数据特征提取与故障诊断研究(Matlab代码实现)
434 0
|
机器学习/深度学习 传感器 算法
【无人车路径跟踪】基于神经网络的数据驱动迭代学习控制(ILC)算法,用于具有未知模型和重复任务的非线性单输入单输出(SISO)离散时间系统的无人车的路径跟踪(Matlab代码实现)
【无人车路径跟踪】基于神经网络的数据驱动迭代学习控制(ILC)算法,用于具有未知模型和重复任务的非线性单输入单输出(SISO)离散时间系统的无人车的路径跟踪(Matlab代码实现)
887 2
|
机器学习/深度学习 缓存 算法
2025年华为杯A题|通用神经网络处理器下的核内调度问题研究生数学建模|思路、代码、论文|持续更新中....
2025年华为杯A题|通用神经网络处理器下的核内调度问题研究生数学建模|思路、代码、论文|持续更新中....
1150 1
|
机器学习/深度学习 算法 数据可视化
PINN物理信息神经网络用于求解二阶常微分方程(ODE)的边值问题研究(Matlab代码实现)
PINN物理信息神经网络用于求解二阶常微分方程(ODE)的边值问题研究(Matlab代码实现)
596 6

热门文章

最新文章