首页 文章

R中神经网络包的错误

提问于
浏览
1

我正在尝试使用R中可用于“虹膜”数据集的“神经网络”包实现一个简单的多层前馈神经网络 .

我使用的代码如下 -

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.)如何使用“隐藏”参数?根据手册页,其说 -

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

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

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

eval中的错误(predvars,data,env):找不到对象'Species'

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

我在这做错了什么?

编辑:添加行后 -

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 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 ... $种类:因子w / 3级“setosa”,“versicolor”,..: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 回答

  • 1

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

    只是指出它为什么不运行 - 专栏 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))
    

相关问题