0% found this document useful (0 votes)
3 views

DL

The document discusses various implementations of neural networks, including Deep ANNs for handwritten digit recognition and regression tasks using supermarket sales data. It covers performance metrics for evaluating models, algorithms for training, and examples of using gradient descent and stochastic gradient descent. Additionally, it includes implementations of simple feedforward neural networks and convolutional neural networks for image classification using the MNIST and CIFAR-10 datasets.

Uploaded by

Anmol Agarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

DL

The document discusses various implementations of neural networks, including Deep ANNs for handwritten digit recognition and regression tasks using supermarket sales data. It covers performance metrics for evaluating models, algorithms for training, and examples of using gradient descent and stochastic gradient descent. Additionally, it includes implementations of simple feedforward neural networks and convolutional neural networks for image classification using the MNIST and CIFAR-10 datasets.

Uploaded by

Anmol Agarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

1. Deep Artificial Neural Network (ANN) model.

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:

Example: Handwritten Digit Recognition (MNIST 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 1: Import necessary libraries


import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.datasets import mnist
from sklearn.metrics import accuracy_score

# Step 2: Load dataset


(X_train, y_train), (X_test, y_test) = mnist.load_data()

# 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

# Step 4: Model Definition


model = Sequential([
Flatten(input_shape=(28, 28)), # Flatten 28x28 images into a vector
Dense(128, activation='relu'), # First hidden layer
Dense(64, activation='relu'), # Second hidden layer
Dense(10, activation='softmax') # Output layer (10 classes)
])

# Step 5: Compile the model


model.compile(optimizer=Adam(), loss='categorical_crossentropy', metrics=['accuracy'])

# Step 6: Train the model


model.fit(X_train, y_train, epochs=10, batch_size=32, validation_split=0.2)

# Step 7: Evaluate the model


y_pred = np.argmax(model.predict(X_test), axis=1) # Convert predictions
accuracy = accuracy_score(y_test_actual, y_pred)

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:

Example: Supermarket Sales Data

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

# Load and preprocess data


data = pd.read_csv('supermarket_sales.csv')
data = pd.get_dummies(data, columns=['Branch', 'Customer Type', 'Product Line'],
drop_first=True)
X = data[['Quantity', 'Unit Price', 'VAT'] + list(data.columns[6:])].values
y = data['Total Sales'].values

# Split and normalize data


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
scaler = StandardScaler()
X_train, X_test = scaler.fit_transform(X_train), scaler.transform(X_test)
X_train, X_test = map(torch.tensor, (X_train, X_test))
y_train, y_test = map(lambda y: torch.tensor(y, dtype=torch.float32).view(-1, 1), (y_train,
y_test))

# 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()

print(f'MSE: {((y_test.numpy() - y_pred) ** 2).mean():.4f}')

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

Gradient descent is evaluated based on:

1. Loss Reduction: The decrease in the loss function over iterations.


2. Convergence Speed: How quickly the algorithm reaches an optimal solution.
3. Step Size Stability: Ensuring a suitable learning rate (α\alphaα) to prevent divergence or
slow convergence.

Dataset Example:

For demonstration, assume we are optimizing a single parameter θ\thetaθ for a simple
quadratic loss function.

Sample Data:

Iteration θ (Parameter) Gradient Updated θ


1 5.0 10.0 4.9
2 4.9 9.8 4.802
3 4.802 9.6 4.706

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

# Simulating gradient values for demonstration


gradients = [10.0, 9.8, 9.6]

# Applying gradient descent update rule iteratively


for i in range(iterations):
theta = gradient_descent(theta, gradients[i], learning_rate)
print(f'Iteration {i+1}: Updated θ = {theta:.4f}')

This demonstrates the gradient descent update rule in a minimal and efficient manner.

4. Implementation and Evaluation of Stochastic Gradient Descent (SGD)

Performance Metrics:

SGD is evaluated based on:

1. Loss Reduction: How the loss decreases over iterations.


2. Convergence Speed: Faster than batch gradient descent but fluctuates more.
3. Step Size Stability: Learning rate (α\alphaα) must be well-tuned to avoid instability.

Dataset Example:

For demonstration, we use a simple linear regression model predicting y from x using SGD.

Sample Data (Training Examples):

x (Input) y (Target Output)


2 4.2
3 6.1
5 10.3
7 14.2

Algorithm Implementation:
python
CopyEdit
import numpy as np

# Stochastic Gradient Descent (SGD) Update Rule


def stochastic_gradient_descent(theta, X, y, learning_rate, epochs):
for epoch in range(epochs):
for i in range(len(X)): # Process each training example
x_i = X[i]
y_i = y[i]
gradient = 2 * x_i * (theta * x_i - y_i) # Derivative of MSE
Loss
theta = theta - learning_rate * gradient # SGD Update Rule
if epoch % 10 == 0:
print(f'Epoch {epoch}: θ = {theta:.4f}')
return theta

# Example dataset (Single feature x and target y)


X = np.array([2, 3, 5, 7])
y = np.array([4.2, 6.1, 10.3, 14.2])

# Initialize parameter and train using SGD


theta_initial = 0.0
learning_rate = 0.01
epochs = 50

theta_final = stochastic_gradient_descent(theta_initial, X, y, learning_rate,


epochs)
print(f'Final optimized parameter: θ = {theta_final:.4f}')

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:

To evaluate the model, we use:

1. Accuracy: Measures the percentage of correctly classified images.


2. Loss (Cross-Entropy): Measures the difference between predicted and actual labels.
3. Confusion Matrix: Evaluates classification performance across different classes.

Dataset:

We use the MNIST dataset, which consists of 28x28 grayscale images of handwritten digits (0-
9).

Image Example Label


🖊 (Digit '2') 2
🔢 (Digit '7') 7
✏ (Digit '0') 0

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)

testset = datasets.MNIST(root='./data', train=False, download=True, transform=transform)


testloader = torch.utils.data.DataLoader(testset, batch_size=64, shuffle=False)
# Define neural network
class NN(nn.Module):
def __init__(self):
super(NN, self).__init__()
self.model = nn.Sequential(
nn.Linear(28*28, 128), nn.ReLU(),
nn.Linear(128, 64), nn.ReLU(),
nn.Linear(64, 10)
)

def forward(self, x):


return self.model(x.view(-1, 28*28))

# Initialize model, loss, optimizer


model = NN()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)

# 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}%')

6. Implementation and Training of a Convolutional Neural Network (ConvNet) in PyTorch


Introduction:

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:

To evaluate the model, we use:


1. Accuracy: Percentage of correctly classified images.
2. Loss (Cross-Entropy): Measures the difference between predicted and actual labels.
3. Confusion Matrix: Evaluates classification performance across different classes.

Dataset:

We use the CIFAR-10 dataset, containing RGB images of 32x32 pixels across 10 categories.

Image Example Label


✈ (Airplane) 0
🚗 (Car) 1
🐱 (Cat) 2
🐶 (Dog) 3

Optimized Code for a CNN in PyTorch:

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))

def forward(self, x):


return self.fc(self.conv(x).view(x.size(0), -1))

# Train and Evaluate


model, criterion, optimizer = ConvNet(), nn.CrossEntropyLoss(),
optim.Adam(ConvNet().parameters(), lr=0.001)
for epoch in range(3): # Reduced epochs for brevity
for images, labels in trainloader:
optimizer.zero_grad()
loss = criterion(model(images), labels)
loss.backward()
optimizer.step()

correct = sum((torch.argmax(model(images), 1) == labels).sum().item() for images, labels in


testloader)
print(f'Accuracy: {100 * correct / len(testloader.dataset):.2f}%')

You might also like