I was trying to add a hidden layer with dropout to my neural network. I found that the following code:
model.add(
tf.keras.layers.Dense(256, activation = "relu")
)
model.add(
tf.keras.layers.Dropout(0.5)
)
resulted in terrible accuracy (~65%) while the following code:
model.add(
tf.keras.layers.Dense(256, activation = "relu"),
tf.keras.layers.Dropout(0.5)
)
resulted in adequate accuracy (~95%). Can someone explain why these lines of code resulted in vastly different accuracies and which one is the proper way to add a hidden layer with dropout?
More generally, I was wondering if adding a neural network's layers one at a time is different than instantiating the neural network with one line, and if so why?
model = tf.keras.models.Sequential()
model.add(...) # Add one layer at a time
...
VS
model = tf.keras.models.Sequential([... (complete neural network) ...])
According to https://www.tensorflow.org/api_docs/python/tf/keras/Sequential#add, model.add
only takes one argument for layer, so your code
model.add(
tf.keras.layers.Dense(256, activation = "relu"),
tf.keras.layers.Dropout(0.5)
)
effectively only add the dense layer and pass in a second argument True
which is the default. If you compare it with
model.add(
tf.keras.layers.Dense(256, activation = "relu"),
True
)
The results should be the same.