2017-12-16 15 views
1

"iris"データセットのRで利用可能な "neuralnet"パッケージを使用して、単純なマルチレイヤフィードフォワードニューラルネットワークを実装しようとしています。neuralnetパッケージのエラーが発生しました。

私が使用していたコードは、私は、時間

1のこの時点では二つの質問を持ってfollows-

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) 

ようです)どのように私は、「隠された」パラメータを使用していますか?マニュアルページによると、そのsaying-

隠さ:各レイヤー

に隠されたニューロンの数(頂点)を指定する整数のベクトルがどのように私は整数のベクトルを供給する必要がありますか?各層に4つのニューロン/パーセプトロンからなる1つの隠れたレイヤーを持っていたいと思ったら、あるいは各レイヤーに5つのニューロンからなる3つの隠れたレイヤーを持っていたいとします。私が削除した場合、オブジェクトの種の

が見つかりません:

2)コードの最後の行は、evalの中にエラー -

エラー(predvars、データ、ENV)私を与えます"hidden"パラメータでは、このエラーは引き続き発生します。

私はここで間違っていますか?

編集:ライン -

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 ... $種: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

可能な重複:https://stackoverflow.com/questions/17457028/working-with-neuralnet([初めてRにneuralnetでの作業を取得は、「数値/複素数行列/ベクトル引数が必要です」] -in-r-for-the-first-time-requires-numeric-complex-ma) – SamFlynn

+0

@SamFlynn私は行列 'm'を含むように自分の投稿を編集しました。しかし、今私は "種"を予測しようとしている変数はなくなっています!したがって、コードの最後の行は "Species"が見つかりませんでした。何か案は? – Arun

+0

私はあまりにもそれを試して、それが何かエラーを取得し続けて把握できませんでした。その質問に 'str(d)'の出力を加えてください。私がしたのは、すべての階乗列をダミー変数に手動で変更していたことです。 – SamFlynn

答えて

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が要因を取るdoesntの要因です。

variblesをダミーに変換 -

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) 

EDIT:あなたはhidden = c(5,3) を言うとき

それでは、ニューラルネットワーク図は、あなたの入力ノードを持つことになり、サイド隠れたノード(層)による5側、 3隠れノード(別のレイヤー)と並んで、出力ノード/ノード

どのように精度に影響を与えるか分かりません。

computeは、他のすべてのマシン学習モデルと同様です。

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

コードをありがとう!それは働いている!しかし、私はまだ2つの質問があります。1.「隠された」パラメータを使用する方法(2)「neuralnet」では、「計算」関数をどのように使用しますか?あなたはNNについてあまり知らないと分かっています。他の人にそこに尋ねるだけです。 – Arun

+0

@Arunが編集を行いました。それを受け入れて質問を閉じます。 – SamFlynn

+0

私の質問のうちの2つはまだ答えられていません – Arun

関連する問題