In R neural net package, what is the difference between a single number argument and a vector argument for hidden neurons? e.g. hidden = 100 vs hidden = c(100,50)
In R neural net package, what is the difference between a single number argument and a vector argument for hidden neurons? e.g. hidden = 100 vs hidden = c(100,50)
You use it to provide 2 information, the number of layers, which is the length of the vector, and the number of neurons per layer.
So in your question, neuralnet(..hidden=100) means 1 layer with 100 neurons, whereas neuralnet(..hidden=c(100,50)) means 2 layers, 1st layer 100 and 2nd layer 50.
We can see this by looking at the results matrix which contains the estimated weights of the connections:
fit = neuralnet(Species == "setosa" ~ ., iris, linear.output = FALSE,hidden=2)
fit$result.matrix
[,1]
error 0.008141255
reached.threshold 0.008900248
steps 48.000000000
Intercept.to.1layhid1 -0.996032931
Sepal.Length.to.1layhid1 0.433339769
Sepal.Width.to.1layhid1 -1.178668127
Petal.Length.to.1layhid1 -0.483878954
Petal.Width.to.1layhid1 4.399508265
Intercept.to.1layhid2 1.764932246
Sepal.Length.to.1layhid2 -0.748650261
Sepal.Width.to.1layhid2 3.602604148
Petal.Length.to.1layhid2 -2.226669079
Petal.Width.to.1layhid2 -3.210541627
Intercept.to.Species == "setosa" -1.422643815
1layhid1.to.Species == "setosa" -3.596971312
1layhid2.to.Species == "setosa" 6.205140270
So you have only 1 layer and 2 hidden neurons in that layer which are connected to the variables and output layer.
If you do:
fit = neuralnet(Species == "setosa" ~ ., iris, linear.output = FALSE,hidden=c(2,1))
fit$result.matrix
[,1]
error 0.010978699
reached.threshold 0.007306809
steps 92.000000000
Intercept.to.1layhid1 4.616674345
Sepal.Length.to.1layhid1 3.342501151
Sepal.Width.to.1layhid1 4.367109042
Petal.Length.to.1layhid1 5.385330435
Petal.Width.to.1layhid1 1.402765434
Intercept.to.1layhid2 -3.828104421
Sepal.Length.to.1layhid2 1.013023746
Sepal.Width.to.1layhid2 -6.037198731
Petal.Length.to.1layhid2 3.705741605
Petal.Width.to.1layhid2 7.993464809
Intercept.to.2layhid1 -2.075821677
1layhid1.to.2layhid1 -2.550031126
1layhid2.to.2layhid1 9.393164468
Intercept.to.Species == "setosa" 4.146671533
2layhid1.to.Species == "setosa" -8.957645357
It's similar to before except now you have a 2nd layer with one neurons and this is connected to the output layer.