![]() |
VOOZH | about |
In deep learning, overfitting is a common challenge where a model learns patterns that work well on training data but fails to generalize to unseen data.
One effective technique to mitigate overfitting is Dropout, which randomly deactivates a fraction of neurons during training. In TensorFlow, this is implemented using tf.keras.layers.Dropout.
Syntax of tf.keras.layers.Dropout:
tf.keras.layers.Dropout(rate, noise_shape=None, seed=None, **kwargs)
Parameters:
Letβs build a simple neural network using tf.keras with Dropout applied to prevent overfitting.
The model architecture contains fully connected neural network and the dropout layer is used after hidden layers to reduce overfitting:
Output:
Epoch 1/5
1875/1875 ββββββββββββββββββββ 14s 7ms/step - accuracy: 0.8072 - loss: 0.6074 - val_accuracy: 0.9584 - val_loss: 0.1393
Epoch 2/5
1875/1875 ββββββββββββββββββββ 17s 5ms/step - accuracy: 0.9444 - loss: 0.1890 - val_accuracy: 0.9667 - val_loss: 0.1116
Epoch 3/5
1875/1875 ββββββββββββββββββββ 9s 5ms/step - accuracy: 0.9566 - loss: 0.1458 - val_accuracy: 0.9705 - val_loss: 0.0962
Epoch 4/5
1875/1875 ββββββββββββββββββββ 7s 4ms/step - accuracy: 0.9606 - loss: 0.1285 - val_accuracy: 0.9696 - val_loss: 0.0972
Epoch 5/5
1875/1875 ββββββββββββββββββββ 11s 5ms/step - accuracy: 0.9651 - loss: 0.1115 - val_accuracy: 0.9752 - val_loss: 0.0902
<keras.src.callbacks.history.History at 0x7968dc3ca790>
By using tf.keras.layers.Dropout, we can randomly deactivate neurons, forcing the network to become more robust and preventing overfitting.