2017-12-16 6 views
1

"홍채"데이터 세트의 R에서 사용할 수있는 "neuralnet"패키지를 사용하여 간단한 다층 피드 순방향 신경망을 구현하려고합니다. 나는 시간 -neuralnet 패키지에 대한 오류가 있음

1)의이 시점에서 두 가지 질문이

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) 

follows-으로 내가 사용하고

코드는 어떻게이 "숨겨진"매개 변수를 사용합니까? 매뉴얼 페이지에 따르면, 자사의 제 말

숨겨진 : 각 계층

에 숨겨진 뉴런의 수 (정점)를 지정하는 정수의 벡터가 어떻게 정수의 벡터를 제공해야합니까? 각 레이어에 4 개의 뉴런/퍼셉트론으로 된 숨겨진 레이어가 1 개 있고 싶었거나, 각 레이어에 5 개의 뉴런으로 구성된 3 개의 숨겨진 레이어가 있기를 원한다면 말하십시오.

2) 코드의 마지막 줄은 나에게 평가에

오류를 오류 -를 제공는 (predvars, 데이터, 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 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) : 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-get-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이다.더미하기

변환이 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) 

편집 : 당신이 hidden = c(5,3) 을 말할 때

은 그럼 신경 네트워크 다이어그램 측면 숨겨진 노드 (레이어)하여 입력 노드, (5) 측을 것이다, 3 옆에 숨겨진 노드 (다른 계층), 출력 노드/노드

정확도에 어떤 영향을 미치는지 실마리가 없습니다.

neuralnet에 대한 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

코드를 보내 주셔서 감사합니다! 작동 중! 그러나 나는 여전히 두 가지 질문이 있습니다. 1. "숨겨진"매개 변수를 사용하는 방법 (2) '신경 회로망'에서 "계산"() 함수를 사용하는 방법은 무엇입니까? NN에 대해 많이 알지 못한다는 것을 이해합니다. 그냥 다른 사람들에게 물어보십시오. – Arun

+0

@Arun이 (가) 편집했습니다. 또한 그것을 수락하여 질문을 닫으십시오. – SamFlynn

+0

내 질문 중 두 개는 여전히 답이 없습니다 – Arun