Hrushi
Hrushi

Reputation: 325

How to calculate Hidden layer in ANN model

"I need help with my Artificial Neural Network model. I'm working on a project where I have to build a neural network, but I'm confused about the number of hidden layers and nodes in the model.

I've read several articles and posts, but I couldn't find a relevant answer. Can you please advise on how to determine the number of hidden layers and nodes needed to build a neural network model for tabular data? Below are the shapes of our training and testing datasets."

[*] Shape of Test data set : (3571, 1), (3571, 71),

[*] Shape of Train data set :(353512, 71), (353512, 1)

def fun():
    model = Sequential()
    model.add(Dense(32,input_dim =71, activation = 'relu', kernel_initializer = 'normal'))
    model.add(Dense(64,activation = 'relu', kernel_initializer = 'normal'))
    model.add(Dense(128,activation = 'relu', kernel_initializer = 'normal'))
    model.add(Dense(9,activation='softmax', kernel_initializer = 'normal'))
    model.compile(loss ='sparse_categorical_crossentropy',optimizer ='adam', metrics = ['accuracy'])
    return model
class_weights = class_weight.compute_class_weight(class_weight='balanced',classes=np.unique(Y),y=Y.event_type)
class_weights = dict(enumerate(class_weights))
ann_model = KerasClassifier(build_fn=fun, epochs=100, batch_size = 128, class_weight = class_weights)
f_model = ann_model.fit(X_train, Y_train.values.ravel())

"This is how I've defined my code. Could you please review it and let me know what's wrong? The model is running, but the accuracy is consistently low. Whenever I change the number of hidden layers or nodes, the accuracy fluctuates."

Upvotes: 0

Views: 255

Answers (1)

SARVESH DESHPANDE
SARVESH DESHPANDE

Reputation: 3

Don't select any number randomly. There are multiple concept or formula you will find. Please implement those with try and error. don't stick with the one concept or one number. You will definitely get what you are looking for. Please refer attached snapshot for first and second hidden layers selection. I hope it will help.

The sufficient number of hidden nodes in the first layer and second layer mentioned in below:

enter image description here

where m = output neuron, and N- number of hidden nodes

Upvotes: 0

Related Questions