2
$\begingroup$

I used Iris dataset for classification with 3 layer Neural Network
I decided to use :
3 neurons for input since it has 3 features,
3 neurons for output since it has 3 classes and
In the hidden layer what is the best number of neurons to do classification in this case?

$\endgroup$

2 Answers 2

1
$\begingroup$

And to automate the evaluations, you can use a tool like, Keras Tuner https://keras-team.github.io/keras-tuner/

From their docs, an example setup would look like this.

The min-max value for the units is where you would tune it for your case.

from tensorflow import keras
from tensorflow.keras import layers
from kerastuner.tuners import RandomSearch


def build_model(hp):
    model = keras.Sequential()
    model.add(layers.Dense(units=hp.Int('units',
                                        min_value=32,
                                        max_value=512,
                                        step=32),
                           activation='relu'))
    model.add(layers.Dense(10, activation='softmax'))
    model.compile(
        optimizer=keras.optimizers.Adam(
            hp.Choice('learning_rate',
                      values=[1e-2, 1e-3, 1e-4])),
        loss='sparse_categorical_crossentropy',
        metrics=['accuracy'])
    return model
$\endgroup$
1
$\begingroup$

There is not a clear a answer for this question. You should try and evaluated how the models performs testing diffferents configurations. But, basically, the next two premises are followed:

  • More neurons per layer --> more complex model, and probably you will obtain better accuracy.
  • More hidden layers --> more complex model, and again, probably you will obtain better accuracy.
  • WARNING Both approaches increase the chance of overfitting by increasing the complexity of model. You need to carefully evaluate them through statistically robust model selection procedures e.g. cross-validation

Be aware that incrementing the complexity of the model will increase the computational process and , as mentioned, be careful with overfitting.

I recommend try a small number of neurons first, and then, try to increase step by step seeing if some improvement is achieved in the model.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.