Course Content
Deep Neural Networks
Advanced deep network architectures and training techniques
Introduction
Deep neural networks (DNNs) are neural networks with multiple hidden layers that allow learning of complex, non-linear patterns in data.
They power image recognition, natural language processing, and complex time-series forecasting, forming the backbone of modern deep learning systems.
Why Go Deep?
β
Learn hierarchical representations of data.
β
Capture complex patterns that shallow networks cannot.
β
Increase model capacity for challenging tasks.
Challenges with Deep Networks
- Vanishing and Exploding Gradients: Gradients can become too small or too large, hindering learning.
- Overfitting: More parameters increase the risk of overfitting to training data.
- Training Time: Deeper networks require more data and compute resources.
Solutions:
β
Use ReLU activations to mitigate vanishing gradients.
β
Apply dropout and regularization to reduce overfitting.
β
Use batch normalization to stabilize training.
Building a Deep Neural Network with Keras
We will build a DNN for classifying digits using the MNIST dataset.
1οΈβ£ Install and Import Libraries
pip install tensorflow
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, BatchNormalization
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical
2οΈβ£ Load and Prepare Data
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(-1, 28*28) / 255.0
x_test = x_test.reshape(-1, 28*28) / 255.0
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
3οΈβ£ Define the Deep Neural Network
model = Sequential([
Dense(512, activation='relu', input_shape=(784,)),
BatchNormalization(),
Dropout(0.3),
Dense(256, activation='relu'),
BatchNormalization(),
Dropout(0.3),
Dense(128, activation='relu'),
Dense(10, activation='softmax')
])
4οΈβ£ Compile and Train the Model
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
history = model.fit(x_train, y_train,
validation_split=0.1,
epochs=20,
batch_size=128)
5οΈβ£ Evaluate the Model
test_loss, test_acc = model.evaluate(x_test, y_test)
print("Test accuracy:", test_acc)
Visualization of Training
You can visualize accuracy and loss using matplotlib to monitor overfitting and learning progress.
import matplotlib.pyplot as plt
plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.title('Accuracy over Epochs')
plt.show()
Conclusion
β
Deep neural networks allow for learning complex, non-linear patterns.
β
They require careful handling to prevent overfitting and training instability.
β
Using frameworks like Keras simplifies building and training DNNs for practical applications.
Whatβs Next?
β
Explore Convolutional Neural Networks (CNNs) for image data.
β
Learn Recurrent Neural Networks (RNNs) for sequence data.
β
Apply hyperparameter tuning to optimize deep learning models.
Join the SuperML Community to share your DNN projects and learn advanced deep learning strategies with others.
Happy Deep Learning! π€