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)
目录
相关文章
|
2月前
|
机器学习/深度学习 自然语言处理 计算机视觉
【YOLOv8改进 - Backbone主干】VanillaNet:极简的神经网络,利用VanillaBlock降低YOLOV8参数
【YOLOv8改进 - Backbone主干】VanillaNet:极简的神经网络,利用VanillaBlock降低YOLOV8参数
|
29天前
|
监控 网络协议 Linux
在Linux中,如何进行网络调优?
在Linux中,如何进行网络调优?
|
1月前
|
机器学习/深度学习 前端开发 数据挖掘
基于Python Django的房价数据分析平台,包括大屏和后台数据管理,有线性、向量机、梯度提升树、bp神经网络等模型
本文介绍了一个基于Python Django框架开发的房价数据分析平台,该平台集成了多种机器学习模型,包括线性回归、SVM、GBDT和BP神经网络,用于房价预测和市场分析,同时提供了前端大屏展示和后台数据管理功能。
|
1月前
|
监控 Linux 测试技术
什么是Linux系统的网络参数?
【8月更文挑战第10天】什么是Linux系统的网络参数?
45 5
|
2月前
|
负载均衡 Linux
网络相关的调优
网络相关的调优
25 11
|
2月前
|
Linux 开发工具
CPU-IO-网络-内核参数的调优
CPU-IO-网络-内核参数的调优
64 7
|
29天前
|
监控 网络协议 算法
在Linux中,如何进行网络性能调优?
在Linux中,如何进行网络性能调优?
|
2月前
|
机器学习/深度学习 数据采集 监控
Python基于BP神经网络算法实现家用热水器用户行为分析与事件识别
Python基于BP神经网络算法实现家用热水器用户行为分析与事件识别
|
2月前
|
机器学习/深度学习 数据采集 监控
算法金 | DL 骚操作扫盲,神经网络设计与选择、参数初始化与优化、学习率调整与正则化、Loss Function、Bad Gradient
**神经网络与AI学习概览** - 探讨神经网络设计,包括MLP、RNN、CNN,激活函数如ReLU,以及隐藏层设计,强调网络结构与任务匹配。 - 参数初始化与优化涉及Xavier/He初始化,权重和偏置初始化,优化算法如SGD、Adam,针对不同场景选择。 - 学习率调整与正则化,如动态学习率、L1/L2正则化、早停法和Dropout,以改善训练和泛化。
26 0
算法金 | DL 骚操作扫盲,神经网络设计与选择、参数初始化与优化、学习率调整与正则化、Loss Function、Bad Gradient
|
3月前
|
机器学习/深度学习 PyTorch 算法框架/工具
【从零开始学习深度学习】28.卷积神经网络之NiN模型介绍及其Pytorch实现【含完整代码】
【从零开始学习深度学习】28.卷积神经网络之NiN模型介绍及其Pytorch实现【含完整代码】

热门文章

最新文章