0% found this document useful (0 votes)
37 views22 pages

Sindhuja Assignment-2 AI

The document details an assignment focused on implementing a Softmax function and a neural network to model AND, OR, and XOR logic gates. It includes code snippets for testing the Softmax function with random data and for training a neural network using various configurations, along with results from experiments. Additionally, it discusses a single-layer perceptron model for binary classification, including data preparation and performance metrics.
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)
37 views22 pages

Sindhuja Assignment-2 AI

The document details an assignment focused on implementing a Softmax function and a neural network to model AND, OR, and XOR logic gates. It includes code snippets for testing the Softmax function with random data and for training a neural network using various configurations, along with results from experiments. Additionally, it discusses a single-layer perceptron model for binary classification, including data preparation and performance metrics.
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

11/13/24, 7:15 PM Assignment-2_AI

Sindhuja_Suresh_0817685_AI_Assignment-2

Part (a) Implementing the Softmax Function and Testing It

In [15]: import numpy as np

def softmax(x):
# Stabilize the inputs by subtracting the maximum value to avoid large expon
e_x = [Link](x - [Link](x))
return e_x / e_x.sum()

In [16]: # Testing Softmax with Randomly Generated Data


if __name__ == "__main__":
test_scenarios = [
[Link](1, 20, size=5),
[Link](5, 25, size=7),
[Link](10, 50, size=10)
]

for i, scenario in enumerate(test_scenarios, 1):


