Unit - 4 DL
Unit - 4 DL
Why PyTorch?
Dynamic Graphs: Easy debugging and flexible model design.
GPU Support: Built-in support for faster computation on GPUs.
Large Community: Extensive documentation and resources.
PyTorch simplifies deep learning, making it easy for both beginners and researchers
to experiment with advanced models.
Convolutional Neural Networks (CNN) with
PyTorch
CNNs are widely used for image-related tasks like classification, object detection, and
segmentation. Below is a simple example to classify images using CNN in PyTorch.
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, TensorDataset
# 2. Create DataLoader
dataset = TensorDataset(X, y)
dataloader = DataLoader(dataset, batch_size=2, shuffle=True)
model = SimpleCNN()
Explanation
1. Data:
o X: Represents 10 grayscale images of size 4x4 pixels. Each image has only 1
channel.
o y: Binary labels (0 or 1) representing the class of each image.
o Data is loaded using TensorDataset and DataLoader.
2. CNN Architecture:
o conv1: A convolutional layer with:
1 input channel (grayscale images).
2 filters (or kernels) that extract 2 feature maps.
2x2 kernel size, which scans the image.
o fc1: A fully connected layer that takes the flattened features (from conv1) and
predicts probabilities for 2 classes.
3. Training:
o Forward pass: The input passes through conv1 and fc1 to make predictions.
o Backward pass: Gradients are calculated and weights are updated to reduce
loss.
4. Testing:
o Predictions are obtained by applying the trained model to input data. The
predicted class is determined using torch.argmax().
What’s Happening?
1. The convolutional layer (conv1) extracts spatial features like edges or patterns from
the input images.
2. The ReLU activation function introduces non-linearity, helping the model learn
complex features.
3. The fully connected layer (fc1) combines the extracted features and makes a final
prediction.
4. The loss function (CrossEntropyLoss) measures how far the predicted output is from
the actual labels.
5. The model learns through optimization (SGD in this case).
Key Points
CNN Advantage: It automatically learns spatial features, making it ideal for image-
related tasks.
Tiny Example: A small input (4x4 images) and simple architecture (1 conv layer, 1
dense layer) keep the example easy to follow.
Scalability: The same principles apply to more complex models for larger datasets
like CIFAR or ImageNet.