DL
DL
Introduction:
A Deep Artificial Neural Network (ANN) is a computational model inspired by the human brain,
consisting of multiple layers of interconnected neurons. It is particularly useful for handling
complex patterns and non-linear relationships in data. Deep ANNs consist of an input layer,
multiple hidden layers, and an output layer. The model learns by adjusting the weights of
connections through backpropagation and optimization techniques such as gradient descent.
Performance Metrics:
To evaluate the performance of the Deep ANN model, we commonly use the following metrics:
1. Mean Squared Error (MSE): Measures the average squared difference between the
observed and predicted values.
2. Categorical Cross-Entropy (for classification): Measures the difference between the
predicted probability distribution and the actual distribution.
3. Accuracy: Percentage of correctly predicted instances in classification problems.
4. Precision, Recall, and F1-score: Used in classification problems to measure the model's
effectiveness in identifying classes.
5. Mean Absolute Error (MAE): The average of the absolute differences between
predicted and actual values in regression problems.
DATASET:
Scenario:
A deep learning model is trained to recognize handwritten digits (0-9) from grayscale images of
size 28x28 pixels.
Dataset Example:
• Input (X): Pixel values of the 28x28 image (flattened into a vector of 784 features).
• Output (Y): Digit label (0-9).
ALGORITHM:
python
CopyEdit
# Step 3: Preprocessing
X_train, X_test = X_train / 255.0, X_test / 255.0 # Normalize pixel values
y_train, y_test = tf.keras.utils.to_categorical(y_train, 10) # One-hot encode labels
y_test_actual = np.argmax(y_test, axis=1) # Convert back for evaluation
print(f'Accuracy: {accuracy:.4f}')
This model takes in an image of a handwritten digit, processes it through a Deep ANN, and
predicts the corresponding digit.
2. linear regression using deep neural network with pytorch using super market sales data
Introduction:
Deep Neural Networks (DNNs) can be used for regression tasks where the goal is to predict a
continuous output variable. Unlike simple linear regression, a DNN with multiple layers can
capture complex relationships between input features and target variables. In this case, we use
Supermarket Sales Data to predict total sales based on various attributes such as product type,
customer type, and store information.
Performance Metrics:
To evaluate the performance of the Deep Neural Network for regression, we use the following
metrics:
1. Mean Squared Error (MSE): Measures the average squared difference between actual
and predicted sales.
2. Root Mean Squared Error (RMSE): The square root of MSE, providing a scale-sensitive
error measure.
3. Mean Absolute Error (MAE): Measures the average magnitude of the errors in sales
predictions.
4. R-Squared (R²): Indicates how well the independent variables explain the variability in
sales.
DATASET:
Scenario:
A supermarket chain wants to predict total sales based on various factors such as product type,
customer type, and store branch location.
Dataset Example:
Branch Customer Type Product Line Quantity Unit Price VAT Total Sales
A Member Food & Beverages 5 20.00 1.50 101.50
B Normal Electronic Goods 2 50.00 3.50 107.00
C Member Fashion & Clothing 3 30.00 2.00 92.00
• Input (X): Branch, Customer Type, Product Line, Quantity, Unit Price, VAT
• Output (Y): Total Sales
Note: Categorical features (Branch, Customer Type, Product Line) must be converted into
numerical values using one-hot encoding before feeding them into the deep learning model.
ALGORITHM:
import torch
import torch.nn as nn
import torch.optim as optim
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# Define model
model = nn.Sequential(nn.Linear(X_train.shape[1], 64), nn.ReLU(),
nn.Linear(64, 32), nn.ReLU(),
nn.Linear(32, 1))
# Train model
criterion, optimizer = nn.MSELoss(), optim.Adam(model.parameters(), lr=0.01)
for epoch in range(300):
optimizer.zero_grad()
loss = criterion(model(X_train.float()), y_train)
loss.backward()
optimizer.step()
# Evaluate model
y_pred = model(X_test.float()).detach().numpy()
Results Example:
yaml
CopyEdit
Epoch 0, Loss: 9000.34
Epoch 100, Loss: 203.92
Epoch 200, Loss: 110.46
Epoch 300, Loss: 87.23
Epoch 400, Loss: 74.38
MSE: 72.57, RMSE: 8.52, R²: 0.91
3.Implementation and Evaluation of Gradient Descent Update Rule
Dataset Example:
For demonstration, assume we are optimizing a single parameter θ\thetaθ for a simple
quadratic loss function.
Sample Data:
Algorithm Implementation:
python
CopyEdit
# Gradient Descent Update Rule
def gradient_descent(theta, gradient, learning_rate):
return theta - learning_rate * gradient
# Example usage
theta = 5.0 # Initial parameter
learning_rate = 0.01
iterations = 3
This demonstrates the gradient descent update rule in a minimal and efficient manner.
Performance Metrics:
Dataset Example:
For demonstration, we use a simple linear regression model predicting y from x using SGD.
Algorithm Implementation:
python
CopyEdit
import numpy as np
This implementation efficiently updates θ (weights) after processing each data point, following
the SGD update rule. 🚀
5. Implementation and Evaluation of a Simple Neural Network for Image Classification using
PyTorch
Introduction:
A neural network for image classification takes an image as input and predicts the class it
belongs to. In this implementation, we use a simple feedforward neural network (FNN) trained
on the MNIST dataset (handwritten digits 0-9). The model consists of an input layer, hidden
layers with ReLU activation, and an output layer with Softmax activation for classification.
Performance Metrics:
Dataset:
We use the MNIST dataset, which consists of 28x28 grayscale images of handwritten digits (0-
9).
Algorithm Implementation:
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
import torchvision.datasets as datasets
# Load dataset
transform = transforms.ToTensor()
trainset = datasets.MNIST(root='./data', train=True, download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)
# Training loop
for epoch in range(5):
for images, labels in trainloader:
optimizer.zero_grad()
loss = criterion(model(images), labels)
loss.backward()
optimizer.step()
print(f'Epoch {epoch+1}, Loss: {loss.item():.4f}')
# Evaluation
correct = sum((torch.argmax(model(images), 1) == labels).sum().item() for images, labels in
testloader)
print(f'Accuracy: {100 * correct / len(testset):.2f}%')
A Convolutional Neural Network (CNN) is widely used for image classification tasks due to its
ability to capture spatial features using convolutional layers. In this implementation, we build a
CNN to classify the CIFAR-10 dataset, which consists of 60,000 images across 10 classes (e.g.,
airplane, car, cat, dog).
Performance Metrics:
Dataset:
We use the CIFAR-10 dataset, containing RGB images of 32x32 pixels across 10 categories.
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
import torchvision.datasets as datasets
# Load dataset
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])
trainloader = torch.utils.data.DataLoader(datasets.CIFAR10(root='./data', train=True,
download=True, transform=transform), batch_size=64, shuffle=True)
testloader = torch.utils.data.DataLoader(datasets.CIFAR10(root='./data', train=False,
transform=transform), batch_size=64, shuffle=False)
# Define CNN
class ConvNet(nn.Module):
def __init__(self):
super().__init__()
self.conv = nn.Sequential(nn.Conv2d(3, 32, 3, padding=1), nn.ReLU(), nn.MaxPool2d(2),
nn.Conv2d(32, 64, 3, padding=1), nn.ReLU(), nn.MaxPool2d(2))
self.fc = nn.Sequential(nn.Linear(64*8*8, 128), nn.ReLU(), nn.Linear(128, 10))