Sindhuja_Assignment-2_AI
Sindhuja_Assignment-2_AI
Sindhuja_Suresh_0817685_AI_Assignment-2
def softmax(x):
# Stabilize the inputs by subtracting the maximum value to avoid large expon
e_x = np.exp(x - np.max(x))
return e_x / e_x.sum()
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
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
self.bias_hidden = np.zeros(self.hidden_size)
self.bias_output = np.zeros(self.output_size)
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.T.dot(output_delta) * l
self.weights_input_hidden += X.T.dot(hidden_delta) * learning_rate
# Prepare to plot confusion matrices in a compact 1x3 grid (one row, three c
fig, axes = plt.subplots(1, 3, figsize=(18, 6)) # Create a 1x3 grid of subp
axes = axes.ravel() # Flatten the grid to make it easier to iterate
# 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)
# 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)
plt.show()
# 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)
# 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] = np.sum(image_row[i - 1:i + 2] * filter, axis=0)
return filtered_row
plt.show()
# 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[trainy.flatten() == class_1]
class_2_images = trainX[trainy.flatten() == class_2]
# Display original and Gabor-filtered image for one sample of each class
fig, axes = plt.subplots(2, 2, figsize=(8, 8))
for ax in axes.flat:
ax.axis('off')
plt.show()
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)
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%
# 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[trainy.flatten() == class_1]
class_2_images = trainX[trainy.flatten() == class_2]
# Display original and Gabor-filtered image for one sample of each class
fig, axes = plt.subplots(2, 2, figsize=(8, 8))
for ax in axes.flat:
ax.axis('off')
plt.show()
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
class SimpleRNN:
def __init__(self, input_size, hidden_size, output_size):
self.hidden_size = hidden_size
self.W_xh = np.random.randn(hidden_size, input_size) * 0.01
self.W_hh = np.random.randn(hidden_size, hidden_size) * 0.01
self.W_hy = np.random.randn(output_size, hidden_size) * 0.01
self.b_h = np.zeros((hidden_size, 1))
self.b_y = np.zeros((output_size, 1))
# 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
# Helper functions
def softmax(x):
e_x = np.exp(x - np.max(x))
return e_x / e_x.sum(axis=0)
for t in range(len(inputs)):
xs[t] = one_hot_encode(inputs[t], char_to_index)
hs[t] = np.tanh(np.dot(Wxh, xs[t]) + np.dot(Whh, hs[t - 1]) + bh)
ys[t] = np.dot(Why, hs[t]) + by
ps[t] = softmax(ys[t])
return xs, hs, ys, ps
for t in reversed(range(len(inputs))):
dy = np.copy(ps[t])
dy[char_to_index[targets[t]]] -= 1
dWhy += np.dot(dy, hs[t].T)
dby += dy
dh = np.dot(Why.T, dy) + dhnext
dhraw = (1 - hs[t] * hs[t]) * dh
dbh += dhraw
dWxh += np.dot(dhraw, xs[t].T)
dWhh += np.dot(dhraw, hs[t - 1].T)
dhnext = np.dot(Whh.T, dhraw)
if epoch % 10 == 0:
print(f'Epoch {epoch}, Loss: {smooth_loss}')
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()}
train_RNN(data, epochs=100)
test_seq = 'abc'
print(f"Next character after '{test_seq}': {predict(test_seq, char_to_index, ind
In [ ]: import numpy as np
import requests
# 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
# Create sequences
seq_length = 100 # Length of each input sequence
X, y = create_sequences(text_int, seq_length)
def _init_matrices(self):
if len(self.topology) > 1:
j = 1
for i in range(len(self.topology) - 1):
num_rows = self.topology[i]
num_cols = self.topology[j]
mat = self.RNG.uniform(-0.1, 0.1, size=(num_rows, num_cols))
self.weight_mats.append(mat)
j += 1
self.recurrent_weights = self.RNG.uniform(-0.1, 0.1, size=(self.topo
if epoch % 100 == 0:
print(f"Epoch {epoch}, Loss: {total_loss / len(X)}")
predicted_char_idx = np.argmax(output)
predicted_char = int_to_char[predicted_char_idx]
generated_text += predicted_char
input_seq.append(predicted_char_idx)