繁体   English   中英

R中神经网络包的错误

[英]Error for neuralnet package in R

我正在尝试使用 R 中为“iris”数据集提供的“neuralnet”包来实现一个简单的多层前馈神经网络。

我使用的代码如下-

library(neuralnet)
data(iris)

D <- data.frame(iris, stringsAsFactors=TRUE)

# create formula-
f <- as.formula(Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width)

# convert qualitative variables to dummy (binary) variables-
m <- model.matrix(f, data = D)

# create neural network-
iris_nn <- neuralnet(f, data = m, hidden = 4, learningrate = 0.3)

此时我有两个问题——

1.) 如何使用“隐藏”参数? 根据手册页,它的说法-

hidden:一个整数向量,指定每层中隐藏神经元(顶点)的数量

我应该如何提供整数向量? 假设我想在每层有 1 个隐藏层,每层有 4 个神经元/感知器,或者如果我想在每层有 3 个隐藏层,每层有 5 个神经元。

2.) 最后一行代码给了我错误-

eval(predvars, data, env) 中的错误:找不到对象“物种”

如果我删除“隐藏”参数,此错误仍然存​​在。

我在这里做错了什么?

编辑:添加行后 -

m <- model.matrix(f, data = D)

矩阵“m”不再包含我试图预测的“物种”变量/属性。

输出

str(D)

str(D) 'data.frame': 150 obs。 共 5 个变量:$ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ... $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 Petal 3.4 Length ... $1 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ... $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ... $ 物种:因子 w/",.."vers:set osa 1 1 1 1 1 1 1 1 1 ...

我已经成功地用“nnet”编码了这个。 发布我的代码以供参考-

data(iris)
library(nnet)

# create formula-
f <- as.formula(Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width)

# create a NN with hidden layer having 4 neurons/node and
# maximum number of iterations = 3
iris_nn <- nnet(f, data = iris, size = 4, maxit = 3)

# create a test data-
new_obs <- data.frame(Sepal.Length = 5.5, Sepal.Width = 3.1, Petal.Length = 1.4, Petal.Width = 0.4)

# make prediction-
predict(iris_nn, new_obs)   # gives percentage of which class it may belong
predict(iris_nn, new_obs, type = "class")   # gives the class instead of percentages of which 'class' this data type may belong to


# create a 'confusion matrix' to measure accuracy of model-
# rows are actual values and columns are predicted values-
# table(iris$Species, predict(iris_nn, iris[, 1:4], type = "class"))
cat("\n\nConfusion Matrix for # of iters = 3\n")
print(table(iris$Species, predict(iris_nn, iris[, 1:4], type = "class")))
cat("\n\n")

rm(iris_nn)

# setting 'maxit' to 1000, makes the model coverge-
iris_nn <- nnet(f, data = iris, size = 4, maxit = 1000)

# create a new confusion matrix to check model accuracy again-
cat("\n\nConfusion Matrix for # of iters = 1000\n")
print(table(iris$Species, predict(iris_nn, iris[, 1:4], type = "class")))
# table(iris$Species, predict(iris_nn, iris[, 1:4], type = "class"))


# to plot 'iris_nn' trained NN-
# library("NeuralNetTools")
# plotnet(iris_nn)

谢谢!!

不知道 NN 如何运行以及运行它的最佳方式是什么。 对iris数据集也不太了解。

只是指出为什么它没有运行 - 专栏Species

str(d)
'data.frame':   150 obs. of  5 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...

Species是一个因素 NN 不考虑因素。

转换为虚拟变量 -

d$set <-0
d$set[d$Species == "setosa"] <- 1

d$versi <-0 
d$versi[d$Species == "versicolor"] <- 1



f <- as.formula(set+versi ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width)

iris_nn <- neuralnet(f, data = d, hidden = 4, learningrate = 0.3)

编辑:

所以当你说hidden = c(5,3)那么神经网络图就会有你的输入节点,5 个并排隐藏节点(一层),3 个并排隐藏节点(另一层),输出节点/节点

不知道它们如何影响准确性。

neuralnetcompute就像所有其他机器学习模型的预测。

library(neuralnet)
library(caret) #has the confusionmatrix function in it
#for some reason compute needs to be called like that, calling normally was producing some error
nnans <- neuralnet::compute(NN, test)
confusionMatrix(nnans, test_labels))

1.) 关于如何使用“隐藏”参数的问题,这里有一些例子。

neuralnet(f, data = m, hidden = c(2,3,2) , learningrate = 0.3)
or
neuralnet(f, data = m, hidden = c(2,2) , learningrate = 0.3)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM