Experiment-1
Write a python program to implement Perceptron for understanding single layer neural
network?
Code:
import numpy as np
class Perceptron:
def __init__(self, input_size, learning_rate=0.1, epochs=10):
[Link] = [Link](input_size + 1) # +1 for bias
[Link] = learning_rate
[Link] = epochs
def activation_function(self, x):
return 1 if x >= 0 else 0
def predict(self, x):
z = [Link](x, [Link][1:]) + [Link][0]
return self.activation_function(z)
def train(self, X, y):
for epoch in range([Link]):
print(f"\nEpoch {epoch+1}")
for xi, target in zip(X, y):
prediction = [Link](xi)
error = target - prediction
[Link][1:] += [Link] * error * xi
[Link][0] += [Link] * error # bias update
print(f"Input: {xi}, Target: {target}, Predicted: {prediction}, Updated Weights:
{[Link]}")
X = [Link]([[0, 0],
[0, 1],
[1, 0],
[1, 1]])
y = [Link]([0, 0, 0, 1]) # AND Gate output
model = Perceptron(input_size=2, learning_rate=0.1, epochs=10)
[Link](X, y)
print("\nFinal Predictions:")
for x in X:
print(f"Input: {x}, Predicted Output: {[Link](x)}")
OUTPUT:-
Input: [0 0], Target: 0, Predicted: 1, Updated Weights: [-0.1 0.1 0.1]
Input: [0 1], Target: 0, Predicted: 1, Updated Weights: [-0.2 0.1 0. ]
Input: [1 0], Target: 0, Predicted: 0, Updated Weights: [-0.2 0.1 0. ]
Input: [1 1], Target: 1, Predicted: 0, Updated Weights: [-0.1 0.2 0.1]
Epoch 3
Input: [0 0], Target: 0, Predicted: 0, Updated Weights: [-0.1 0.2 0.1]
Input: [0 1], Target: 0, Predicted: 1, Updated Weights: [-0.2 0.2 0. ]
Input: [1 0], Target: 0, Predicted: 1, Updated Weights: [-0.3 0.1 0. ]
Input: [1 1], Target: 1, Predicted: 0, Updated Weights: [-0.2 0.2 0.1]
Epoch 4
Input: [0 0], Target: 0, Predicted: 0, Updated Weights: [-0.2 0.2 0.1]
Input: [0 1], Target: 0, Predicted: 0, Updated Weights: [-0.2 0.2 0.1]
Input: [1 0], Target: 0, Predicted: 0, Updated Weights: [-0.2 0.2 0.1]
Input: [1 1], Target: 1, Predicted: 1, Updated Weights: [-0.2 0.2 0.1]
Epoch 5
Input: [0 0], Target: 0, Predicted: 0, Updated Weights: [-0.2 0.2 0.1]
Input: [0 1], Target: 0, Predicted: 0, Updated Weights: [-0.2 0.2 0.1]
Input: [1 0], Target: 0, Predicted: 0, Updated Weights: [-0.2 0.2 0.1]
Input: [1 1], Target: 1, Predicted: 1, Updated Weights: [-0.2 0.2 0.1]
Epoch 6
Input: [0 0], Target: 0, Predicted: 0, Updated Weights: [-0.2 0.2 0.1]
Input: [0 1], Target: 0, Predicted: 0, Updated Weights: [-0.2 0.2 0.1]
Input: [1 0], Target: 0, Predicted: 0, Updated Weights: [-0.2 0.2 0.1]
Input: [1 1], Target: 1, Predicted: 1, Updated Weights: [-0.2 0.2 0.1]
Epoch 7
Input: [0 0], Target: 0, Predicted: 0, Updated Weights: [-0.2 0.2 0.1]
Input: [0 1], Target: 0, Predicted: 0, Updated Weights: [-0.2 0.2 0.1]
Input: [1 0], Target: 0, Predicted: 0, Updated Weights: [-0.2 0.2 0.1]
Input: [1 1], Target: 1, Predicted: 1, Updated Weights: [-0.2 0.2 0.1]
Epoch 8
Input: [0 0], Target: 0, Predicted: 0, Updated Weights: [-0.2 0.2 0.1]
Input: [0 1], Target: 0, Predicted: 0, Updated Weights: [-0.2 0.2 0.1]
Input: [1 0], Target: 0, Predicted: 0, Updated Weights: [-0.2 0.2 0.1]
Input: [1 1], Target: 1, Predicted: 1, Updated Weights: [-0.2 0.2 0.1]
Epoch 9
Input: [0 0], Target: 0, Predicted: 0, Updated Weights: [-0.2 0.2 0.1]
Input: [0 1], Target: 0, Predicted: 0, Updated Weights: [-0.2 0.2 0.1]
Input: [1 0], Target: 0, Predicted: 0, Updated Weights: [-0.2 0.2 0.1]
Input: [1 1], Target: 1, Predicted: 1, Updated Weights: [-0.2 0.2 0.1]
Epoch 10
Input: [0 0], Target: 0, Predicted: 0, Updated Weights: [-0.2 0.2 0.1]
Input: [0 1], Target: 0, Predicted: 0, Updated Weights: [-0.2 0.2 0.1]
Input: [1 0], Target: 0, Predicted: 0, Updated Weights: [-0.2 0.2 0.1]
Input: [1 1], Target: 1, Predicted: 1, Updated Weights: [-0.2 0.2 0.1]
Final Predictions:
Input: [0 0], Predicted Output: 0
Input: [0 1], Predicted Output: 0
Input: [1 0], Predicted Output: 0
Input: [1 1], Predicted Output: 1
Experiment-2
Write a program to Visualize Activation Functions(Sigmoid, ReLU, Tanh)?
Code:-
import numpy as np
import [Link] as plt
def sigmoid(x):
return 1 / (1 + [Link](-x))
def relu(x):
return [Link](0, x)
def tanh(x):
return [Link](x)
x = [Link](-10, 10, 1000)
y_sigmoid = sigmoid(x)
y_relu = relu(x)
y_tanh = tanh(x)
[Link](figsize=(12, 6))
[Link](1, 3, 1)
[Link](x, y_sigmoid, color='blue')
[Link]("Sigmoid Function")
[Link]("x")
[Link]("sigmoid(x)")
[Link](True)
[Link](1, 3, 2)
[Link](x, y_relu, color='green')
[Link]("ReLU Function")
[Link]("x")
[Link]("ReLU(x)")
[Link](True)
[Link](1, 3, 3)
[Link](x, y_tanh, color='red')
[Link]("Tanh Function")
[Link]("x")
[Link]("tanh(x)")
[Link](True)
plt.tight_layout()
[Link]()
Output:-
Experiment-3
Write a program to Build a Simple Feedforward Neural Network?
Code:-
import numpy as np
def sigmoid(x):
return 1 / (1 + [Link](-x))
def sigmoid_derivative(x):
return sigmoid(x) * (1 - sigmoid(x))
def relu(x):
return [Link](0, x)
def relu_derivative(x):
return (x > 0).astype(float)
X = [Link]([[0, 0],
[0, 1],
[1, 0],
[1, 1]])
y = [Link]([[0],
[1],
[1],
[0]])
[Link](42)
input_size = 2
hidden_size = 2
output_size = 1
W1 = [Link](input_size, hidden_size) # weights input to hidden
b1 = [Link]((1, hidden_size)) # bias for hidden
W2 = [Link](hidden_size, output_size) # weights hidden to output
b2 = [Link]((1, output_size)) # bias for output
learning_rate = 0.1
epochs = 10000
for epoch in range(epochs):
z1 = [Link](X, W1) + b1
a1 = relu(z1)
z2 = [Link](a1, W2) + b2
a2 = sigmoid(z2)
loss = [Link]((y - a2) ** 2)
dz2 = (a2 - y) * sigmoid_derivative(z2)
dW2 = [Link](a1.T, dz2)
db2 = [Link](dz2, axis=0, keepdims=True)
dz1 = [Link](dz2, W2.T) * relu_derivative(z1)
dW1 = [Link](X.T, dz1)
db1 = [Link](dz1, axis=0, keepdims=True)
W2 -= learning_rate * dW2
b2 -= learning_rate * db2
W1 -= learning_rate * dW1
b1 -= learning_rate * db1
if epoch % 1000 == 0:
print(f"Epoch {epoch}, Loss: {loss:.4f}")
print("\nFinal Predictions:")
output = sigmoid([Link](relu([Link](X, W1) + b1), W2) + b2)
for i in range(len(X)):
print(f"Input: {X[i]}, Predicted Output: {output[i][0]:.4f}, Actual Output: {y[i][0]}")
Output:-
Epoch 0, Loss: 0.2616
Epoch 1000, Loss: 0.1264
Epoch 2000, Loss: 0.1254
Epoch 3000, Loss: 0.1252
Epoch 4000, Loss: 0.1252
Epoch 5000, Loss: 0.1251
Epoch 6000, Loss: 0.1251
Epoch 7000, Loss: 0.1251
Epoch 8000, Loss: 0.1251
Epoch 9000, Loss: 0.1251
Final Predictions:
Input: [0 0], Predicted Output: 0.5001, Actual Output: 0
Input: [0 1], Predicted Output: 0.9895, Actual Output: 1
Input: [1 0], Predicted Output: 0.5001, Actual Output: 1
Input: [1 1], Predicted Output: 0.0088, Actual Output: 0
Experiment-4
Write a program to MNISTDigit Classification using Keras?
Code:-
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
# 1. Load the MNIST dataset
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# 2. Normalize the pixel values (0–255) to (0–1)
x_train = x_train / 255.0
x_test = x_test / 255.0
# 3. One-hot encode the labels (0–9 → 10 classes)
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
# 4. Build the model
model = Sequential([
Flatten(input_shape=(28, 28)), # Flatten 28x28 images into 784 features
Dense(128, activation='relu'), # Hidden layer with 128 neurons
Dense(64, activation='relu'), # Hidden layer with 64 neurons
Dense(10, activation='softmax') # Output layer with 10 neurons (digits 0–9)
])
# 5. Compile the model
[Link](optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
# 6. Train the model
[Link](x_train, y_train, epochs=5, batch_size=32, validation_data=(x_test, y_test))
# 7. Evaluate the model
loss, accuracy = [Link](x_test, y_test)
print(f"Test Loss: {loss:.4f}")
print(f"Test Accuracy: {accuracy:.4f}")
# 8. Save the model
[Link]("mnist_digit_classifier.h5")
# Predict on first 5 test images
predictions = [Link](x_test[:5])
print("\nSample Predictions:")
for i in range(5):
[Link](x_test[i], cmap='gray')
[Link](f"Predicted: {predictions[i].argmax()}, Actual: {y_test[i].argmax()}")
[Link]('off')
[Link]()
Output:-
Experiment-6
Write a program to CIFAR-10 Image Classification for understanding Multiclass image
classification using CNN?
Code:-
# Step 1: Import Libraries
import tensorflow as tf
from [Link] import datasets, layers, models
import [Link] as plt
# Step 2: Load and Preprocess CIFAR-10 Dataset
# CIFAR-10 has 60,000 images (32x32 pixels, 3 channels, 10 classes)
(x_train, y_train), (x_test, y_test) = datasets.cifar10.load_data()
x_train = x_train.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0
# Step 3: Define Class Names
class_names = ['Airplane', 'Automobile', 'Bird', 'Cat', 'Deer',
'Dog', 'Frog', 'Horse', 'Ship', 'Truck']
# Step 4: Build CNN Model
model = [Link]([
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
[Link](),
[Link](64, activation='relu'),
[Link](10, activation='softmax') # 10 output neurons for 10 classes
])
# Step 5: Compile the Model
[Link](optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# Step 6: Train the Model
history = [Link](x_train, y_train, epochs=10,
validation_data=(x_test, y_test))
# Step 7: Evaluate the Model
test_loss, test_acc = [Link](x_test, y_test, verbose=2)
print(f"Test Accuracy: {test_acc:.2f}")
# Step 8: Plot Training and Validation Accuracy
[Link]([Link]['accuracy'], label='Train Accuracy')
[Link]([Link]['val_accuracy'], label='Validation Accuracy')
[Link]('Epoch')
[Link]('Accuracy')
[Link]()
[Link]()
# Step 9: Predict on Test Images
predictions = [Link](x_test[:5])
[Link](figsize=(10, 5))
for i in range(5):
[Link](1, 5, i+1)
[Link]([])
[Link]([])
[Link](x_test[i])
[Link](class_names[predictions[i].argmax()])
[Link]()
Output:
Experiment-5
Write a program to Create and Visualize CNN Layers?
Code:-
# Simple: Create & Visualize CNN
# (MNIST • Keras/TensorFlow)
import numpy as np
import [Link] as plt
import tensorflow as tf
from [Link] import layers, models
# 1) Load & preprocess MNIST (28x28 grayscale)
(x_train, y_train), (x_test, y_test) = [Link].load_data()
x_train = x_train.astype("float32") / 255.0
x_test =x_test.astype("float32") / 255.0
x_train = x_train[..., [Link]] # (N,28,28,1)
x_test =x_test[..., [Link]]
# 2) Build a tiny CNN
model = [Link]([
layers.Conv2D(16, (3,3), activation='relu', padding='same', input_shape=(28,28,1),
name="conv1"),
layers.MaxPooling2D((2,2), name="pool1"),
layers.Conv2D(32, (3,3), activation='relu', padding='same', name="conv2"),
layers.MaxPooling2D((2,2), name="pool2"),
[Link](),
[Link](64, activation='relu'),
[Link](10, activation='softmax')
])
[Link](optimizer="adam", loss="sparse_categorical_crossentropy",
metrics=["accuracy"])
[Link]()
# 3) Quick train (fast demo)
[Link](x_train[:12000], y_train[:12000], epochs=1, batch_size=64,
validation_data=(x_test[:2000], y_test[:2000]), verbose=1)
# 4) Visualize learned filters from the first conv layer
def show_conv_filters(layer, max_filters=16):
w = layer.get_weights()[0] # (kh, kw, in_ch, out_ch)
kh, kw, in_ch, out_ch = [Link]
n = min(out_ch, max_filters)
cols, rows = 8, int([Link](n/8))
[Link](figsize=(cols*1.3, rows*1.3))
for i in range(n):
k = w[:, :, 0, i] # grayscale -> channel 0
k = (k - [Link]()) / ([Link]() - [Link]() + 1e-9)
ax = [Link](rows, cols, i+1)
[Link](k, cmap="gray")
ax.set_xticks([]); ax.set_yticks([])
ax.set_title(f"f{i}", fontsize=8)
[Link](f"Filters: {[Link]}")
plt.tight_layout(); [Link]()
show_conv_filters(model.get_layer("conv1"), max_filters=16)
# 5) Visualize feature maps (activations) for one image
def show_feature_maps(model, img, layers_to_show=None, max_channels=16):
x = img[[Link], ...] # add batch dim
# pick conv/pool layers if none specified
if layers_to_show is None:
layers_to_show = [[Link] for l in [Link] if isinstance(l, (layers.Conv2D,
layers.MaxPooling2D))]
outputs = [model.get_layer(n).output for n in layers_to_show]
act_model = [Link](inputs=[Link], outputs=outputs)
activations = act_model.predict(x, verbose=0)
for name, act in zip(layers_to_show, activations):
a = act[0] # (h,w,c)
h, w, c = [Link]
n = min(c, max_channels)
cols, rows = 8, int([Link](n/8))
[Link](figsize=(cols*1.3, rows*1.3))
for i in range(n):
fm = a[:, :, i]
fm = (fm - [Link]()) / ([Link]() - [Link]() + 1e-9)
ax = [Link](rows, cols, i+1)
[Link](fm, cmap="viridis")
ax.set_xticks([]); ax.set_yticks([])
[Link](f"Feature maps: {name} ({h}x{w}x{c})")
plt.tight_layout(); [Link]()
idx = 0
sample = x_test[idx]
[Link]([Link](), cmap="gray"); plt
Output:-
Experiment-7
Write a program to implement Image Augmentation Techniques for application of
preprocessing and transformation.
Code:
import torch
import [Link] as plt
import torchvision
import [Link] as transforms
# 1. Define augmentation techniques
transform = [Link]([
[Link](p=0.5), # Flip image horizontally
[Link](20), # Rotate image by ±20 degrees
[Link](32, scale=(0.8, 1.0)), # Random crop & resize
[Link](brightness=0.2, contrast=0.2, saturation=0.2), # Color change
[Link]() # Convert to tensor
])
# 2. Load CIFAR-10 dataset
dataset = [Link].CIFAR10(root='./data', train=True, download=True,
transform=transform)
# 3. Show some augmented images
def show_images(dataset, num_images=6):
fig, axes = [Link](1, num_images, figsize=(12, 2))
for i in range(num_images):
img, label = dataset[i]
img = [Link](1, 2, 0).numpy() # Convert tensor → numpy image
axes[i].imshow(img)
axes[i].axis("off")
[Link]("Image Augmentation Techniques on CIFAR-10", fontsize=14)
[Link]()
show_images(dataset)
Output:
Experiment-8
Write a program using Dropout for Regularization to improve model generalization.
Code:
import tensorflow as tf
from [Link] import mnist
from [Link] import Sequential
from [Link] import Dense, Flatten, Dropout
from [Link] import to_categorical
import [Link] as plt
# Step 1: Load and Preprocess Data
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.astype('float32') / 255.0
X_test = X_test.astype('float32') / 255.0
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
# Step 2: Build Neural Network with Dropout
model = Sequential([
Flatten(input_shape=(28, 28)), # Flatten 28x28 image to 784
Dense(512, activation='relu'),
Dropout(0.5), # Dropout layer (50% neurons dropped)
Dense(256, activation='relu'),
Dropout(0.5), # Dropout again
Dense(10, activation='softmax') # Output layer (10 classes)
])
# Step 3: Compile the Model
[Link](optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
# Step 4: Train the Model
history = [Link](X_train, y_train,
validation_data=(X_test, y_test),
epochs=10,
batch_size=128,
verbose=2)
# Step 5: Evaluate
test_loss, test_acc = [Link](X_test, y_test, verbose=0)
print("Test Accuracy with Dropout:", test_acc)
# Step 6: Plot Accuracy & Loss
[Link](figsize=(12,5))
[Link](1,2,1)
[Link]([Link]['accuracy'], label='Train Accuracy')
[Link]([Link]['val_accuracy'], label='Validation Accuracy')
[Link]("Training vs Validation Accuracy (Dropout)")
[Link]("Epochs")
[Link]("Accuracy")
[Link]()
[Link](1,2,2)
[Link]([Link]['loss'], label='Train Loss')
[Link]([Link]['val_loss'], label='Validation Loss')
[Link]("Training vs Validation Loss (Dropout)")
[Link]("Epochs")
[Link]("Loss")
[Link]()
[Link]()
Output:
Experiment-9
Write a program to build Model using PyTorch.
Code:
import torch
import [Link] as nn
import [Link] as optim
from [Link] import DataLoader, TensorDataset
X = [Link](100, 1, 28, 28) # Random images
y = [Link](0, 10, (100,)) # Random labels (10 classes)
dataset = TensorDataset(X, y)
dataloader = DataLoader(dataset, batch_size=16, shuffle=True)
class SimpleCNN([Link]):
def __init__(self):
super(SimpleCNN, self).__init__()
self.conv1 = nn.Conv2d(1, 16, kernel_size=3, padding=1)
[Link] = nn.MaxPool2d(2, 2)
self.fc1 = [Link](16 * 14 * 14, 10) # 10 output classes
def forward(self, x):
x = [Link]([Link](self.conv1(x)))
x = [Link](-1, 16 * 14 * 14)
x = self.fc1(x)
return x
model = SimpleCNN()
criterion = [Link]()
optimizer = [Link]([Link](), lr=0.001)
for epoch in range(10): # Change to more epochs as needed
running_loss = 0.0
for inputs, labels in dataloader:
optimizer.zero_grad() # Zero gradients
outputs = model(inputs) # Forward pass
loss = criterion(outputs, labels) # Compute Loss
[Link]() # Backpropagation
[Link]() # Update weights
running_loss += [Link]()
print(f"Epoch [{epoch+1}], Loss: {running_loss/len(dataloader):.4f}")
[Link]()
with torch.no_grad():
sample_input = [Link](1, 1, 28, 28)
output = model(sample_input)
predicted_class = [Link](output, dim=1)
print(f"Predicted Class: {predicted_class.item()}")
Output:
Epoch [1], Loss: 2.8840
Epoch [2], Loss: 2.5861
Epoch [3], Loss: 1.9787
Epoch [4], Loss: 1.5296
Epoch [5], Loss: 1.2532
Epoch [6], Loss: 1.0451
Epoch [7], Loss: 0.8899
Epoch [8], Loss: 0.6983
Epoch [9], Loss: 0.5394
Epoch [10], Loss: 0.4490
Predicted Class: 2
Experiment-10
Write a program to Compare Training with and without Batch Normalization for Analyzing
performance and convergence?
Code:
import torch
import [Link] as nn
import [Link] as optim
import [Link] as F
from torchvision import datasets, transforms
from [Link] import DataLoader
import [Link] as plt
transform = [Link]([
[Link](),
[Link]((0.5,), (0.5,))])
train_dataset = [Link](root="./data", train=True, download=True,
transform=transform)
test_dataset = [Link](root="./data", train=False, download=True, transform=transform)
train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=1000, shuffle=False)
class SimpleCNN([Link]):
def __init__(self, use_bn=False):
super(SimpleCNN, self).__init__()
self.use_bn = use_bn
self.conv1 = nn.Conv2d(1, 32, 3, 1)
self.bn1 = nn.BatchNorm2d(32) if use_bn else [Link]()
self.conv2 = nn.Conv2d(32, 64, 3, 1)
self.bn2 = nn.BatchNorm2d(64) if use_bn else [Link]()
self.fc1 = [Link](9216, 128)
self.bn3 = nn.BatchNorm1d(128) if use_bn else [Link]()
self.fc2 = [Link](128, 10)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = [Link](x)
x = self.conv2(x)
x = self.bn2(x)
x = [Link](x)
x = F.max_pool2d(x, 2)
x = [Link](x, 1)
x = self.fc1(x)
x = self.bn3(x)
x = [Link](x)
x = self.fc2(x)
return F.log_softmax(x, dim=1)
def train_and_evaluate(use_bn=False, epochs=5):
device = [Link]("cuda" if [Link].is_available() else "cpu")
model = SimpleCNN(use_bn).to(device)
optimizer = [Link]([Link](), lr=0.001)
train_losses, test_accs = [], []
for epoch in range(1, epochs+1):
[Link]()
epoch_loss = 0
for data, target in train_loader:
data, target = [Link](device), [Link](device)
optimizer.zero_grad()
output = model(data)
loss = F.nll_loss(output, target)
[Link]()
[Link]()
epoch_loss += [Link]()
avg_loss = epoch_loss / len(train_loader)
train_losses.append(avg_loss)
[Link]()
correct = 0
with torch.no_grad():
for data, target in test_loader:
data, target = [Link](device), [Link](device)
output = model(data)
pred = [Link](dim=1, keepdim=True)
correct += [Link](target.view_as(pred)).sum().item()
acc = 100. * correct / len(test_loader.dataset)
test_accs.append(acc)
print(f"Epoch {epoch}: Loss={avg_loss:.4f}, Accuracy={acc:.2f}%")
return train_losses, test_accs
print("Training WITHOUT Batch Normalization...")
loss_no_bn, acc_no_bn = train_and_evaluate(use_bn=False, epochs=10)
print("\nTraining WITH Batch Normalization...")
loss_bn, acc_bn = train_and_evaluate(use_bn=True, epochs=10)
[Link](figsize=(12,5))
[Link](1,2,1)
[Link](loss_no_bn, label="Without BN")
[Link](loss_bn, label="With BN")
[Link]("Training Loss")
[Link]("Epoch")
[Link]("Loss")
[Link]()
[Link](1,2,2)
[Link](acc_no_bn, label="Without BN")
[Link](acc_bn, label="With BN")
[Link]("Test Accuracy")
[Link]("Epoch")
[Link]("Accuracy (%)")
[Link]()
[Link]()
Output:
Experiment-11
Write a program to Implement early stopping in model training (Keras)
Code:
import tensorflow as tf
from [Link] import mnist
from [Link] import Sequential
from [Link] import Dense, Flatten, Conv2D, MaxPooling2D
from [Link] import EarlyStopping
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(-1, 28, 28, 1).astype('float32') / 255.0
x_test = x_test.reshape(-1, 28, 28, 1).astype('float32') / 255.0
model = Sequential([
Conv2D(32, kernel_size=(3,3), activation='relu', input_shape=(28,28,1)),
MaxPooling2D(pool_size=(2,2)),
Flatten(),
Dense(128, activation='relu'),
Dense(10, activation='softmax')] )
[Link](optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
early_stop = EarlyStopping(monitor='val_loss', patience=3, restore_best_weights=True)
history = [Link](
x_train, y_train,
epochs=20,
batch_size=128,
validation_split=0.2,
callbacks=[early_stop],
verbose=2)
test_loss, test_acc = [Link](x_test, y_test, verbose=0)
print(f"Test Accuracy: {test_acc:.4f}")
import [Link] as plt
[Link]([Link]['loss'], label='Train Loss')
[Link]([Link]['val_loss'], label='Val Loss')
[Link](len([Link]['val_loss']) - early_stop.patience, color='r', linestyle='--',
label='Early Stop Trigger')
[Link]()
[Link]('Loss Curve with Early Stopping')
[Link]()
import tensorflow as tf
from [Link] import mnist
from [Link] import Sequential
from [Link] import Dense, Flatten, Conv2D, MaxPooling2D
from [Link] import EarlyStopping
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(-1, 28, 28, 1).astype('float32') / 255.0
x_test = x_test.reshape(-1, 28, 28, 1).astype('float32') / 255.0
model = Sequential([
Conv2D(32, kernel_size=(3,3), activation='relu', input_shape=(28,28,1)),
MaxPooling2D(pool_size=(2,2)),
Flatten(),
Dense(128, activation='relu'),
Dense(10, activation='softmax')] )
[Link](optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
early_stop = EarlyStopping(monitor='val_loss', patience=3, restore_best_weights=True)
history = [Link](
x_train, y_train,
epochs=20,
batch_size=128,
validation_split=0.2,
callbacks=[early_stop],
verbose=2)
test_loss, test_acc = [Link](x_test, y_test, verbose=0)
print(f"Test Accuracy: {test_acc:.4f}")
import [Link] as plt
[Link]([Link]['loss'], label='Train Loss')
[Link]([Link]['val_loss'], label='Val Loss')
[Link](len([Link]['val_loss']) - early_stop.patience, color='r', linestyle='--',
label='Early Stop Trigger')
[Link]()
[Link]('Loss Curve with Early Stopping')
[Link]()
Output:
Experiment-12
Write a program to Visualize Feature Maps from CNN (Intermediate Feature Visualization)
Code:
import tensorflow as tf
from [Link] import Model
from [Link] import Conv2D, MaxPooling2D, Flatten, Dense, Input
from [Link] import mnist
import [Link] as plt
import numpy as np
(x_train, y_train), (_, _) = mnist.load_data()
x_train = x_train.astype('float32') / 255.0
x_train = np.expand_dims(x_train, axis=-1) # Shape: (batch, 28, 28, 1)
inputs = Input(shape=(28, 28, 1))
x = Conv2D(32, (3,3), activation='relu', name='conv1')(inputs)
x = MaxPooling2D((2,2), name='pool1')(x)
x = Conv2D(64, (3,3), activation='relu', name='conv2')(x)
x = MaxPooling2D((2,2), name='pool2')(x)
x = Flatten()(x)
x = Dense(64, activation='relu')(x)
outputs = Dense(10, activation='softmax')(x)
model = Model(inputs, outputs)
[Link](optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
[Link](x_train, y_train, epochs=1, batch_size=128, verbose=2)
layer_outputs = [[Link] for layer in [Link] if 'conv' in [Link] or 'pool' in
[Link]]
feature_map_model = Model(inputs=[Link], outputs=layer_outputs)
sample_img = x_train[0]
sample_img = np.expand_dims(sample_img, axis=0) # Add batch dimension
feature_maps = feature_map_model.predict(sample_img)
for layer_name, feature_map in zip([[Link] for layer in [Link] if 'conv' in [Link]
or 'pool' in [Link]], feature_maps):
num_filters = feature_map.shape[-1]
size = feature_map.shape[1]
display_grid = [Link]((size, size * num_filters))
for i in range(num_filters):
x = feature_map[0, :, :, i]
x -= [Link]()
x /= ([Link]() + 1e-5)
x *= 64
x += 128
x = [Link](x, 0, 255).astype('uint8')
display_grid[:,i * size : (i + 1) * size] = x
scale = 20. / num_filters
[Link](figsize=(scale * num_filters, scale))
[Link](f'Feature Maps of Layer: {layer_name}')
[Link](False)
[Link](display_grid, aspect='auto', cmap='viridis')
[Link]()
Output: