在绘制神经网络图时出现错误:权重未计算。

5

在尝试绘制神经网络时,我收到了一个错误消息。起初我可以正常运行代码,但后来就停止了。当运行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个回答

6
在你报告的错误之前,很可能你也收到了一个 警告:
# 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

仔细查看您的代码和数据后,发现问题在于您在拟合神经网络时选择了linear.output = T; 根据文档

linear.output 逻辑值。如果不应将act.fct应用于输出神经元,则将linear output设置为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的结果:

在此输入图片描述


1
我没有收到那个警告。谢谢。 - Sharon M

3

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

nueralModel <- neuralnet(formula = f, hidden = c(4,2), linear.output = F, data = train_nn, stepmax = 1e6)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接