print(f"\nScenario {i}")
for j in range(3): # Repeat each scenario 3 times
print(f"Test {j+1}: Input: {scenario} -> Softmax: {softmax(scenario)

[Link] (4).html 1/22


11/13/24, 7:15 PM Assignment-2_AI

Scenario 1
Test 1: Input: [ 7 5 17 1 11] -> Softmax: [4.52853393e-05 6.12870423e-06 9.9747
5978e-01 1.12251133e-07
2.47249575e-03]
Test 2: Input: [ 7 5 17 1 11] -> Softmax: [4.52853393e-05 6.12870423e-06 9.9747
5978e-01 1.12251133e-07
2.47249575e-03]
Test 3: Input: [ 7 5 17 1 11] -> Softmax: [4.52853393e-05 6.12870423e-06 9.9747
5978e-01 1.12251133e-07
2.47249575e-03]

Scenario 2
Test 1: Input: [15 20 19 23 8 7 21] -> Softmax: [2.78675784e-04 4.13591535e-02
1.52151823e-02 8.30720805e-01
2.54119422e-07 9.34853110e-08 1.12425835e-01]
Test 2: Input: [15 20 19 23 8 7 21] -> Softmax: [2.78675784e-04 4.13591535e-02
1.52151823e-02 8.30720805e-01
2.54119422e-07 9.34853110e-08 1.12425835e-01]
Test 3: Input: [15 20 19 23 8 7 21] -> Softmax: [2.78675784e-04 4.13591535e-02
1.52151823e-02 8.30720805e-01
2.54119422e-07 9.34853110e-08 1.12425835e-01]

Scenario 3
Test 1: Input: [19 27 22 10 10 18 38 13 45 23] -> Softmax: [5.10443431e-12 1.5216
1042e-08 1.02525304e-10 6.29937238e-16
6.29937238e-16 1.87781644e-12 9.11051180e-04 1.26526276e-14
9.99088933e-01 2.78692670e-10]
Test 2: Input: [19 27 22 10 10 18 38 13 45 23] -> Softmax: [5.10443431e-12 1.5216
1042e-08 1.02525304e-10 6.29937238e-16
6.29937238e-16 1.87781644e-12 9.11051180e-04 1.26526276e-14
9.99088933e-01 2.78692670e-10]
Test 3: Input: [19 27 22 10 10 18 38 13 45 23] -> Softmax: [5.10443431e-12 1.5216
1042e-08 1.02525304e-10 6.29937238e-16
6.29937238e-16 1.87781644e-12 9.11051180e-04 1.26526276e-14
9.99088933e-01 2.78692670e-10]

Part (b) Implementing the Multilay with AND,OR,XOR gateser Neural Network with
Additional Outputs with AND,OR,XOR gates

In [1]: import numpy as np


import pandas as pd
from [Link] import confusion_matrix
import seaborn as sns
import [Link] as plt
import random

# Set random seed for reproducibility


[Link](828967)
[Link](828967)

# Load data from CSV file


data = pd.read_csv('F:\\Logic gates random [Link]')

# Extract input features (x, y) and target (OR, AND, XOR)


X = data[['x', 'y']].values
y_xor = data['Label: XOR'].values

# Generate AND and OR outputs based on the XOR data


y_and = np.logical_and(X[:, 0] > 0, X[:, 1] > 0).astype(int)

[Link] (4).html 2/22


11/13/24, 7:15 PM Assignment-2_AI

y_or = np.logical_or(X[:, 0] > 0, X[:, 1] > 0).astype(int)

# Stack the targets together into a 3-column array


y = np.column_stack([y_xor, y_and, y_or])

class NeuralNetwork:
def __init__(self, input_size, hidden_size, output_size, init_method='xavier
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
self.init_method = init_method

# Initialize weights and biases


if init_method == 'xavier':
self.weights_input_hidden = [Link](self.input_size, [Link]
self.weights_hidden_output = [Link](self.hidden_size, self.
else:
self.weights_input_hidden = [Link](self.input_size, [Link]
self.weights_hidden_output = [Link](self.hidden_size, self.

self.bias_hidden = [Link](self.hidden_size)
self.bias_output = [Link](self.output_size)

def sigmoid(self, x):


return 1 / (1 + [Link](-x))

def sigmoid_derivative(self, x):


return x * (1 - x)

def forward(self, X):


self.hidden_input = [Link](X, self.weights_input_hidden) + self.bias_hid
self.hidden_output = [Link](self.hidden_input)

self.final_input = [Link](self.hidden_output, self.weights_hidden_output


self.final_output = [Link](self.final_input)
return self.final_output

def backward(self, X, y, learning_rate=0.1):


output_error = y - self.final_output
output_delta = output_error * self.sigmoid_derivative(self.final_output)

hidden_error = output_delta.dot(self.weights_hidden_output.T)
hidden_delta = hidden_error * self.sigmoid_derivative(self.hidden_output

self.weights_hidden_output += self.hidden_output.[Link](output_delta) * l
self.weights_input_hidden += [Link](hidden_delta) * learning_rate

self.bias_output += [Link](output_delta, axis=0) * learning_rate


self.bias_hidden += [Link](hidden_delta, axis=0) * learning_rate

def train(self, X, y, epochs=10000, learning_rate=0.1):


for epoch in range(epochs):
[Link](X)
[Link](X, y, learning_rate)
if epoch % 1000 == 0:
loss = [Link]([Link](y - self.final_output)) # Mean squared
print(f"Epoch {epoch}, Loss: {loss}")

def predict(self, X):


return [Link](X)

[Link] (4).html 3/22


11/13/24, 7:15 PM Assignment-2_AI

# Experiment with different network configurations


def experiment(hidden_size, learning_rate):
nn = NeuralNetwork(input_size=2, hidden_size=hidden_size, output_size=3)
[Link](X, y, epochs=10000, learning_rate=learning_rate)
predictions = [Link](X)
predictions = [Link](predictions) # Round to nearest integer for classifi

# Prepare to plot confusion matrices in a compact 1x3 grid (one row, three c
fig, axes = [Link](1, 3, figsize=(18, 6)) # Create a 1x3 grid of subp
axes = [Link]() # Flatten the grid to make it easier to iterate

# Confusion matrix for each gate operation


for i, operation in enumerate(["XOR", "AND", "OR"]):
cm = confusion_matrix(y[:, i], predictions[:, i])
[Link](cm, annot=True, fmt="d", cmap="Blues", xticklabels=["0", "1"
axes[i].set_title(f"Confusion Matrix for {operation} Gate")
axes[i].set_xlabel("Predicted")
axes[i].set_ylabel("True")

plt.tight_layout() # Adjust the layout for better spacing


[Link]()

# Run experiments with different configurations


print("Experiment 1: 4 hidden neurons, learning rate 0.1")
experiment(hidden_size=4, learning_rate=0.1)

print("Experiment 2: 6 hidden neurons, learning rate 0.01")


experiment(hidden_size=6, learning_rate=0.01)

print("Experiment 3: 8 hidden neurons, learning rate 0.05")


experiment(hidden_size=8, learning_rate=0.05)

Experiment 1: 4 hidden neurons, learning rate 0.1


Epoch 0, Loss: 0.2793942227108188
Epoch 1000, Loss: 0.15756478602285318
Epoch 2000, Loss: 0.15719856746061647
Epoch 3000, Loss: 0.15704659470142465
Epoch 4000, Loss: 0.15695605021364786
Epoch 5000, Loss: 0.1568926302339849
Epoch 6000, Loss: 0.15684404967270502
Epoch 7000, Loss: 0.15680480276420103
Epoch 8000, Loss: 0.15677197735003442
Epoch 9000, Loss: 0.15674384302574845

[Link] (4).html 4/22


11/13/24, 7:15 PM Assignment-2_AI

Experiment 2: 6 hidden neurons, learning rate 0.01


Epoch 0, Loss: 0.2877635167445881
Epoch 1000, Loss: 0.015985662853869993
Epoch 2000, Loss: 0.010615244948812374
Epoch 3000, Loss: 0.008243619984823983
Epoch 4000, Loss: 0.0069262931537108895
Epoch 5000, Loss: 0.006071075249899509
Epoch 6000, Loss: 0.005459116792525534
Epoch 7000, Loss: 0.004998590188461029
Epoch 8000, Loss: 0.004642402693993645
Epoch 9000, Loss: 0.0043577337731462995

Experiment 3: 8 hidden neurons, learning rate 0.05


Epoch 0, Loss: 0.26950122416126926
Epoch 1000, Loss: 0.03040078284448532
Epoch 2000, Loss: 0.02651800625015735
Epoch 3000, Loss: 0.025281944674081017
Epoch 4000, Loss: 0.02470574138555958
Epoch 5000, Loss: 0.024334906410539692
Epoch 6000, Loss: 0.024051370548227473
Epoch 7000, Loss: 0.02381532328878226
Epoch 8000, Loss: 0.023609366242466612
Epoch 9000, Loss: 0.02342436835837076

2)Perceptrons and Backpropagation

Import Necessary Libraries

In [18]: import numpy as np


import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPRegressor
from [Link] import mean_squared_error

# Load the data


# Assuming data is stored in a CSV file with x values as columns, y values as ro
data = pd.read_csv('F:/A2 interesting [Link]', header=None).values # Adju

[Link] (4).html 5/22


11/13/24, 7:15 PM Assignment-2_AI

# Generate x, y input coordinates based on the data's shape


x_values = [Link](1, 7, [Link][1]) # Corresponding x values
y_values = [Link](1, 7, [Link][0]) # Corresponding y values
X, Y = [Link](x_values, y_values)

# Flatten the data to create pairs of (x, y) -> z


X_inputs = [Link]([[Link](), [Link]()]).T # Input (x, y) pairs
Z_outputs = [Link]() # Flattened target values (z values)

# Split the data into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X_inputs, Z_outputs, test_si

Part (a): Single-Layer Perceptron

In [6]: from sklearn.linear_model import Perceptron


from [Link] import accuracy_score, mean_squared_error
import numpy as np

# Check and handle NaN values in y_train and y_test if necessary


y_train = np.nan_to_num(y_train, nan=[Link](y_train))
y_test = np.nan_to_num(y_test, nan=[Link](y_train))

# Round y values for binary classification if not already binary


y_train_binary = [Link](y_train)
y_test_binary = [Link](y_test)

# Initialize the Perceptron model


single_layer_model = Perceptron(max_iter=1000, tol=1e-3, random_state=42)

# Train the model


single_layer_model.fit(X_train, y_train_binary)

# Make predictions
y_pred_train = single_layer_model.predict(X_train)
y_pred_test = single_layer_model.predict(X_test)

# Calculate accuracy
train_accuracy = accuracy_score(y_train_binary, y_pred_train)
test_accuracy = accuracy_score(y_test_binary, y_pred_test)

# Calculate Mean Squared Error if still needed


train_error = mean_squared_error(y_train_binary, y_pred_train)
test_error = mean_squared_error(y_test_binary, y_pred_test)

print(f"Single-Layer Perceptron Training Accuracy: {train_accuracy}")


print(f"Single-Layer Perceptron Test Accuracy: {test_accuracy}")
print(f"Single-Layer Perceptron Training MSE: {train_error}")
print(f"Single-Layer Perceptron Test MSE: {test_error}")

Single-Layer Perceptron Training Accuracy: 0.7861904761904762


Single-Layer Perceptron Test Accuracy: 0.7984790874524715
Single-Layer Perceptron Training MSE: 9.365238095238094
Single-Layer Perceptron Test MSE: 7.608365019011407

Part (b): Multilayer Perceptron for Complex Function

In [7]: # Initialize the MLP Regressor with a deep architecture


mlp_model = MLPRegressor(hidden_layer_sizes=(100, 50, 25), activation='relu', ma

[Link] (4).html 6/22


11/13/24, 7:15 PM Assignment-2_AI

# Train the model on the training data


mlp_model.fit(X_train, y_train)

# Make predictions and evaluate


y_train_pred = mlp_model.predict(X_train)
y_test_pred = mlp_model.predict(X_test)

# Calculate MSE for evaluation


train_mse = mean_squared_error(y_train, y_train_pred)
test_mse = mean_squared_error(y_test, y_test_pred)

print(f"Multilayer Perceptron Training MSE: {train_mse}")


print(f"Multilayer Perceptron Test MSE: {test_mse}")

Multilayer Perceptron Training MSE: 0.10987052146995327


Multilayer Perceptron Test MSE: 0.06719153913754096

Part (c): Applying Stochastic Gradient Descent (SGD)

In [8]: # Initialize MLP with small batch size to simulate SGD


sgd_model = MLPRegressor(hidden_layer_sizes=(100, 50, 25), activation='relu', ma
solver='sgd', batch_size=32, random_state=42)

# Train with SGD on the training data


sgd_model.fit(X_train, y_train)

# Predictions and evaluation for SGD-based model


y_train_pred_sgd = sgd_model.predict(X_train)
y_test_pred_sgd = sgd_model.predict(X_test)

# Calculate and print MSE


train_mse_sgd = mean_squared_error(y_train, y_train_pred_sgd)
test_mse_sgd = mean_squared_error(y_test, y_test_pred_sgd)

print(f"SGD Multilayer Perceptron Training MSE: {train_mse_sgd}")


print(f"SGD Multilayer Perceptron Test MSE: {test_mse_sgd}")

SGD Multilayer Perceptron Training MSE: 0.882578114905113


SGD Multilayer Perceptron Test MSE: 0.7247175279035392

[Link] filters and convolution networks

Part (a): Manually Applying Horizontal and Vertical Filters

In [9]: import numpy as np


import cv2 # OpenCV for image processing
import [Link] as plt
from [Link] import cifar10

# Load CIFAR-10 dataset


(X_train, y_train), (_, _) = cifar10.load_data()
image = X_train[0] # Pick any image for testing

# Define vertical and horizontal filters


vertical_filter = [Link]([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]])
horizontal_filter = [Link]([[1, 1, 1], [0, 0, 0], [-1, -1, -1]])

# Apply filters using OpenCV


vertical_edges = cv2.filter2D(image, -1, vertical_filter)

[Link] (4).html 7/22


11/13/24, 7:15 PM Assignment-2_AI

horizontal_edges = cv2.filter2D(image, -1, horizontal_filter)

# Plot original and filtered images


[Link](figsize=(10, 4))
[Link](1, 3, 1)
[Link]("Original Image")
[Link](image)
[Link](1, 3, 2)
[Link]("Vertical Edges")
[Link](vertical_edges)
[Link](1, 3, 3)
[Link]("Horizontal Edges")
[Link](horizontal_edges)
[Link]()

In [2]: import pickle


import numpy as np
import [Link] as plt
import cv2 # OpenCV for applying the filters

# Load CIFAR-10 batch


def unpickle(file):
with open(file, 'rb') as fo:
dict = [Link](fo, encoding='bytes')
return dict

# Load the CIFAR-10 batch file (change path accordingly)


batch = unpickle('C:/Users/sindhuja/Downloads/cifar-10-python/cifar-10-batches-p

# Image data (a 10000 x 3072 array)


data = batch[b'data']
# Image labels (a list of 10000 labels)
labels = batch[b'labels']

# List of CIFAR-10 class names


class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', '

# Get one image (for example, index 3)


image_index = 12
image = data[image_index].reshape(3, 32, 32).transpose(1, 2, 0) # Reshape and t

# Get the label of the image


image_label = labels[image_index]
image_name = class_names[image_label] # Map label to class name

# Display the original image with its name


[Link](image)

[Link] (4).html 8/22


11/13/24, 7:15 PM Assignment-2_AI

[Link](f'Class: {image_name}') # Show the class name as the title


[Link]('off') # Hide axes for better visualization
[Link]()

# Select a specific row from the image (e.g., row 10)


row = image[10, :, :] # Select row 10, which is 32 pixels with 3 RGB values

# Convert the row to grayscale by averaging the RGB values


grayscale_row = [Link](row, axis=1).astype(np.uint8)

# Reshape grayscale_row to 2D (1 row and 32 columns)


grayscale_row = grayscale_row.reshape(1, 32)

# Define a simple 3x3 vertical filter (emphasizes vertical edges)


vertical_filter = [Link]([
[1, 0, -1],
[1, 0, -1],
[1, 0, -1]
])

# Define a simple 3x3 horizontal filter (emphasizes horizontal edges)


horizontal_filter = [Link]([
[1, 1, 1],
[0, 0, 0],
[-1, -1, -1]
])

# Apply the vertical filter to the row (using filter2D from OpenCV)
vertical_result = cv2.filter2D(grayscale_row, -1, vertical_filter)

# Apply the horizontal filter to the row (using filter2D from OpenCV)
horizontal_result = cv2.filter2D(grayscale_row, -1, horizontal_filter)

# Plot the results


[Link](figsize=(12, 6))

# Display the original grayscale row


[Link](1, 3, 1)
[Link](grayscale_row, cmap='gray', aspect='auto')
[Link]('Original Row (Grayscale)')

# Display the result after applying the vertical filter


[Link](1, 3, 2)
[Link](vertical_result, cmap='gray', aspect='auto')
[Link]('Vertical Filter Applied')

# Display the result after applying the horizontal filter


[Link](1, 3, 3)
[Link](horizontal_result, cmap='gray', aspect='auto')
[Link]('Horizontal Filter Applied')

[Link]()

[Link] (4).html 9/22


11/13/24, 7:15 PM Assignment-2_AI

In [3]: import numpy as np


import [Link] as plt
from [Link] import cifar10

# Load CIFAR-10 dataset


(trainX, trainy), (testX, testy) = cifar10.load_data()

# Select a random image from the dataset (for example, the first image in the tr
image = trainX[0] # Shape is (32, 32, 3) for CIFAR-10 images (32x32 RGB image)

# Select a row from the image (for example, row 16)


row = image[16] # A row from the image (shape: (32, 3))

# Define horizontal and vertical filters (3x3)


horizontal_filter = [Link]([[ 1, 1, 1],
[ 0, 0, 0],

[Link] (4).html 10/22


11/13/24, 7:15 PM Assignment-2_AI

[-1, -1, -1]])

vertical_filter = [Link]([[ 1, 0, -1],


[ 1, 0, -1],
[ 1, 0, -1]])

# Function to apply a filter to an image row (simple convolution)


def apply_filter(image_row, filter):
# For simplicity, apply filter directly to the row (not full convolution for
row_len = image_row.shape[0]
filtered_row = np.zeros_like(image_row)

# Slide the filter across the row (since it's a row, we can only apply it ho
for i in range(1, row_len - 1):
filtered_row[i] = [Link](image_row[i - 1:i + 2] * filter, axis=0)

return filtered_row

# Apply filters to the row


horizontal_filtered_row = apply_filter(row, horizontal_filter)
vertical_filtered_row = apply_filter(row, vertical_filter)

# Plot the original image and filtered rows


fig, axes = [Link](1, 4, figsize=(16, 4))

# Display original full image


axes[0].imshow(image) # Display the full image
axes[0].set_title('Original Image')
axes[0].axis('off')

# Display original row


axes[1].imshow([row], cmap='gray', aspect='auto') # Display the single row of t
axes[1].set_title('Original Row')
axes[1].axis('off')

# Display horizontal filter result


axes[2].imshow([horizontal_filtered_row], cmap='gray', aspect='auto') # Display
axes[2].set_title('Horizontal Filter Applied')
axes[2].axis('off')

# Display vertical filter result


axes[3].imshow([vertical_filtered_row], cmap='gray', aspect='auto') # Display t
axes[3].set_title('Vertical Filter Applied')
axes[3].axis('off')

[Link]()

In [ ]: Part (b): Applying Gabor Filters and Using a CNN

[Link] (4).html 11/22


11/13/24, 7:15 PM Assignment-2_AI

In [4]: import numpy as np


import [Link] as plt
from [Link] import cifar10
from [Link] import gabor
from [Link] import Sequential
from [Link] import Conv2D, MaxPooling2D, Flatten, Dense
from [Link] import to_categorical
from sklearn.model_selection import train_test_split

# Load CIFAR-10 dataset


(trainX, trainy), (testX, testy) = cifar10.load_data()

# Filter the dataset for two classes (e.g., class 1: Automobile, class 9: Truck)
class_1, class_2 = 1, 9 # Automobile and Truck classes in CIFAR-10
class_1_images = trainX[[Link]() == class_1]
class_2_images = trainX[[Link]() == class_2]

# Combine and label the data


data = [Link]([class_1_images, class_2_images], axis=0)
labels = [Link]([[Link](len(class_1_images)), [Link](len(class_2_imag

# Apply Gabor filters to each image


def apply_gabor_filter(image, frequency=0.6, theta=0):
"""Apply Gabor filter to all color channels of an image."""
filtered_channels = []
for c in range([Link][2]): # Loop over each channel (R, G, B)
filtered, _ = gabor(image[:, :, c], frequency=frequency, theta=theta)
filtered_channels.append(filtered)
return [Link](filtered_channels, axis=-1) # Stack along the last axis (co

# Select frequency and orientation parameters for Gabor filter


frequency = 0.6
theta = [Link] / 4 # 45 degrees orientation

# Process images with the Gabor filter


filtered_data = [Link]([apply_gabor_filter(image, frequency=frequency, theta=t
filtered_data = filtered_data.reshape(filtered_data.shape[0], 32, 32, 3) # Resh

# Split data into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(filtered_data, labels, test_

# Convert labels to categorical (one-hot encoding)


y_train = to_categorical(y_train, 2)
y_test = to_categorical(y_test, 2)

# Build a simple CNN model


model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
MaxPooling2D((2, 2)),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D((2, 2)),
Flatten(),
Dense(64, activation='relu'),
Dense(2, activation='softmax') # Output layer for 2 classes
])

# Compile the model


[Link](optimizer='adam', loss='categorical_crossentropy', metrics=['accur

[Link] (4).html 12/22


11/13/24, 7:15 PM Assignment-2_AI

# Train the model


history = [Link](X_train, y_train, epochs=10, batch_size=32, validation_data=

# Evaluate the model


loss, accuracy = [Link](X_test, y_test)
print(f"Test Accuracy: {accuracy * 100:.2f}%")

# Display original and Gabor-filtered image for one sample of each class
fig, axes = [Link](2, 2, figsize=(8, 8))

# Original and filtered for class 1 (Automobile)


axes[0, 0].imshow(class_1_images[0])
axes[0, 0].set_title("Original (Class: Automobile)")
axes[0, 1].imshow(filtered_data[0].squeeze(), cmap='gray') # Filtered image
axes[0, 1].set_title("Gabor Filtered")

# Original and filtered for class 2 (Truck)


axes[1, 0].imshow(class_2_images[0])
axes[1, 0].set_title("Original (Class: Truck)")
axes[1, 1].imshow(filtered_data[len(class_1_images)].squeeze(), cmap='gray') #
axes[1, 1].set_title("Gabor Filtered")

for ax in [Link]:
[Link]('off')
[Link]()

C:\Users\sindhuja\anaconda3\Lib\site-packages\keras\src\layers\convolutional\base
_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a
layer. When using Sequential models, prefer using an `Input(shape)` object as the
first layer in the model instead.
super().__init__(activity_regularizer=activity_regularizer, **kwargs)

[Link] (4).html 13/22


11/13/24, 7:15 PM Assignment-2_AI

Epoch 1/10
250/250 ━━━━━━━━━━━━━━━━━━━━ 6s 13ms/step - accuracy: 0.5383 - loss: 7.6274 - val
_accuracy: 0.6185 - val_loss: 0.6477
Epoch 2/10
250/250 ━━━━━━━━━━━━━━━━━━━━ 3s 12ms/step - accuracy: 0.6573 - loss: 0.6180 - val
_accuracy: 0.7180 - val_loss: 0.5646
Epoch 3/10
250/250 ━━━━━━━━━━━━━━━━━━━━ 3s 12ms/step - accuracy: 0.7367 - loss: 0.5262 - val
_accuracy: 0.7425 - val_loss: 0.5146
Epoch 4/10
250/250 ━━━━━━━━━━━━━━━━━━━━ 3s 12ms/step - accuracy: 0.7817 - loss: 0.4496 - val
_accuracy: 0.7685 - val_loss: 0.4829
Epoch 5/10
250/250 ━━━━━━━━━━━━━━━━━━━━ 3s 12ms/step - accuracy: 0.8197 - loss: 0.3996 - val
_accuracy: 0.7220 - val_loss: 0.5916
Epoch 6/10
250/250 ━━━━━━━━━━━━━━━━━━━━ 3s 10ms/step - accuracy: 0.8396 - loss: 0.3574 - val
_accuracy: 0.7620 - val_loss: 0.5028
Epoch 7/10
250/250 ━━━━━━━━━━━━━━━━━━━━ 3s 11ms/step - accuracy: 0.8648 - loss: 0.3104 - val
_accuracy: 0.7755 - val_loss: 0.5291
Epoch 8/10
250/250 ━━━━━━━━━━━━━━━━━━━━ 3s 12ms/step - accuracy: 0.8869 - loss: 0.2624 - val
_accuracy: 0.7665 - val_loss: 0.5338
Epoch 9/10
250/250 ━━━━━━━━━━━━━━━━━━━━ 3s 11ms/step - accuracy: 0.9151 - loss: 0.2042 - val
_accuracy: 0.7435 - val_loss: 0.7290
Epoch 10/10
250/250 ━━━━━━━━━━━━━━━━━━━━ 3s 11ms/step - accuracy: 0.9258 - loss: 0.1799 - val
_accuracy: 0.7510 - val_loss: 0.7770
63/63 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 0.7393 - loss: 0.7797
Test Accuracy: 75.10%

[Link] (4).html 14/22


11/13/24, 7:15 PM Assignment-2_AI

In [5]: import numpy as np


import [Link] as plt
from [Link] import cifar10
from [Link] import gabor
from [Link] import Sequential
from [Link] import Conv2D, MaxPooling2D, Flatten, Dense
from [Link] import to_categorical
from sklearn.model_selection import train_test_split

# Load CIFAR-10 dataset


(trainX, trainy), (testX, testy) = cifar10.load_data()

# Filter the dataset for two classes (e.g., class 1: Automobile, class 9: Truck)
class_1, class_2 = 1, 9 # Automobile and Truck classes in CIFAR-10
class_1_images = trainX[[Link]() == class_1]
class_2_images = trainX[[Link]() == class_2]

# Combine and label the data


data = [Link]([class_1_images, class_2_images], axis=0)
labels = [Link]([[Link](len(class_1_images)), [Link](len(class_2_imag

# Apply Gabor filters to each image


def apply_gabor_filter(image, frequency=0.6, theta=0):

[Link] (4).html 15/22


11/13/24, 7:15 PM Assignment-2_AI

"""Apply Gabor filter to an image."""


filtered, _ = gabor(image[:, :, 0], frequency=frequency, theta=theta) # App
return filtered

# Select frequency and orientation parameters for Gabor filter


frequency = 0.6
theta = [Link] / 4

# Process images with the Gabor filter


filtered_data = [Link]([apply_gabor_filter(image, frequency=frequency, theta=t
filtered_data = filtered_data.reshape(filtered_data.shape[0], 32, 32, 1) # Resh

# Split data into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(filtered_data, labels, test_

# Convert labels to categorical


y_train = to_categorical(y_train, 2)
y_test = to_categorical(y_test, 2)

# Build a simple CNN model


model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 1)),
MaxPooling2D((2, 2)),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D((2, 2)),
Flatten(),
Dense(64, activation='relu'),
Dense(2, activation='softmax')
])

# Compile the model


[Link](optimizer='adam', loss='categorical_crossentropy', metrics=['accur

# Train the model


history = [Link](X_train, y_train, epochs=5, batch_size=32, validation_data=(

# Evaluate the model


loss, accuracy = [Link](X_test, y_test)
print(f"Test Accuracy: {accuracy * 100:.2f}%")

# Display original and Gabor-filtered image for one sample of each class
fig, axes = [Link](2, 2, figsize=(8, 8))

# Original and filtered for class 1


axes[0, 0].imshow(class_1_images[0])
axes[0, 0].set_title("Original (Class: Automobile)")
axes[0, 1].imshow(filtered_data[0].squeeze(), cmap='gray')
axes[0, 1].set_title("Gabor Filtered")

# Original and filtered for class 2


axes[1, 0].imshow(class_2_images[0])
axes[1, 0].set_title("Original (Class: Truck)")
axes[1, 1].imshow(filtered_data[len(class_1_images)].squeeze(), cmap='gray')
axes[1, 1].set_title("Gabor Filtered")

for ax in [Link]:
[Link]('off')
[Link]()

[Link] (4).html 16/22


11/13/24, 7:15 PM Assignment-2_AI

Epoch 1/5
250/250 ━━━━━━━━━━━━━━━━━━━━ 5s 12ms/step - accuracy: 0.5601 - loss: 4.3308 - val
_accuracy: 0.6895 - val_loss: 0.5777
Epoch 2/5
250/250 ━━━━━━━━━━━━━━━━━━━━ 3s 11ms/step - accuracy: 0.7114 - loss: 0.5511 - val
_accuracy: 0.7360 - val_loss: 0.5256
Epoch 3/5
250/250 ━━━━━━━━━━━━━━━━━━━━ 3s 11ms/step - accuracy: 0.7697 - loss: 0.4830 - val
_accuracy: 0.7540 - val_loss: 0.5201
Epoch 4/5
250/250 ━━━━━━━━━━━━━━━━━━━━ 3s 11ms/step - accuracy: 0.8119 - loss: 0.4137 - val
_accuracy: 0.7570 - val_loss: 0.5037
Epoch 5/5
250/250 ━━━━━━━━━━━━━━━━━━━━ 3s 11ms/step - accuracy: 0.8488 - loss: 0.3492 - val
_accuracy: 0.7640 - val_loss: 0.5165
63/63 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 0.7792 - loss: 0.5020
Test Accuracy: 76.40%

4. Recurrent network

a) Convert FFNN to RNN

[Link] (4).html 17/22


11/13/24, 7:15 PM Assignment-2_AI

In [1]: import numpy as np

class SimpleRNN:
def __init__(self, input_size, hidden_size, output_size):
self.hidden_size = hidden_size
self.W_xh = [Link](hidden_size, input_size) * 0.01
self.W_hh = [Link](hidden_size, hidden_size) * 0.01
self.W_hy = [Link](output_size, hidden_size) * 0.01
self.b_h = [Link]((hidden_size, 1))
self.b_y = [Link]((output_size, 1))

def forward(self, inputs):


h = [Link]((self.hidden_size, 1))
outputs = []
for x in inputs:
h = [Link]([Link](self.W_xh, x) + [Link](self.W_hh, h) + self.b_h)
y = [Link](self.W_hy, h) + self.b_y
[Link](y)
return outputs

# Test the RNN


rnn = SimpleRNN(input_size=1, hidden_size=10, output_size=1)
test_input = [[Link](1, 1) for _ in range(5)]
output = [Link](test_input)
print("Output:", output)

Output: [array([[0.00014617]]), array([[0.00035498]]), array([[-0.00035866]]), ar


ray([[2.4095945e-05]]), array([[0.00012321]])]

b) Simple Dataset - Predicting the Next Character in "abcdefghij"

In [6]: import numpy as np

# Hyperparameters
input_size = 27 # Size of the alphabet + 1 for a space (for simplicity)
hidden_size = 50 # Number of hidden units
output_size = 27 # Same as input_size for character prediction
learning_rate = 0.01

# Initialize weights and biases


Wxh = [Link](hidden_size, input_size) * 0.01 # input to hidden
Whh = [Link](hidden_size, hidden_size) * 0.01 # hidden to hidden
Why = [Link](output_size, hidden_size) * 0.01 # hidden to output
bh = [Link]((hidden_size, 1)) # hidden bias
by = [Link]((output_size, 1)) # output bias

# Helper functions
def softmax(x):
e_x = [Link](x - [Link](x))
return e_x / e_x.sum(axis=0)

def one_hot_encode(char, char_to_index):


one_hot = [Link]((len(char_to_index), 1))
one_hot[char_to_index[char]] = 1
return one_hot

def forward(inputs, hidden_prev):


xs, hs, ys, ps = {}, {}, {}, {}
hs[-1] = [Link](hidden_prev)

[Link] (4).html 18/22


11/13/24, 7:15 PM Assignment-2_AI

for t in range(len(inputs)):
xs[t] = one_hot_encode(inputs[t], char_to_index)
hs[t] = [Link]([Link](Wxh, xs[t]) + [Link](Whh, hs[t - 1]) + bh)
ys[t] = [Link](Why, hs[t]) + by
ps[t] = softmax(ys[t])
return xs, hs, ys, ps

def loss_and_gradients(inputs, targets, hidden_prev):


xs, hs, ys, ps = forward(inputs, hidden_prev)
loss = 0
for t in range(len(inputs)):
loss += -[Link](ps[t][char_to_index[targets[t]], 0])

# Backpropagation through time (BPTT)


dWxh, dWhh, dWhy = np.zeros_like(Wxh), np.zeros_like(Whh), np.zeros_like(Why
dbh, dby = np.zeros_like(bh), np.zeros_like(by)
dhnext = np.zeros_like(hs[0])

for t in reversed(range(len(inputs))):
dy = [Link](ps[t])
dy[char_to_index[targets[t]]] -= 1
dWhy += [Link](dy, hs[t].T)
dby += dy
dh = [Link](Why.T, dy) + dhnext
dhraw = (1 - hs[t] * hs[t]) * dh
dbh += dhraw
dWxh += [Link](dhraw, xs[t].T)
dWhh += [Link](dhraw, hs[t - 1].T)
dhnext = [Link](Whh.T, dhraw)

for dparam in [dWxh, dWhh, dWhy, dbh, dby]:


[Link](dparam, -5, 5, out=dparam) # Clip to mitigate exploding gradien

return loss, dWxh, dWhh, dWhy, dbh, dby, hs[len(inputs) - 1]

def train_RNN(data, epochs=100):


global Wxh, Whh, Why, bh, by
n = 0
smooth_loss = -[Link](1.0 / len(char_to_index)) * len(data)

for epoch in range(epochs):


hidden_prev = [Link]((hidden_size, 1))
inputs = data[:-1]
targets = data[1:]

loss, dWxh, dWhh, dWhy, dbh, dby, hidden_prev = loss_and_gradients(input


smooth_loss = smooth_loss * 0.999 + loss * 0.001

for param, dparam in zip([Wxh, Whh, Why, bh, by],


[dWxh, dWhh, dWhy, dbh, dby]):
param -= learning_rate * dparam

if epoch % 10 == 0:
print(f'Epoch {epoch}, Loss: {smooth_loss}')

def predict(input_seq, char_to_index, index_to_char):


hidden_prev = [Link]((hidden_size, 1))
for char in input_seq:
xs, hs, _, ps = forward(input_seq, hidden_prev)
pred_index = [Link](ps[len(input_seq) - 1])

[Link] (4).html 19/22


11/13/24, 7:15 PM Assignment-2_AI

pred_char = index_to_char[pred_index]
return pred_char

# Example Data
char_to_index = {char: index for index, char in enumerate(' abcdefghijklmnopqrst
index_to_char = {index: char for char, index in char_to_index.items()}

# Training data for 'abcdefghij'


data = ' abcdefghij' * 100

train_RNN(data, epochs=100)
test_seq = 'abc'
print(f"Next character after '{test_seq}': {predict(test_seq, char_to_index, ind

Epoch 0, Loss: 3625.4171772964037


Epoch 10, Loss: 3618.376371851605
Epoch 20, Loss: 3590.401336673658
Epoch 30, Loss: 3554.686090191355
Epoch 40, Loss: 3519.3076496882636
Epoch 50, Loss: 3484.2789370862656
Epoch 60, Loss: 3449.5976194904497
Epoch 70, Loss: 3415.260714619771
Epoch 80, Loss: 3381.265044920851
Epoch 90, Loss: 3347.607364218161
Next character after 'abc': d

c) Training on the Shakespeare Dataset

In [ ]: import numpy as np
import requests

# Download the Shakespeare dataset


url = "[Link]
response = [Link](url)
text = [Link]

# Preprocess the text (remove newlines and spaces, and create mappings)
chars = sorted(set(text)) # Unique characters in the text
char_to_int = {ch: i for i, ch in enumerate(chars)} # Map characters to integer
int_to_char = {i: ch for i, ch in enumerate(chars)} # Reverse map to get charac

# Vectorize the text


text_int = [char_to_int[ch] for ch in text]

# Function to create sequences for training


def create_sequences(data, seq_length=100):
X, y = [], []
for i in range(len(data) - seq_length):
[Link](data[i:i + seq_length])
[Link](data[i + seq_length]) # Next character is the target
return [Link](X), [Link](y)

# Create sequences
seq_length = 100 # Length of each input sequence
X, y = create_sequences(text_int, seq_length)

# Define the RNN model (similar to the previous one)


class RNN(object):
RNG = [Link].default_rng()

[Link] (4).html 20/22


11/13/24, 7:15 PM Assignment-2_AI

def __init__(self, topology: list[int] = []):


[Link] = topology
self.weight_mats = []
self.recurrent_weights = []
self._init_matrices()

def _init_matrices(self):
if len([Link]) > 1:
j = 1
for i in range(len([Link]) - 1):
num_rows = [Link][i]
num_cols = [Link][j]
mat = [Link](-0.1, 0.1, size=(num_rows, num_cols))
self.weight_mats.append(mat)
j += 1
self.recurrent_weights = [Link](-0.1, 0.1, size=([Link]

def feedforward(self, input_vector, hidden_state):


I = [Link](input_vector, self.weight_mats[0]) + [Link](hidden_state, sel
hidden_state = [Link](I)
output = [Link](hidden_state, self.weight_mats[1])
return output, hidden_state

def train(self, X, y, epochs=1000, learning_rate=0.001):


for epoch in range(epochs):
total_loss = 0
hidden_state = [Link]([Link][1])
for i in range(len(X)):
input_data = X[i]
target = y[i]

output, hidden_state = [Link](input_data, hidden_state

loss = [Link]((output - target) ** 2)


total_loss += loss

output_error = output - target


hidden_error = output_error.dot(self.weight_mats[1].T) * (1 - hi

self.weight_mats[1] -= learning_rate * [Link](hidden_state, ou


self.weight_mats[0] -= learning_rate * [Link](input_data, hidd
self.recurrent_weights -= learning_rate * [Link](hidden_state,

if epoch % 100 == 0:
print(f"Epoch {epoch}, Loss: {total_loss / len(X)}")

# Define the RNN topology


topology = [100, 256, len(chars)] # Input size = 100, hidden layer size = 256,
rnn = RNN(topology)

# Train the model on the Shakespeare dataset


[Link](X, y, epochs=1000)

# Generate text after training


seed_input = "To be or not to be"
input_seq = [char_to_int[ch] for ch in seed_input]
hidden_state = [Link](topology[1])

# Generate text based on the seed input


generated_text = seed_input

[Link] (4).html 21/22


11/13/24, 7:15 PM Assignment-2_AI

for _ in range(500): # Generate 500 characters


input_data = [Link](input_seq[-seq_length:]).reshape(1, -1)
output, hidden_state = [Link](input_data, hidden_state)

predicted_char_idx = [Link](output)
predicted_char = int_to_char[predicted_char_idx]

generated_text += predicted_char
input_seq.append(predicted_char_idx)

print(f"Generated text: {generated_text}")

Epoch 0, Loss: 527.2239487776425

[Link] (4).html 22/22

You might also like