提问者:小点点

R中的neuralnet包出错


我正在尝试使用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.)如何使用“隐藏”参数?根据手册的页面,它的说法是-

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

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

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

评估(预变量、数据、环境)中出错:未找到对象“物种”

如果删除“hidden”参数,此错误仍然存在。

我做错了什么?

编辑:添加行之后-

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

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

产量

str(D)

STR(D)'data.frame': 150 obs. of 5变量:$Sepal.长度: num5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9...$Sepal。宽度: num3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1...花瓣。长度: num1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5...花瓣。宽度: num0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1...$物种:因子w/3水平"刚毛","杂色",...:1 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)

谢谢


共1个答案

匿名用户

不知道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 ...

物种是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)

编辑:

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

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

neuralnetcompute与所有其他机器学习模型的predict类似。

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))