Master deep learning with TensorFlow and PyTorch. Learn to build neural networks, CNNs, and leverage transfer learning for powerful AI applications.
Deep Learning is a subset of machine learning that uses neural networks with multiple layers to learn hierarchical representations of data. TensorFlow and PyTorch are the two most popular frameworks for building deep learning models.
# Deep Learning Applications:
- Image Classification
- Object Detection
- Natural Language Processing
- Speech Recognition
- Generative AI (GANs, Diffusion Models)
- Reinforcement LearningCommon applications of deep learning
TensorFlow is an open-source deep learning framework developed by Google. It provides a comprehensive ecosystem for building and deploying ML models.
# Install TensorFlow
pip install tensorflow
# Verify installation
import tensorflow as tf
print(f"TensorFlow version: {tf.__version__}")
# Check GPU availability
print(f"GPU Available: {tf.config.list_physical_devices('GPU')}")Install and verify TensorFlow installation
Keras is the high-level API for TensorFlow that makes building neural networks intuitive and straightforward.
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
# Create a Sequential model
model = keras.Sequential([
layers.Dense(128, activation='relu', input_shape=(784,)),
layers.Dropout(0.2),
layers.Dense(64, activation='relu'),
layers.Dropout(0.2),
layers.Dense(10, activation='softmax')
])
# Compile the model
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
# View model architecture
model.summary()Create a simple feedforward neural network
# Train the model
history = model.fit(
X_train, y_train,
epochs=10,
batch_size=32,
validation_split=0.2,
verbose=1
)
# Evaluate on test set
test_loss, test_acc = model.evaluate(X_test, y_test)
print(f"Test accuracy: {test_acc:.4f}")Train and evaluate the neural network
CNNs are specialized neural networks for processing grid-like data such as images. They use convolutional layers to automatically learn spatial hierarchies.
from tensorflow.keras import layers, models
# Build CNN for image classification
model = models.Sequential([
# Convolutional layers
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
# Dense layers
layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dense(10, activation='softmax')
])
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)CNN architecture for image classification
PyTorch is a deep learning framework developed by Meta (Facebook) that provides flexibility and dynamic computation graphs, making it popular for research.
# Install PyTorch (CPU version)
pip install torch torchvision torchaudio
# Verify installation
import torch
print(f"PyTorch version: {torch.__version__}")
print(f"CUDA available: {torch.cuda.is_available()}")Install and verify PyTorch
PyTorch uses a more explicit approach to building neural networks through classes and the nn.Module base class.
import torch
import torch.nn as nn
import torch.optim as optim
# Define neural network class
class NeuralNetwork(nn.Module):
def __init__(self):
super(NeuralNetwork, self).__init__()
self.fc1 = nn.Linear(784, 128)
self.fc2 = nn.Linear(128, 64)
self.fc3 = nn.Linear(64, 10)
self.relu = nn.ReLU()
self.dropout = nn.Dropout(0.2)
def forward(self, x):
x = self.relu(self.fc1(x))
x = self.dropout(x)
x = self.relu(self.fc2(x))
x = self.dropout(x)
x = self.fc3(x)
return x
# Create model instance
model = NeuralNetwork()
print(model)Define a neural network class in PyTorch
# Define loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
# Training loop
for epoch in range(10):
for batch_idx, (data, target) in enumerate(train_loader):
# Forward pass
output = model(data)
loss = criterion(output, target)
# Backward pass and optimization
optimizer.zero_grad()
loss.backward()
optimizer.step()
print(f'Epoch {epoch+1}, Loss: {loss.item():.4f}')Training loop in PyTorch
Building convolutional neural networks in PyTorch follows the same class-based approach with Conv2d layers.
import torch.nn as nn
import torch.nn.functional as F
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.conv1 = nn.Conv2d(1, 32, 3, 1)
self.conv2 = nn.Conv2d(32, 64, 3, 1)
self.dropout1 = nn.Dropout(0.25)
self.dropout2 = nn.Dropout(0.5)
self.fc1 = nn.Linear(9216, 128)
self.fc2 = nn.Linear(128, 10)
def forward(self, x):
x = self.conv1(x)
x = F.relu(x)
x = self.conv2(x)
x = F.relu(x)
x = F.max_pool2d(x, 2)
x = self.dropout1(x)
x = torch.flatten(x, 1)
x = self.fc1(x)
x = F.relu(x)
x = self.dropout2(x)
x = self.fc2(x)
return F.log_softmax(x, dim=1)
model = CNN()CNN implementation in PyTorch
Transfer learning allows you to use pre-trained models and fine-tune them for your specific task, saving time and computational resources.
# TensorFlow/Keras Transfer Learning
from tensorflow.keras.applications import ResNet50
from tensorflow.keras import layers, models
# Load pre-trained ResNet50
base_model = ResNet50(
weights='imagenet',
include_top=False,
input_shape=(224, 224, 3)
)
# Freeze base model layers
base_model.trainable = False
# Add custom layers
model = models.Sequential([
base_model,
layers.GlobalAveragePooling2D(),
layers.Dense(256, activation='relu'),
layers.Dropout(0.5),
layers.Dense(10, activation='softmax')
])
model.compile(
optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy']
)Transfer learning with ResNet50 in TensorFlow
# PyTorch Transfer Learning
import torchvision.models as models
import torch.nn as nn
# Load pre-trained ResNet
resnet = models.resnet50(pretrained=True)
# Freeze all layers
for param in resnet.parameters():
param.requires_grad = False
# Replace final layer
num_features = resnet.fc.in_features
resnet.fc = nn.Linear(num_features, 10)
# Only the final layer will be trained
optimizer = optim.Adam(resnet.fc.parameters(), lr=0.001)Transfer learning with ResNet in PyTorch
Saving trained models is essential for deployment and future use. Both frameworks provide simple methods for model persistence.
# TensorFlow/Keras - Save model
model.save('my_model.h5') # HDF5 format
model.save('my_model') # SavedModel format
# Load model
from tensorflow import keras
loaded_model = keras.models.load_model('my_model.h5')
# Save only weights
model.save_weights('model_weights.h5')
model.load_weights('model_weights.h5')Save and load models in TensorFlow
# PyTorch - Save model
torch.save(model.state_dict(), 'model_weights.pth')
# Load model
model = NeuralNetwork()
model.load_state_dict(torch.load('model_weights.pth'))
model.eval() # Set to evaluation mode
# Save entire model
torch.save(model, 'complete_model.pth')
loaded_model = torch.load('complete_model.pth')Save and load models in PyTorch