Reputation: 9160
The below code works perfectly okay. If I try to change all the 64s to 128s then I get an error about shape. Do I need to change the input data shape if I change the number of layers in an artificial neural network when using Keras? I didn't think so because it asks for input_dim which is correct.
Works:
model = Sequential()
model.add(Dense(64, input_dim=14, init='uniform'))
model.add(Activation('tanh'))
model.add(Dropout(0.5))
model.add(Dense(64, init='uniform'))
model.add(Activation('tanh'))
model.add(Dropout(0.5))
model.add(Dense(64, init='uniform'))
model.add(Activation('softmax'))
sgd3 = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='binary_crossentropy', optimizer=sgd3)
model.fit(X_train, y_train, nb_epoch=20, batch_size=16, show_accuracy=True, validation_split=0.2, verbose = 2)
Doesn't Work:
model = Sequential()
model.add(Dense(128, input_dim=14, init='uniform'))
model.add(Activation('tanh'))
model.add(Dropout(0.5))
model.add(Dense(128, init='uniform'))
model.add(Activation('tanh'))
model.add(Dropout(0.5))
model.add(Dense(128, init='uniform'))
model.add(Activation('softmax'))
sgd3 = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='binary_crossentropy', optimizer=sgd3)
model.fit(X_train, y_train, nb_epoch=20, batch_size=16, show_accuracy=True, validation_split=0.2, verbose = 2)
Upvotes: 5
Views: 4279
Reputation: 2277
You can use a different number of hidden layers and a different number of units/neurons for each of them on the same input.
Each Dense
except the last one can be seen as a hidden layer. The last Dense
should have a number of outputs equal to your desired output dimension (in your case the dimension of y
seems to be 64).
Try this:
model = Sequential()
model.add(Dense(128, input_dim=14, init='uniform'))
model.add(Activation('tanh'))
model.add(Dropout(0.5))
model.add(Dense(128, init='uniform'))
model.add(Activation('tanh'))
model.add(Dropout(0.5))
model.add(Dense(64, init='uniform'))
model.add(Activation('softmax'))
sgd3 = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='binary_crossentropy', optimizer=sgd3)
model.fit(X_train, y_train, nb_epoch=20, batch_size=16, show_accuracy=True, validation_split=0.2, verbose = 2)
Upvotes: 3
Reputation: 492
You do not change the number of layers at all in between your code-snippets. What you change is the number of units.
Dense(64) is a fully-connected layer with 64 hidden units. Since it is fully connected, the number of your input units also changes to that number, and hence also the requirements for the inputs.
Upvotes: 1