Program-1:
1. Implement a basic Perceptron using Python and classify binary data.
Aim: To write a program implement a basic perceptron using python and
classify binary data.
Algorithm:
Step 1: Initialize Parameters
Set the learning rate.
Set the number of training epochs.
Initialize all weights to zero.
Initialize the bias to zero.
Step 2: Define Activation Function
Use a step (threshold) activation function:
o Output 1 if the input is greater than or equal to zero.
o Output 0 otherwise.
Step 3: Input Training Data
Provide input feature vectors.
Provide corresponding target output labels.
Step 4: Training Phase
Repeat for the specified number of epochs:
1. Select one training sample at a time.
2. Compute the weighted sum of inputs and bias.
3. Apply the activation function to obtain the predicted output.
4. Calculate the error as the difference between the actual output and the
predicted output.
5. Update the weights using the perceptron learning rule.
6. Update the bias using the same learning rule.
Step 5: Prediction Phase
Compute the weighted sum for new input samples.
Apply the activation function.
Generate the final predicted output.
Step 6: Termination
Stop training after completing all epochs.
Output the trained weights and bias.
Use the model to classify inputs.
Program:
import numpy as np
class Perceptron:
def __init__(self, learning_rate=0.1, epochs=10):
[Link] = learning_rate
[Link] = epochs
[Link] = None
[Link] = None
def activation(self, x):
return 1 if x >= 0 else 0
def fit(self, X, y):
n_samples, n_features = [Link]
# Initialize weights and bias
[Link] = [Link](n_features)
[Link] = 0
# Training loop
for _ in range([Link]):
for idx, x_i in enumerate(X):
linear_output = [Link](x_i, [Link]) + [Link]
y_predicted = [Link](linear_output)
# Perceptron update rule
update = [Link] * (y[idx] - y_predicted)
[Link] += update * x_i
[Link] += update
def predict(self, X):
linear_output = [Link](X, [Link]) + [Link]
return [Link]([[Link](i) for i in linear_output])
# Input features
X = [Link]([
[0, 0],
[0, 1],
[1, 0],
[1, 1],
])
# Labels (OR gate output)
y = [Link]([0, 1, 1, 1])
p = Perceptron(learning_rate=0.1, epochs=10)
# Train model
[Link](X, y)
# Predict on training data
predictions = [Link](X)
print("Predictions:", predictions)
print("Actual:", y)
print("Weights:", [Link])
print("Bias:", [Link])
program-2:
2. Build and train a Multilayer Perceptron (MLP) using TensorFlow/Keras on
the MNIST dataset.
Aim: To Build and train a Multilayer Perceptron (MLP) using TensorFlow/Keras
on the MNIST dataset.
Algorithm:
Step 1: Import Required Libraries
Load TensorFlow, Keras, NumPy, and Matplotlib for deep learning,
numerical operations, and visualization.
Step 2: Load Dataset
Load the MNIST dataset containing handwritten digit images and labels.
Split the dataset into training data and testing data.
Step 3: Data Preprocessing
Normalize pixel values of images from the range 0–255 to 0–1.
Convert class labels into one-hot encoded format for multi-class
classification.
Step 4: Build the Neural Network Model
Create a Sequential deep learning model.
Flatten the 28×28 image into a one-dimensional vector.
Add a fully connected hidden layer with ReLU activation.
Add another hidden layer with ReLU activation.
Add an output layer with Softmax activation to classify digits (0–9).
Step 5: Compile the Model
Select Adam optimizer for efficient training.
Use categorical cross-entropy as the loss function.
Choose accuracy as the performance metric.
Step 6: Train the Model
Train the model using training data.
Use a validation split to monitor performance.
Train for a fixed number of epochs with a defined batch size.
Step 7: Evaluate the Model
Test the trained model using unseen test data.
Calculate test loss and test accuracy.
Step 8: Prediction
Predict digit classes for test samples.
Compare predicted labels with actual labels.
Step 9: Visualization
Display a sample handwritten digit from the test dataset.
Predict and display the label for the selected image.
Step 10: End
Output the final accuracy and predictions.
Confirm successful handwritten digit recognition using deep learning.
Program:
import tensorflow as tf
from [Link] import Sequential
from [Link] import Dense, Flatten
from tensorflow import keras
from [Link] import to_categorical
from [Link] import mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# Normalize pixel values (0–255 → 0–1)
x_train = x_train.astype("float32") / 255.0
x_test = x_test.astype("float32") / 255.0
# One-hot encode labels
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
print("Training samples:", x_train.shape)
print("Test samples:", x_test.shape)
model = Sequential([
Flatten(input_shape=(28, 28)),
Dense(128, activation="relu"),
Dense(64, activation="relu"),
Dense(10, activation="softmax")
])
[Link](
optimizer="adam",
loss="categorical_crossentropy",
metrics=["accuracy"]
)
history = [Link](
x_train, y_train,
validation_split=0.1,
epochs=10,
batch_size=32
)
test_loss, test_acc = [Link](x_test, y_test)
print("Test Accuracy:", test_acc)
predictions = [Link](x_test[:5])
print("Predicted:", [Link](axis=1))
print("Actual:", y_test[:5].argmax(axis=1))
import numpy as np
import [Link] as plt
# Pick a test sample
idx = 0
sample = x_test[idx]
# Show image
[Link]([Link](28, 28), cmap='gray')
[Link]("Actual Label: " + str([Link](y_test[idx])))
[Link]()
# Predict
prediction = [Link]([Link](1, 28, 28))
print("Predicted Label:", [Link](prediction))
program-3:
3. Experiment with different activation functions (ReLU, sigmoid, tanh) and
observe effects on learning.
Aim: Experiment with different activation functions (ReLU, sigmoid, tanh) and
observe effects on learning.
Algorithm:
Step 1: Import Required Libraries
Import TensorFlow, Keras modules, and Matplotlib for deep learning and
visualization.
Step 2: Load the Dataset
Load the MNIST handwritten digit dataset.
Split the dataset into training and testing sets.
Step 3: Data Preprocessing
Normalize image pixel values to the range 0–1.
Convert class labels into one-hot encoded vectors.
Step 4: Define Neural Network Architecture
Create a function to build a deep neural network model.
Flatten the 28×28 image into a one-dimensional vector.
Add two fully connected hidden layers.
Apply a chosen activation function to the hidden layers.
Add an output layer with Softmax activation for multi-class classification.
Step 5: Compile the Model
Use the Adam optimizer for training.
Use categorical cross-entropy as the loss function.
Select accuracy as the evaluation metric.
Step 6: Select Activation Functions
Choose different activation functions (ReLU, Sigmoid, Tanh) for
comparison.
Step 7: Train the Models
Train a separate neural network for each activation function.
Use a portion of training data for validation.
Train for a fixed number of epochs with a defined batch size.
Store the training history for each model.
Step 8: Compare Model Performance
Extract validation accuracy values from each model.
Plot validation accuracy versus epochs for all activation functions.
Step 9: Analysis
Compare learning behavior and performance of different activation
functions.
Identify the activation function that provides the best validation
accuracy.
Step 10: End
Conclude the effectiveness of activation functions in deep learning
models.
Program:
import tensorflow as tf
from [Link] import mnist
from [Link] import Sequential
from [Link] import Dense, Flatten
from [Link] import to_categorical
import [Link] as plt
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train / 255.0
x_test = x_test / 255.0
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
def build_model(activation):
model = Sequential([
Flatten(input_shape=(28,28)),
Dense(256, activation=activation),
Dense(128, activation=activation),
Dense(10, activation="softmax")
])
[Link](optimizer="adam",
loss="categorical_crossentropy",
metrics=["accuracy"])
return model
activations = ["relu", "sigmoid", "tanh"]
histories = {}
for act in activations:
print(f"\nTraining model with activation: {[Link]()}\n")
model = build_model(act)
history = [Link](x_train, y_train,
validation_split=0.2,
epochs=10, batch_size=128,
verbose=1)
histories[act] = history
[Link](figsize=(12, 5))
for act in activations:
[Link](histories[act].history["val_accuracy"], label=f"{act}")
[Link]("Validation Accuracy Comparison")
[Link]("Epochs")
[Link]("Accuracy")
[Link]()
[Link](True)
[Link]()
program-4:
4. Compare optimizers (SGD, Adam, RMSprop) on convergence and
performance.
Aim: To write a program to Compare optimizers (SGD, Adam, RMSprop) on
convergence and performance.
Algorithm:
Step 1: Import Required Libraries
Import TensorFlow, Keras modules, and Matplotlib for deep learning and
visualization.
Step 2: Load the Dataset
Load the MNIST handwritten digit dataset.
Split the dataset into training data and testing data.
Step 3: Data Preprocessing
Normalize image pixel values to the range 0–1.
Convert class labels into one-hot encoded vectors for multi-class
classification.
Step 4: Define Neural Network Architecture
Create a function to build a deep neural network model.
Flatten each 28×28 image into a one-dimensional vector.
Add two fully connected hidden layers with ReLU activation.
Add an output layer with Softmax activation for digit classification.
Step 5: Compile the Model
Choose an optimizer for training.
Use categorical cross-entropy as the loss function.
Select accuracy as the performance metric.
Step 6: Select Optimizers
Choose different optimization algorithms:
o Stochastic Gradient Descent (SGD)
o Adam Optimizer
o RMSprop Optimizer
Step 7: Train the Models
Train a separate neural network model for each optimizer.
Use a validation split to monitor performance.
Train for a fixed number of epochs with a defined batch size.
Store the training history of each optimizer.
Step 8: Performance Evaluation
Extract validation accuracy from each trained model.
Compare learning behavior across optimizers.
Step 9: Visualization
Plot validation accuracy versus epochs for all optimizers on a single
graph.
Step 10: Analysis and Conclusion
Analyze which optimizer provides faster convergence and higher
accuracy.
Conclude the most effective optimizer for the given deep learning task.
Program:
import tensorflow as tf
from [Link] import mnist
from [Link] import Sequential
from [Link] import Dense, Flatten
from [Link] import to_categorical
import [Link] as plt
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train / 255.0
x_test = x_test / 255.0
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
def build_model(optimizer):
model = Sequential([
Flatten(input_shape=(28,28)),
Dense(256, activation="relu"),
Dense(128, activation="relu"),
Dense(10, activation="softmax")
])
[Link](optimizer=optimizer,
loss="categorical_crossentropy",
metrics=["accuracy"])
return model
optimizers = {
"SGD": [Link](learning_rate=0.01),
"Adam": [Link](),
"RMSprop": [Link]()
}
histories = {}
for opt_name, opt_obj in [Link]():
print(f"\nTraining with {opt_name} optimizer...\n")
model = build_model(opt_obj)
history = [Link](x_train, y_train,
validation_split=0.2,
epochs=10,
batch_size=128,
verbose=1)
histories[opt_name] = history
[Link](figsize=(12, 5))
for opt in [Link]():
[Link](histories[opt].history["val_accuracy"], label=f"{opt}")
[Link]("Validation Accuracy Comparison (SGD vs Adam vs RMSprop)")
[Link]("Epochs")
[Link]("Accuracy")
[Link]()
[Link](True)
[Link]()
program-5:
5. Implement Convolutional Neural Networks (CNN) for image classification
on CIFAR-10 dataset.
Aim: To Implement Convolutional Neural Networks (CNN) for image
classification on CIFAR-10 dataset.
Algorithm:
Step 1: Import Required Libraries
Import TensorFlow, Keras modules, NumPy, Matplotlib, and evaluation
utilities.
Step 2: Load the Dataset
Load the CIFAR-10 dataset containing color images of size 32×32
belonging to 10 different classes.
Split the dataset into training and testing sets.
Step 3: Data Preprocessing
Normalize image pixel values to the range 0–1.
Convert class labels into one-hot encoded format.
Step 4: Construct the CNN Model
Initialize a Sequential model.
Add a convolutional layer with multiple filters, ReLU activation, and
padding.
Add another convolutional layer to extract deeper features.
Apply max pooling to reduce spatial dimensions.
Use dropout to prevent overfitting.
Repeat convolution, pooling, and dropout layers with increased filters.
Flatten the feature maps into a one-dimensional vector.
Add a fully connected dense layer with ReLU activation.
Apply dropout for regularization.
Add an output layer with Softmax activation for multi-class classification.
Step 5: Compile the Model
Select the Adam optimizer.
Use categorical cross-entropy as the loss function.
Choose accuracy as the evaluation metric.
Step 6: Train the Model
Train the CNN using training data.
Specify batch size and number of epochs.
Validate the model using test data during training.
Step 7: Evaluate the Model
Evaluate the trained model on the test dataset.
Obtain test loss and test accuracy.
Step 8: Performance Visualization
Plot training and validation accuracy over epochs.
Plot training and validation loss over epochs.
Step 9: Prediction and Visualization
Predict class labels for test images.
Display sample test images with predicted class labels.
Step 10: Model Evaluation using Confusion Matrix
Generate predicted class labels for all test samples.
Compute the confusion matrix.
Visualize the confusion matrix to analyze classification performance.
Step 11: End
Conclude the CNN model’s effectiveness for image classification on
CIFAR-10.
Program:
import tensorflow as tf
from [Link] import cifar10
from [Link] import to_categorical
from [Link] import Sequential
from [Link] import Conv2D, MaxPooling2D, Flatten, Dense,
Dropout
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
# Normalize pixel values
x_train = x_train.astype("float32") / 255
x_test = x_test.astype("float32") / 255
# One-hot encode labels
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
model = Sequential()
[Link](Conv2D(32, (3,3), activation='relu', padding='same',
input_shape=(32,32,3)))
[Link](Conv2D(32, (3,3), activation='relu'))
[Link](MaxPooling2D(pool_size=(2,2)))
[Link](Dropout(0.25))
[Link](Conv2D(64, (3,3), activation='relu', padding='same'))
[Link](Conv2D(64, (3,3), activation='relu'))
[Link](MaxPooling2D(pool_size=(2,2)))
[Link](Dropout(0.25))
[Link](Flatten())
[Link](Dense(512, activation='relu'))
[Link](Dropout(0.5))
[Link](Dense(10, activation='softmax'))
# 3. Compile Model
[Link](
loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy']
)
# 4. Train Model
history = [Link](
x_train, y_train,
batch_size=64,
epochs=10,
validation_data=(x_test, y_test)
)
test_loss, test_acc = [Link](x_test, y_test)
print("Test Accuracy:", test_acc)
import [Link] as plt
[Link](figsize=(12,5))
# Accuracy plot
[Link](1,2,1)
[Link]([Link]['accuracy'], label='Train Accuracy')
[Link]([Link]['val_accuracy'], label='Validation Accuracy')
[Link]()
[Link]("Accuracy")
# Loss plot
[Link](1,2,2)
[Link]([Link]['loss'], label='Train Loss')
[Link]([Link]['val_loss'], label='Validation Loss')
[Link]()
[Link]("Loss")
[Link]()
import numpy as np
[Link](figsize=(10,10))
for i in range(9):
[Link](3,3,i+1)
[Link](x_test[i])
[Link]("Pred: " + class_names[[Link]([Link](x_test[i:i+1]))])
[Link]("off")
[Link]()
from [Link] import confusion_matrix, ConfusionMatrixDisplay
y_pred = [Link](x_test)
y_pred_classes = [Link](y_pred, axis=1)
# Correct prediction on test set
y_pred = [Link](x_test)
# X_test.shape = (10000, 32, 32, 3)
y_pred_classes = [Link](y_pred, axis=1) # shape = (10000,)
y_true = y_test.flatten()
from [Link] import confusion_matrix, ConfusionMatrixDisplay
cm = confusion_matrix(y_true, y_pred_classes)
disp = ConfusionMatrixDisplay(confusion_matrix=cm,
display_labels=class_names)
[Link](cmap=[Link], xticks_rotation='vertical')
[Link]("CIFAR-10 Confusion Matrix")
[Link]()