神经网络 - plot.nn中的错误:未计算权重

时间:2018-05-16 17:01:15

标签: r machine-learning neural-network

我在尝试绘制神经网络时收到了错误消息。我首先能够运行代码然后停止了。运行neuralnet()函数时,我没有收到错误消息。任何帮助,将不胜感激。我预测贷款违约。

library(neuralnet)
library(plyr)

CreditCardnn <- read.csv("https://raw.githubusercontent.com/621-Group2/Final-Project/master/UCI_Credit_Card.csv")


#Normalize dataset
maxValue <- apply(CreditCardnn, 2, max)
minValue <- apply(CreditCardnn, 2, min)

CreditCardnn <- as.data.frame(scale(CreditCardnn, center = minValue, scale = maxValue - minValue))

#Rename to target variable
colnames(CreditCardnn)[25] <- "target"


smp <- floor(0.70 * nrow(CreditCardnn))
set.seed(4784)

CreditCardnn$ID <- NULL
train_index <- sample(seq_len(nrow(CreditCardnn)), size = smp, replace = FALSE)

train_nn <- CreditCardnn[train_index, ]
test_nn <- CreditCardnn[-train_index, ]

allVars <- colnames(CreditCardnn)
predictorVars <- allVars[!allVars%in%'target']
predictorVars <- paste(predictorVars, collapse = "+")
f <- as.formula(paste("target~", predictorVars, collapse = "+"))

nueralModel <- neuralnet(formula = f, hidden = c(4,2), linear.output = T, data = train_nn)

plot(nueralModel)

出现以下错误:

Error in plot.nn(nueralModel) : weights were not calculated 

2 个答案:

答案 0 :(得分:1)

在您报告错误之前,很可能您还收到了警告

# your data preparation code verbatim here
> nueralModel <- neuralnet(formula = f, hidden = c(4,2), linear.output = T, data = train_nn)
Warning message:
algorithm did not converge in 1 of 1 repetition(s) within the stepmax 

此消息 非常重要,有效地警告您神经网络没有收敛。鉴于此消息,当您尝试绘制网络时,下游的错误实际上是预期的:

> plot(nueralModel)
Error in plot.nn(nueralModel) : weights were not calculated

仔细查看您的代码&amp;数据,事实证明,问题在于您选择linear.output = T来拟合神经网络;来自docs

  

linear.output 逻辑。如果act.fct不应用于输出神经元,则将线性输出设置为TRUE,否则为FALSE。

在神经网络的最后一层保持线性输出通常仅用于回归设置;在分类设置中,例如你的分类设置,正确的选择是将激活函数应用于输出神经元。因此,尝试使用与linear.output = F相同的代码,我们得到:

> nueralModel <- neuralnet(formula = f, hidden = c(4,2), linear.output = F, data = train_nn) # no warning this time
> plot(nueralModel)

以下是plot

的结果

enter image description here

答案 1 :(得分:1)

尝试增加stepmax。例如设置stepmax = 1e6或更高。更高的stepmax需要更长的时间,但是您可以尝试:

nueralModel <-神经网络(公式= f,隐藏= c(4,2),线性。输出= F,数据= train_nn,stepmax = 1e6)