0% found this document useful (0 votes)
53 views36 pages

Implementing Perceptron in Python

1. The document discusses implementing a shallow neural network using Python, TensorFlow and Keras to classify handwritten digits from the MNIST dataset. It begins by introducing perceptrons and implementing a basic perceptron model to classify a simulated 2D dataset. Next, it visualizes the dataset and trains the perceptron model. Finally, it discusses classifying the MNIST dataset using a multi-layer perceptron neural network model.

Uploaded by

collection58209
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)
53 views36 pages

Implementing Perceptron in Python

1. The document discusses implementing a shallow neural network using Python, TensorFlow and Keras to classify handwritten digits from the MNIST dataset. It begins by introducing perceptrons and implementing a basic perceptron model to classify a simulated 2D dataset. Next, it visualizes the dataset and trains the perceptron model. Finally, it discusses classifying the MNIST dataset using a multi-layer perceptron neural network model.

Uploaded by

collection58209
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

1.

Demonstration and implementation of Shallow architecture using Python,


keyboard_arrow_down TensorFlow and Keras
i) Google Colaboratory - Cloning GitHub repository, Upload Data, Importing Kaggle's dataset, Basic
File operations

ii) Implementing Perceptron


Perceptrons were one of the first algorithms discovered in the field of AI. Its big significance was that it raised the hopes and expectations for
the field of neural networks. It is a machine learning algorithm that uses a single node or neuron to predict a class label for a row of data. It is a
type of neural network model and is considered one of the simplest types.

The Perceptron algorithm consists of four main components:

Input values
Weights and bias
Net sum
Activation function

import numpy as np
import pandas as pd
import [Link] as plt

from [Link] import make_classification

keyboard_arrow_down Creating the dataset


Here we generate a simulated dataset using scikit-learn's make_classification function. This creates a dataset with 2 input features and
binary target labels.

features, targets = make_classification(n_samples = 20, n_features = 2, n_informative = 1, n_redundant = 0, n_clusters_per_class = 1, ra

df = [Link](data=features, columns=['x1', 'x2'])


df['targets'] = targets
[Link]()

x1 x2 targets

0 -0.887629 0.784959 1

1 -0.012665 1.141704 1

2 -0.191836 0.984424 1

3 -0.267888 -0.330421 0

4 -0.935769 -1.883225 0

Next steps: Generate code with df


toggle_off View recommended plots

[Link]

(20, 2)

[Link]

(20,)

[Link](targets)

array([10, 10])

keyboard_arrow_down Visualizing the dataset


We can visualize the dataset by plotting the two input features colored by the target class. This gives us a sense of how linearly separable the
data is. We can see there is an approximate linear decision boundary that separates the two classes.
[Link](
features[targets == 0, 0],
features[targets == 0, 1],
marker = 'P',
markersize = 10,
linestyle = '',
label = 'Class 0'

[Link](
features[targets == 1, 0],
features[targets == 1, 1],
marker = '^',
markersize = 10,
linestyle = '',
label = 'Class 1'
)

[Link](loc = 2)

[Link](-2, 2)
[Link](-2, 2)

[Link]("Feature $x_1$", fontsize=12)


[Link]("Feature $x_2$", fontsize=12)

[Link]()
[Link]()

keyboard_arrow_down Implementing a Perceptron


We can now implement the perceptron algorithm in Python. The perceptron contains weights and bias parameters that can be updated during
training.

class Perceptron:
def __init__(self, num_features):
self.num_features = num_features
[Link] = [0.0 for _ in range(num_features)]
[Link] = 0

ppn = Perceptron(num_features = 2)
[Link]

[0.0, 0.0]

[Link]

Double-click (or enter) to edit


keyboard_arrow_down Implementing the forward function
The forward pass computes the weighted sum of the inputs and bias. An activation function thresholds this sum to produce a binary 0/1
prediction

class Perceptron:
def __init__(self, num_features):
self.num_features = num_features
[Link] = [0.0 for _ in range(num_features)]
[Link] = 0

def forward(self, x):


weighted_sum_z = [Link]
for i, _ in enumerate([Link]):
weighted_sum_z += x[i] * [Link][i]

if weighted_sum_z > 0:
prediction = 1
else:
prediction = 0

return prediction

import numpy as np

class Perceptron:
def __init__(self, num_features):
self.num_features = num_features
[Link] = [0.0 for _ in range(num_features)]
[Link] = 0

def forward(self, x):


weighted_sum_z = [Link]
for i, _ in enumerate([Link]):
weighted_sum_z += x[i] * [Link][i]

if weighted_sum_z > 0:
prediction = 1
else:
prediction = 0

return prediction

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


for epoch in range(epochs):
total_error = 0
for i in range(len(X)):
prediction = [Link](X[i])
error = y[i] - prediction
total_error += error

# Update weights and bias


[Link] = [w + learning_rate * error * x_i for w, x_i in zip([Link], X[i])]
[Link] += learning_rate * error

# Calculate accuracy
accuracy = [Link](X, y)
print(f"Epoch {epoch + 1}/{epochs}, Total Error: {total_error}, Accuracy: {accuracy}")

def evaluate(self, X, y):


correct_predictions = 0
for i in range(len(X)):
prediction = [Link](X[i])
if prediction == y[i]:
correct_predictions += 1
accuracy = correct_predictions / len(X)
return accuracy

# Sample data
X_perceptron = [Link]([[0, 0], [0, 1], [1, 0], [1, 1]])
y_perceptron = [Link]([0, 1, 1, 1])

# Create and train Perceptron model


perceptron_model = Perceptron(num_features=2)
perceptron_model.train(X_perceptron, y_perceptron, epochs=10, learning_rate=0.1)

# Evaluate the model


accuracy_perceptron = perceptron_model.evaluate(X_perceptron, y_perceptron)
print(f"Perceptron Accuracy: {accuracy perceptron}")
print(f Perceptron Accuracy: {accuracy_perceptron} )

output Epoch 1/10, Total Error: 1, Accuracy: 0.75


Epoch 2/10, Total Error: 0, Accuracy: 0.75
Epoch 3/10, Total Error: -1, Accuracy: 1.0
Epoch 4/10, Total Error: 0, Accuracy: 1.0
Epoch 5/10, Total Error: 0, Accuracy: 1.0
Epoch 6/10, Total Error: 0, Accuracy: 1.0
Epoch 7/10, Total Error: 0, Accuracy: 1.0
Epoch 8/10, Total Error: 0, Accuracy: 1.0
Epoch 9/10, Total Error: 0, Accuracy: 1.0
Epoch 10/10, Total Error: 0, Accuracy: 1.0
Perceptron Accuracy: 1.0

iii) Digit Classification: Neural network to classify MNIST dataset

In this notebook, we create a Multilayer Perceptron (MLP) model of the MNIST dataset.

Multilayer Perceptrons (MLPs) usually mean fully connected networks, that is, each neuron in one layer is connected to all neurons in the
next layer. The "fully-connectedness" of these networks makes them prone to overfitting the data.

These MLP models are also referred to as either deep feedforward networks or feedforward neural networks. MLPs are common in
simple logistic and linear regression problems.

So, the objective is to create a neural network for identifying numbers based on handwritten digits. For example, when the input to the
network is an image of a handwritten number 8, the corresponding prediction must also be the digit 8.

To both train and validate a neural network, there must be a sufficiently large dataset of handwritten digits.

The Modified National Institute of Standards and Technology dataset or MNIST dataset for short, is often considered as the Hello World!
of deep learning and is a suitable dataset for handwritten digit classification.

MNIST is used to explain and validate deep learning theories because the 70,000 samples it contains are small, yet sufficiently rich in
information (MNIST dataset is described later).

keyboard_arrow_down 2. Import necessary libraries


# This Python 3 environment comes with many helpful analytics libraries installed
# For example, here's several helpful packages to load in

import numpy as np # linear algebra


import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import [Link] as plt # plotting library


%matplotlib inline

from [Link] import Sequential


from [Link] import Dense , Activation, Dropout
from [Link] import Adam ,RMSprop
from keras import backend as K

# Any results you write to the current directory are saved as output.

keyboard_arrow_down MNIST dataset


MNIST is a collection of handwritten digits ranging from the number 0 to 9.

It has a training set of 60,000 images, and 10,000 test images that are classified into corresponding categories or labels.

To use the MNIST dataset in Keras, an API is provided to download and extract images and labels automatically.

The following Keras code shows how to access MNIST dataset, plot 25 random samples, and count the number of labels for train and test
datasets.
# import dataset
from [Link] import mnist

# load dataset
(x_train, y_train),(x_test, y_test) = mnist.load_data()

# count the number of unique train labels


unique, counts = [Link](y_train, return_counts=True)
print("Train labels: ", dict(zip(unique, counts)))

# count the number of unique test labels


unique, counts = [Link](y_test, return_counts=True)
print("\nTest labels: ", dict(zip(unique, counts)))

Train labels: {0: 5923, 1: 6742, 2: 5958, 3: 6131, 4: 5842, 5: 5421, 6: 5918, 7: 6265, 8: 5851, 9: 5949}

Test labels: {0: 980, 1: 1135, 2: 1032, 3: 1010, 4: 982, 5: 892, 6: 958, 7: 1028, 8: 974, 9: 1009}

keyboard_arrow_down Data visualization


The following code will help to sample the 25 random MNIST digits and visualize them.

indexes = [Link](0, x_train.shape[0], size=25)


images = x_train[indexes]
labels = y_train[indexes]

# plot the 25 mnist digits


[Link](figsize=(5,5))
for i in range(len(indexes)):
[Link](5, 5, i + 1)
image = images[i]
[Link](image, cmap='gray')
[Link]('off')

[Link]()
[Link]("[Link]")
[Link]('all')

2. Basic implementation of a deep Learning models in PyTorch and Tensor Flow. Tune
keyboard_arrow_down its performance by adding additional layers provided by the library

keyboard_arrow_down Basic Deep Learning Model in PyTorch:


import torch
import [Link] as nn
import [Link] as optim

# Define a simple neural network class


class SimpleNet([Link]):
def __init__(self, input_size, hidden_size, output_size):
super(SimpleNet, self).__init__()
self.fc1 = [Link](input_size, hidden_size)
[Link] = [Link]()
self.fc2 = [Link](hidden_size, output_size)

def forward(self, x):


x = self.fc1(x)
x = [Link](x)
x = self.fc2(x)
return x

# Sample data
input_size = 10
hidden_size = 20
output_size = 5
batch_size = 32
epochs = 10 # Define the number of epochs
# Generate random input and labels (for demonstration purposes)
inputs = [Link](batch_size, input_size)
labels = [Link](0, output_size, (batch_size,))

# Create an instance of the model


model_pytorch = SimpleNet(input_size, hidden_size, output_size)

# Define loss function and optimizer


criterion = [Link]()
optimizer = [Link](model_pytorch.parameters(), lr=0.01)

# Training loop
for epoch in range(epochs):
# Forward pass
outputs = model_pytorch(inputs)
loss = criterion(outputs, labels)

# Backward pass and optimization


optimizer.zero_grad()
[Link]()
[Link]()

# Save or use the trained model for inference

keyboard_arrow_down Enhancing PyTorch Model by Adding Layers:


# Enhanced PyTorch Model
class EnhancedNet([Link]):
def __init__(self, input_size, hidden_size1, hidden_size2, output_size):
super(EnhancedNet, self).__init__()
self.fc1 = [Link](input_size, hidden_size1)
self.relu1 = [Link]()
self.fc2 = [Link](hidden_size1, hidden_size2)
self.relu2 = [Link]()
self.fc3 = [Link](hidden_size2, output_size)

def forward(self, x):


x = self.fc1(x)
x = self.relu1(x)
x = self.fc2(x)
x = self.relu2(x)
x = self.fc3(x)
return x

# Create an instance of the enhanced model


model_pytorch_enhanced = EnhancedNet(input_size, 50, 30, output_size)

# Training loop (similar to the previous one)

keyboard_arrow_down Basic Deep Learning Model in TensorFlow:


# Save the trained model
model_tf.save("my_model.h5")

# Load the saved model for inference


loaded_model = [Link].load_model("my_model.h5")

# Example of using the loaded model for inference (replace with your actual input)
inference_input = [Link]((1, input_size))
inference_result = loaded_model.predict(inference_input)

print("Inference Result:", inference_result)

/usr/local/lib/python3.10/dist-packages/keras/src/engine/[Link]: UserWarning: You are saving your model as an HDF5 file vi


saving_api.save_model(
1/1 [==============================] - 0s 201ms/step
Inference Result: [[0.06069971 0.07249957 0.09608455 0.10964236 0.66107386]]

keyboard_arrow_down Enhancing TensorFlow Model by Adding Layers:


# Save the trained enhanced model
model_tf_enhanced.save("my_enhanced_model.h5")

# Load the saved enhanced model for inference


loaded_enhanced_model = [Link].load_model("my_enhanced_model.h5")

# Example of using the loaded enhanced model for inference (replace with your actual input)
inference_input = [Link]((1, input_size))
inference_result = loaded_enhanced_model.predict(inference_input)

print("Inference Result (Enhanced Model):", inference_result)

1/1 [==============================] - 0s 46ms/step


Inference Result (Enhanced Model): [[0.17575717 0.29464447 0.15700005 0.24500619 0.12759209]]

[Link] custom operations in PyTorch by using deep learning via gradient


keyboard_arrow_down descent; recursive chain rule (backpropagation); bias-variance tradeoff, regularization;
output units: linear, softmax; hidden units: tanh, RELU
let's create a simple custom operation using the recursive chain rule for backpropagation, involving a hidden layer with Tanh activation and a
linear output layer. We'll also discuss bias-variance tradeoff and regularization
import torch
import [Link] as nn
import [Link] as optim

# Custom Operation: Recursive Chain Rule


class CustomOperationFunction([Link]):
@staticmethod
def forward(ctx, input):
ctx.save_for_backward(input)
return [Link]()

@staticmethod
def backward(ctx, grad_output):
input, = ctx.saved_tensors
grad_input = grad_output * (1 - [Link]()**2)
return grad_input

# Custom Neural Network Model


class CustomNet([Link]):
def __init__(self, input_size, hidden_size, output_size):
super(CustomNet, self).__init__()
self.fc1 = [Link](input_size, hidden_size)
[Link] = [Link]
self.fc2 = [Link](hidden_size, output_size)

def forward(self, x):


x = self.fc1(x)
x = [Link](x)
x = self.fc2(x)
return x

# Sample data
input_size = 10
hidden_size = 20
output_size = 5
batch_size = 32

# Create an instance of the custom model


model_pytorch_custom = CustomNet(input_size, hidden_size, output_size)

# Define loss function and optimizer


criterion = [Link]()
optimizer = [Link](model_pytorch_custom.parameters(), lr=0.01)

# Training loop
epochs = 10
for epoch in range(epochs):
# Forward pass
outputs = model_pytorch_custom(inputs)
loss = criterion(outputs, labels)

# Backward pass and optimization


optimizer.zero_grad()
[Link]()
[Link]()

# Print loss for monitoring training progress


print(f'Epoch [{epoch+1}/{epochs}], Loss: {[Link]()}')

# Save or use the trained model for inference

Epoch [1/10], Loss: 1.566769003868103


Epoch [2/10], Loss: 1.5645911693572998
Epoch [3/10], Loss: 1.5624265670776367
Epoch [4/10], Loss: 1.560274600982666
Epoch [5/10], Loss: 1.5581356287002563
Epoch [6/10], Loss: 1.5560091733932495
Epoch [7/10], Loss: 1.5538952350616455
Epoch [8/10], Loss: 1.5517940521240234
Epoch [9/10], Loss: 1.549704909324646
Epoch [10/10], Loss: 1.5476276874542236

The model will have a hidden layer with ReLU activation, and we'll discuss bias-variance tradeoff and regularization. Additionally, the output
layer will use softmax activation.
import torch
import [Link] as nn
import [Link] as optim

# Custom Operation: Recursive Chain Rule with ReLU Activation


class CustomOperationFunction([Link]):
@staticmethod
def forward(ctx, input):
ctx.save_for_backward(input)
return [Link]()

@staticmethod
def backward(ctx, grad_output):
input, = ctx.saved_tensors
grad_input = grad_output * (input > 0).float()
return grad_input

# Custom Neural Network Model with ReLU Activation


class CustomNet([Link]):
def __init__(self, input_size, hidden_size, output_size):
super(CustomNet, self).__init__()
self.fc1 = [Link](input_size, hidden_size)
[Link] = [Link]
self.fc2 = [Link](hidden_size, output_size)

def forward(self, x):


x = self.fc1(x)
x = [Link](x)
x = self.fc2(x)
return x

# Sample data
input_size = 10
hidden_size = 20
output_size = 5
batch_size = 32

# Create an instance of the custom model


model_pytorch_custom = CustomNet(input_size, hidden_size, output_size)

# Define loss function and optimizer


criterion = [Link]()
optimizer = [Link](model_pytorch_custom.parameters(), lr=0.01)

# Training loop
epochs = 10
for epoch in range(epochs):
# Forward pass
outputs = model_pytorch_custom(inputs)
loss = criterion(outputs, labels)

# Backward pass and optimization


optimizer.zero_grad()
[Link]()
[Link]()

# Print loss for monitoring training progress


print(f'Epoch [{epoch+1}/{epochs}], Loss: {[Link]()}')

# Save or use the trained model for inference

Epoch [1/10], Loss: 1.6587367057800293


Epoch [2/10], Loss: 1.6560542583465576
Epoch [3/10], Loss: 1.6533976793289185
Epoch [4/10], Loss: 1.6507666110992432
Epoch [5/10], Loss: 1.6481602191925049
Epoch [6/10], Loss: 1.6455782651901245
Epoch [7/10], Loss: 1.6430206298828125
Epoch [8/10], Loss: 1.640486240386963
Epoch [9/10], Loss: 1.6379752159118652
Epoch [10/10], Loss: 1.6354867219924927

The model will have a hidden layer with Tanh activation, and we'll discuss bias-variance tradeoff and regularization. Additionally, the output layer
will use softmax activation.
import torch
import [Link] as nn
import [Link] as optim

# Custom Operation: Recursive Chain Rule with Tanh Activation


class CustomOperationFunction([Link]):
@staticmethod
def forward(ctx, input):
ctx.save_for_backward(input)
return [Link]()

@staticmethod
def backward(ctx, grad_output):
input, = ctx.saved_tensors
grad_input = grad_output * (1 - [Link]()**2)
return grad_input

# Custom Neural Network Model with Tanh Activation


class CustomNet([Link]):
def __init__(self, input_size, hidden_size, output_size):
super(CustomNet, self).__init__()
self.fc1 = [Link](input_size, hidden_size)
[Link] = [Link]
self.fc2 = [Link](hidden_size, output_size)

def forward(self, x):


x = self.fc1(x)
x = [Link](x)
x = self.fc2(x)
return x

# Sample data
input_size = 10
hidden_size = 20
output_size = 5
batch_size = 32

# Create an instance of the custom model


model_pytorch_custom = CustomNet(input_size, hidden_size, output_size)

# Define loss function and optimizer


criterion = [Link]()
optimizer = [Link](model_pytorch_custom.parameters(), lr=0.01)

# Training loop
epochs = 10
for epoch in range(epochs):
# Forward pass
outputs = model_pytorch_custom(inputs)
loss = criterion(outputs, labels)

# Backward pass and optimization


optimizer.zero_grad()
[Link]()
[Link]()

# Print loss for monitoring training progress


print(f'Epoch [{epoch+1}/{epochs}], Loss: {[Link]()}')

# Save or use the trained model for inference

Epoch [1/10], Loss: 1.6401724815368652


Epoch [2/10], Loss: 1.6379419565200806
Epoch [3/10], Loss: 1.6357260942459106
Epoch [4/10], Loss: 1.6335244178771973
Epoch [5/10], Loss: 1.6313371658325195
Epoch [6/10], Loss: 1.6291640996932983
Epoch [7/10], Loss: 1.6270047426223755
Epoch [8/10], Loss: 1.624859094619751
Epoch [9/10], Loss: 1.6227271556854248
Epoch [10/10], Loss: 1.6206086874008179

The model will have a hidden layer with ReLU activation, and we'll discuss bias-variance tradeoff and regularization. The output layer will use a
linear activation
import torch
import [Link] as nn
import [Link] as optim

# Custom Operation: Recursive Chain Rule with ReLU Activation


class CustomOperationFunction([Link]):
@staticmethod
def forward(ctx, input):
ctx.save_for_backward(input)
return [Link]()

@staticmethod
def backward(ctx, grad_output):
input, = ctx.saved_tensors
grad_input = grad_output * (input > 0).float()
return grad_input

# Custom Neural Network Model with ReLU Activation


class CustomNet([Link]):
def __init__(self, input_size, hidden_size, output_size):
super(CustomNet, self).__init__()
self.fc1 = [Link](input_size, hidden_size)
[Link] = [Link]
self.fc2 = [Link](hidden_size, output_size)

def forward(self, x):


x = self.fc1(x)
x = [Link](x)
x = self.fc2(x)
return x

# Sample data
input_size = 10
hidden_size = 20
output_size = 5
batch_size = 32

# Create an instance of the custom model


model_pytorch_custom = CustomNet(input_size, hidden_size, output_size)

# Define loss function and optimizer


criterion = [Link]()
optimizer = [Link](model_pytorch_custom.parameters(), lr=0.01)
# Assuming you have your actual input data and labels
# Replace this with your data
inputs = [Link](batch_size, input_size)
labels = [Link](0, output_size, (batch_size,))
# Training loop
epochs = 10
for epoch in range(epochs):
# Forward pass
outputs = model_pytorch_custom(inputs)
loss = criterion(outputs, labels)

# Backward pass and optimization


optimizer.zero_grad()
[Link]()
[Link]()

# Print loss for monitoring training progress


print(f'Epoch [{epoch+1}/{epochs}], Loss: {[Link]()}')

# Save or use the trained model for inference

Epoch [1/10], Loss: 1.6439942121505737


Epoch [2/10], Loss: 1.642916202545166
Epoch [3/10], Loss: 1.641843557357788
Epoch [4/10], Loss: 1.6407759189605713
Epoch [5/10], Loss: 1.639709234237671
Epoch [6/10], Loss: 1.638649582862854
Epoch [7/10], Loss: 1.6375948190689087
Epoch [8/10], Loss: 1.6365455389022827
Epoch [9/10], Loss: 1.6355013847351074
Epoch [10/10], Loss: 1.6344619989395142

keyboard_arrow_down [Link] a simple CNN starting from filtering, Convolution and pooling operations
and arithmetic of these with Visualization in PyTorch and Tensorflow.
PyTorch Implementation:

import torch
import [Link] as nn
import [Link] as optim
import torchvision
import [Link] as transforms
import [Link] as plt

# Load and preprocess the CIFAR-10 dataset


transform = [Link]([
[Link](),
])

train_dataset = [Link].CIFAR10(root='./data', train=True, download=True, transform=transform)


train_loader = [Link](train_dataset, batch_size=4, shuffle=True)

# Define a simple CNN model


class SimpleCNN([Link]):
def __init__(self):
super(SimpleCNN, self).__init__()
self.conv1 = nn.Conv2d(3, 16, kernel_size=3, padding=1)
[Link] = [Link]()
[Link] = nn.MaxPool2d(kernel_size=2, stride=2)

def forward(self, x):


x = self.conv1(x)
x = [Link](x)
x = [Link](x)
return x

# Instantiate the model


model_pytorch = SimpleCNN()

# Visualize some intermediate results


data_iter = iter(train_loader)
images, _ = data_iter.__next__()

# Plot original image


[Link]([Link].make_grid(images).numpy().transpose(1, 2, 0))
[Link]('Original Image')
[Link]()

# Plot a specific channel from the filtered and pooled image


output_pytorch = model_pytorch(images)
channel_to_visualize = 0 # Adjust this value based on the number of channels in your output
channel_image = output_pytorch[0, channel_to_visualize].detach().numpy()
[Link](channel_image, cmap='gray') # Assuming a grayscale channel
[Link](f'Filtered and Pooled Channel {channel_to_visualize} (PyTorch)')
[Link]()
Files already downloaded and verified

TensorFlow Implementation:

import tensorflow as tf
from [Link] import layers, models, datasets
import [Link] as plt

# Load and preprocess the CIFAR-10 dataset


(train_images, _), (_, _) = datasets.cifar10.load_data()
train_images = train_images.astype('float32') / 255.0

# Define a simple CNN model


model_tf = [Link]([
layers.Conv2D(16, (3, 3), padding='same', input_shape=(32, 32, 3)),
[Link]('relu'),
layers.MaxPooling2D(pool_size=(2, 2)),
])

# Visualize some intermediate results


# Plot original image
[Link]([Link].array_to_img(train_images[0]))
[Link]('Original Image')
[Link]()

# Plot the first channel of the filtered and pooled image


output_tf = model_tf.predict(tf.expand_dims(train_images[0], axis=0))
channel_to_visualize = 0 # Adjust this value based on the number of channels in your output
channel_image = output_tf[0, :, :, channel_to_visualize]
[Link](channel_image, cmap='gray') # Assuming a grayscale channel
[Link](f'Filtered and Pooled Channel {channel_to_visualize} (TensorFlow)')
[Link]()
1/1 [==============================] - 0s 80ms/step

ConvNet Architectures: Implement a famous convNet architectures - AlexNet, ZFNet,


keyboard_arrow_down VGG, C3D, GoogLeNet, ResNet, MobileNet-v1.
implementation of AlexNet using TensorFlow.
import tensorflow as tf
from [Link] import layers, models

def alexnet_model(input_shape=(224, 224, 3), num_classes=1000):


model = [Link]()

# Layer 1
[Link](layers.Conv2D(96, (11, 11), strides=(4, 4), activation='relu', input_shape=input_shape))
[Link](layers.MaxPooling2D((3, 3), strides=(2, 2)))

# Layer 2
[Link](layers.Conv2D(256, (5, 5), padding='same', activation='relu'))
[Link](layers.MaxPooling2D((3, 3), strides=(2, 2)))

# Layer 3
[Link](layers.Conv2D(384, (3, 3), padding='same', activation='relu'))

# Layer 4
[Link](layers.Conv2D(384, (3, 3), padding='same', activation='relu'))

# Layer 5
[Link](layers.Conv2D(256, (3, 3), padding='same', activation='relu'))
[Link](layers.MaxPooling2D((3, 3), strides=(2, 2)))

# Flatten
[Link]([Link]())

# Fully Connected layers


[Link]([Link](4096, activation='relu'))
[Link]([Link](0.5))
[Link]([Link](4096, activation='relu'))
[Link]([Link](0.5))
[Link]([Link](num_classes, activation='softmax'))

return model

# Create AlexNet model


alexnet = alexnet_model()

# Display model summary


[Link]()

Model: "sequential_2"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_2 (Conv2D) (None, 54, 54, 96) 34944

max_pooling2d_2 (MaxPoolin (None, 26, 26, 96) 0


g2D)

conv2d_3 (Conv2D) (None, 26, 26, 256) 614656

max_pooling2d_3 (MaxPoolin (None, 12, 12, 256) 0


g2D)

conv2d_4 (Conv2D) (None, 12, 12, 384) 885120

conv2d_5 (Conv2D) (None, 12, 12, 384) 1327488

conv2d_6 (Conv2D) (None, 12, 12, 256) 884992

max_pooling2d_4 (MaxPoolin (None, 5, 5, 256) 0


g2D)

flatten (Flatten) (None, 6400) 0

dense (Dense) (None, 4096) 26218496

dropout (Dropout) (None, 4096) 0

dense_1 (Dense) (None, 4096) 16781312

dropout_1 (Dropout) (None, 4096) 0

dense_2 (Dense) (None, 1000) 4097000

=================================================================
Total params: 50844008 (193.95 MB)
Trainable params: 50844008 (193.95 MB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________

implementation of ZFNet using TensorFlow:


import tensorflow as tf
from [Link] import layers, models

def zfnet_model(input_shape=(224, 224, 3), num_classes=1000):


model = [Link]()

# Layer 1
[Link](layers.Conv2D(96, (7, 7), strides=(2, 2), activation='relu', input_shape=input_shape))
[Link](layers.MaxPooling2D((3, 3), strides=(2, 2)))

# Layer 2
[Link](layers.Conv2D(256, (5, 5), padding='same', activation='relu'))
[Link](layers.MaxPooling2D((3, 3), strides=(2, 2)))

# Layer 3
[Link](layers.Conv2D(384, (3, 3), padding='same', activation='relu'))
[Link](layers.Conv2D(384, (3, 3), padding='same', activation='relu'))
[Link](layers.Conv2D(256, (3, 3), padding='same', activation='relu'))
[Link](layers.MaxPooling2D((3, 3), strides=(2, 2)))

# Flatten
[Link]([Link]())

# Fully Connected layers


[Link]([Link](4096, activation='relu'))
[Link]([Link](0.5))
[Link]([Link](4096, activation='relu'))
[Link]([Link](0.5))
[Link]([Link](num_classes, activation='softmax'))

return model

# Create ZFNet model


zfnet = zfnet_model()

# Display model summary


[Link]()

Model: "sequential_3"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_7 (Conv2D) (None, 109, 109, 96) 14208

max_pooling2d_5 (MaxPoolin (None, 54, 54, 96) 0


g2D)

conv2d_8 (Conv2D) (None, 54, 54, 256) 614656

max_pooling2d_6 (MaxPoolin (None, 26, 26, 256) 0


g2D)

conv2d_9 (Conv2D) (None, 26, 26, 384) 885120

conv2d_10 (Conv2D) (None, 26, 26, 384) 1327488

conv2d_11 (Conv2D) (None, 26, 26, 256) 884992

max_pooling2d_7 (MaxPoolin (None, 12, 12, 256) 0


g2D)

flatten_1 (Flatten) (None, 36864) 0

dense_3 (Dense) (None, 4096) 150999040

dropout_2 (Dropout) (None, 4096) 0

dense_4 (Dense) (None, 4096) 16781312

dropout_3 (Dropout) (None, 4096) 0

dense_5 (Dense) (None, 1000) 4097000

=================================================================
Total params: 175603816 (669.88 MB)
Trainable params: 175603816 (669.88 MB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________

implementation of the VGG16 architecture using TensorFlow:


import tensorflow as tf
from [Link] import layers, models

def vgg16_model(input_shape=(224, 224, 3), num_classes=1000):


model = [Link]()

# Block 1
[Link](layers.Conv2D(64, (3, 3), activation='relu', padding='same', input_shape=input_shape))
[Link](layers.Conv2D(64, (3, 3), activation='relu', padding='same'))
[Link](layers.MaxPooling2D((2, 2), strides=(2, 2)))

# Block 2
[Link](layers.Conv2D(128, (3, 3), activation='relu', padding='same'))
[Link](layers.Conv2D(128, (3, 3), activation='relu', padding='same'))
[Link](layers.MaxPooling2D((2, 2), strides=(2, 2)))

# Block 3
[Link](layers.Conv2D(256, (3, 3), activation='relu', padding='same'))
[Link](layers.Conv2D(256, (3, 3), activation='relu', padding='same'))
[Link](layers.Conv2D(256, (3, 3), activation='relu', padding='same'))
[Link](layers.MaxPooling2D((2, 2), strides=(2, 2)))

# Block 4
[Link](layers.Conv2D(512, (3, 3), activation='relu', padding='same'))
[Link](layers.Conv2D(512, (3, 3), activation='relu', padding='same'))
[Link](layers.Conv2D(512, (3, 3), activation='relu', padding='same'))
[Link](layers.MaxPooling2D((2, 2), strides=(2, 2)))

# Block 5
[Link](layers.Conv2D(512, (3, 3), activation='relu', padding='same'))
[Link](layers.Conv2D(512, (3, 3), activation='relu', padding='same'))
[Link](layers.Conv2D(512, (3, 3), activation='relu', padding='same'))
[Link](layers.MaxPooling2D((2, 2), strides=(2, 2)))

# Flatten
[Link]([Link]())

# Fully Connected layers


[Link]([Link](4096, activation='relu'))
[Link]([Link](0.5))
[Link]([Link](4096, activation='relu'))
[Link]([Link](0.5))
[Link]([Link](num_classes, activation='softmax'))

return model

# Create VGG16 model


vgg16 = vgg16_model()

# Display model summary


[Link]()

Model: "sequential_4"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_12 (Conv2D) (None, 224, 224, 64) 1792

conv2d_13 (Conv2D) (None, 224, 224, 64) 36928

max_pooling2d_8 (MaxPoolin (None, 112, 112, 64) 0


g2D)

conv2d_14 (Conv2D) (None, 112, 112, 128) 73856

conv2d_15 (Conv2D) (None, 112, 112, 128) 147584

max_pooling2d_9 (MaxPoolin (None, 56, 56, 128) 0


g2D)

conv2d_16 (Conv2D) (None, 56, 56, 256) 295168

conv2d_17 (Conv2D) (None, 56, 56, 256) 590080

conv2d_18 (Conv2D) (None, 56, 56, 256) 590080

max_pooling2d_10 (MaxPooli (None, 28, 28, 256) 0


ng2D)

conv2d_19 (Conv2D) (None, 28, 28, 512) 1180160

conv2d_20 (Conv2D) (None, 28, 28, 512) 2359808

conv2d_21 (Conv2D) (None, 28, 28, 512) 2359808


max_pooling2d_11 (MaxPooli (None, 14, 14, 512) 0
ng2D)

conv2d_22 (Conv2D) (None, 14, 14, 512) 2359808

conv2d_23 (Conv2D) (None, 14, 14, 512) 2359808

conv2d_24 (Conv2D) (None, 14, 14, 512) 2359808

max_pooling2d_12 (MaxPooli (None, 7, 7, 512) 0


ng2D)

flatten_2 (Flatten) (None, 25088) 0

dense_6 (Dense) (None, 4096) 102764544

dropout_4 (Dropout) (None, 4096) 0

dense_7 (Dense) (None, 4096) 16781312

dropout_5 (Dropout) (None, 4096) 0

dense_8 (Dense) (None, 1000) 4097000

=================================================================

implementation of GoogLeNet using TensorFlow

import tensorflow as tf
from [Link] import layers, models

def inception_module(x, filters):


conv1x1_1 = layers.Conv2D(filters[0], (1, 1), padding='same', activation='relu')(x)

conv1x1_3 = layers.Conv2D(filters[1], (1, 1), padding='same', activation='relu')(x)


conv3x3 = layers.Conv2D(filters[2], (3, 3), padding='same', activation='relu')(conv1x1_3)

conv1x1_5 = layers.Conv2D(filters[3], (1, 1), padding='same', activation='relu')(x)


conv5x5 = layers.Conv2D(filters[4], (5, 5), padding='same', activation='relu')(conv1x1_5)

maxpool = layers.MaxPooling2D((3, 3), strides=(1, 1), padding='same')(x)


conv1x1_maxpool = layers.Conv2D(filters[5], (1, 1), padding='same', activation='relu')(maxpool)

inception = [Link](axis=-1)([conv1x1_1, conv3x3, conv5x5, conv1x1_maxpool])


return inception

def googlenet_model(input_shape=(224, 224, 3), num_classes=1000):


inputs = [Link](shape=input_shape)

# Initial convolutions
x = layers.Conv2D(64, (7, 7), strides=(2, 2), padding='same', activation='relu')(inputs)
x = layers.MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)
x = [Link]()(x)

# Inception modules
x = inception_module(x, [64, 128, 128, 32, 32, 32])
x = inception_module(x, [128, 192, 96, 64, 64, 64])
x = layers.MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)

x = inception_module(x, [192, 208, 96, 64, 64, 64])


x = inception_module(x, [160, 224, 112, 64, 64, 64])
x = inception_module(x, [128, 256, 128, 64, 64, 64])
x = inception_module(x, [112, 288, 144, 64, 64, 64])
x = inception_module(x, [256, 320, 160, 128, 128, 128])
x = layers.MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)

# Fully connected layers


x = layers.GlobalAveragePooling2D()(x)
x = [Link](1024, activation='relu')(x)
x = [Link](0.4)(x)
x = [Link](num_classes, activation='softmax')(x)

model = [Link](inputs, x)
return model

# Create GoogLeNet model


googlenet = googlenet_model()

# Display model summary


[Link]()
Model: "model"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_1 (InputLayer) [(None, 224, 224, 3)] 0 []

conv2d_25 (Conv2D) (None, 112, 112, 64) 9472 ['input_1[0][0]']

max_pooling2d_13 (MaxPooli (None, 56, 56, 64) 0 ['conv2d_25[0][0]']


ng2D)

batch_normalization (Batch (None, 56, 56, 64) 256 ['max_pooling2d_13[0][0]']


Normalization)

conv2d_27 (Conv2D) (None, 56, 56, 128) 8320 ['batch_normalization[0][0]']

conv2d_29 (Conv2D) (None, 56, 56, 32) 2080 ['batch_normalization[0][0]']

max_pooling2d_14 (MaxPooli (None, 56, 56, 64) 0 ['batch_normalization[0][0]']


ng2D)

conv2d_26 (Conv2D) (None, 56, 56, 64) 4160 ['batch_normalization[0][0]']

conv2d_28 (Conv2D) (None, 56, 56, 128) 147584 ['conv2d_27[0][0]']

conv2d_30 (Conv2D) (None, 56, 56, 32) 25632 ['conv2d_29[0][0]']

conv2d_31 (Conv2D) (None, 56, 56, 32) 2080 ['max_pooling2d_14[0][0]']

concatenate (Concatenate) (None, 56, 56, 256) 0 ['conv2d_26[0][0]',


'conv2d_28[0][0]',
'conv2d_30[0][0]',
'conv2d_31[0][0]']

conv2d_33 (Conv2D) (None, 56, 56, 192) 49344 ['concatenate[0][0]']

conv2d_35 (Conv2D) (None, 56, 56, 64) 16448 ['concatenate[0][0]']

max_pooling2d_15 (MaxPooli (None, 56, 56, 256) 0 ['concatenate[0][0]']


ng2D)

conv2d_32 (Conv2D) (None, 56, 56, 128) 32896 ['concatenate[0][0]']

conv2d_34 (Conv2D) (None, 56, 56, 96) 165984 ['conv2d_33[0][0]']

conv2d_36 (Conv2D) (None, 56, 56, 64) 102464 ['conv2d_35[0][0]']

conv2d_37 (Conv2D) (None, 56, 56, 64) 16448 ['max_pooling2d_15[0][0]']

concatenate_1 (Concatenate (None, 56, 56, 352) 0 ['conv2d_32[0][0]',


) 'conv2d_34[0][0]',
'conv2d_36[0][0]',
'conv2d_37[0][0]']

max_pooling2d_16 (MaxPooli (None, 28, 28, 352) 0 ['concatenate_1[0][0]']


ng2D)

conv2d_39 (Conv2D) (None, 28, 28, 208) 73424 ['max_pooling2d_16[0][0]']

keyboard_arrow_down Convolution Neural Network application using TensorFlow and Keras,


i) Classification of MNIST Dataset using CNN
import tensorflow as tf
from [Link] import layers, models
from [Link] import mnist
from [Link] import to_categorical

# Load and preprocess the MNIST dataset


(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
train_images = train_images.reshape((60000, 28, 28, 1)).astype('float32') / 255
test_images = test_images.reshape((10000, 28, 28, 1)).astype('float32') / 255

train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)

# Define the CNN model


model = [Link]()
[Link](layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
[Link](layers.MaxPooling2D((2, 2)))
[Link](layers.Conv2D(64, (3, 3), activation='relu'))
[Link](layers.MaxPooling2D((2, 2)))
[Link](layers.Conv2D(64, (3, 3), activation='relu'))
[Link]([Link]())
[Link]([Link](64, activation='relu'))
[Link]([Link](10, activation='softmax'))

# Compile the model


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

# Train the model


[Link](train_images, train_labels, epochs=5, batch_size=64, validation_split=0.2)

# Evaluate the model on the test set


test_loss, test_acc = [Link](test_images, test_labels)
print(f'Test accuracy: {test_acc}')

# Make predictions
predictions = [Link](test_images[:5])
print('Predictions:')
print([Link](axis=1))
print('Actual Labels:')
print(test_labels[:5].argmax(axis=1))

Downloading data from [Link]


11490434/11490434 [==============================] - 0s 0us/step
Epoch 1/5
750/750 [==============================] - 46s 60ms/step - loss: 0.2270 - accuracy: 0.9290 - val_loss: 0.0625 - val_accuracy: 0.9808
Epoch 2/5
750/750 [==============================] - 40s 54ms/step - loss: 0.0566 - accuracy: 0.9823 - val_loss: 0.0447 - val_accuracy: 0.9868
Epoch 3/5
750/750 [==============================] - 40s 53ms/step - loss: 0.0401 - accuracy: 0.9873 - val_loss: 0.0401 - val_accuracy: 0.9882
Epoch 4/5
750/750 [==============================] - 40s 53ms/step - loss: 0.0312 - accuracy: 0.9904 - val_loss: 0.0512 - val_accuracy: 0.9858
Epoch 5/5
750/750 [==============================] - 44s 58ms/step - loss: 0.0261 - accuracy: 0.9915 - val_loss: 0.0436 - val_accuracy: 0.9869
313/313 [==============================] - 4s 12ms/step - loss: 0.0322 - accuracy: 0.9895
Test accuracy: 0.9894999861717224
1/1 [==============================] - 0s 82ms/step
Predictions:
[7 2 1 0 4]
Actual Labels:
[7 2 1 0 4]

keyboard_arrow_down ii) Face recognition using CNN


filename = "../input/opencv-facial-recognition-lbph/yalefaces/test/[Link]"
pixels = [Link](filename)

rgb_pixels = [Link]((pixels, pixels, pixels), axis=2)


print(rgb_pixels.shape)
[Link](pixels)
[Link]()
(243, 320, 3)

keyboard_arrow_down 2) MTCNN
detector = MTCNN()

results = detector.detect_faces(rgb_pixels)
results

KMP_FOREIGN_THREADS_THREADPRIVATE=true
KMP_FORKJOIN_BARRIER='2,2'
KMP_FORKJOIN_BARRIER_PATTERN='hyper,hyper'
KMP_GTID_MODE=3
KMP_HANDLE_SIGNALS=false
KMP_HOT_TEAMS_MAX_LEVEL=1
KMP_HOT_TEAMS_MODE=0
KMP_INIT_AT_FORK=true
KMP_LIBRARY=throughput
KMP_LOCK_KIND=queuing
KMP_MALLOC_POOL_INCR=1M
KMP_NUM_LOCKS_IN_BLOCK=1
KMP_PLAIN_BARRIER='2,2'
KMP_PLAIN_BARRIER_PATTERN='hyper,hyper'
KMP_REDUCTION_BARRIER='1,1'
KMP_REDUCTION_BARRIER_PATTERN='hyper,hyper'
KMP_SCHEDULE='static,balanced;guided,iterative'
KMP_SETTINGS=true
KMP_SPIN_BACKOFF_PARAMS='4096,100'
KMP_STACKOFFSET=64
KMP_STACKPAD=0
KMP_STACKSIZE=8M
KMP_STORAGE_MAP=false
KMP_TASKING=2
KMP_TASKLOOP_MIN_TASKS=0
KMP_TASK_STEALING_CONSTRAINT=1
KMP_TEAMS_THREAD_LIMIT=4
KMP_TOPOLOGY_METHOD=all
KMP_USE_YIELD=1
KMP_VERSION=false
KMP_WARNINGS=false
OMP_AFFINITY_FORMAT='OMP: pid %P tid %i thread %n bound to OS proc set {%A}'
OMP_ALLOCATOR=omp_default_mem_alloc
OMP_CANCELLATION=false
OMP_DEFAULT_DEVICE=0
OMP_DISPLAY_AFFINITY=false
OMP_DISPLAY_ENV=false
OMP_DYNAMIC=false
OMP_MAX_ACTIVE_LEVELS=1
OMP_MAX_TASK_PRIORITY=0
OMP_NESTED: deprecated; max-active-levels-var=1
OMP_NUM_THREADS: value is not defined
OMP_PLACES: value is not defined
OMP_PROC_BIND='intel'
OMP_SCHEDULE='static'
OMP_STACKSIZE=8M
OMP_TARGET_OFFLOAD=DEFAULT
OMP_THREAD_LIMIT=2147483647
OMP_WAIT_POLICY=PASSIVE
KMP_AFFINITY='verbose,warnings,respect,granularity=fine,compact,1,0'

[{'box': [115, 90, 110, 134],


'confidence': 0.9997938275337219,
'keypoints': {'left_eye': (147, 140),
'right_eye': (198, 138),
'nose': (174, 169),
'mouth_left': (151, 192),
'mouth_right': (195, 193)}}]
def draw_image_with_boxes(data, result_list):
[Link](data)
ax = [Link]()
for result in result_list:
x, y, width, height = result['box']
rect = Rectangle((x, y), width, height, fill=False, color='red')
ax.add_patch(rect)
[Link]()

draw_image_with_boxes(rgb_pixels, results)

keyboard_arrow_down 3) Extract and normalise the face pixels


def extract_face_from_file(filename, required_size=(160, 160)):
image = [Link](filename)

return extract_face(image, required_size)

def extract_face(image, required_size=(160, 160)):


image = [Link]('RGB')
pixels = [Link](image)
results = detector.detect_faces(pixels)

x1, y1, width, height = results[0]['box']

x1, y1 = abs(x1), abs(y1)


x2, y2 = x1 + width, y1 + height

face = pixels[y1:y2, x1:x2]

image = [Link](face)
image = [Link](required_size)
face_array = [Link](image)
gray_face = [Link](face_array, cv2.COLOR_BGR2GRAY)

return gray_face

detector = MTCNN()

face_pixels = extract_face_from_file("../input/opencv-facial-recognition-lbph/yalefaces/test/[Link]")

[Link](face_pixels)

<[Link] at 0x782d6d6b1610>

keyboard_arrow_down [Link] set 〜 135 train datas and 30 test datas


def list_files(directory, contains):
return list(f for f in listdir(directory) if contains in f)

i = 1
faces = list()
for filename in tqdm(list_files(DIRECTORY_train, "subject")[0:16]):
# path
path = DIRECTORY_train + filename
# get face
face = extract_face_from_file(path)
# plot
[Link](4, 4, i)
[Link]('off')
[Link](face)
[Link](face)
i += 1
[Link]()

100%|██████████| 16/16 [00:13<00:00, 1.14it/s]

filenames = [Link](list_files(DIRECTORY_train, "subject"))

df_train = filenames[0].[Link](".", expand=True)


df_train["filename"] = filenames

df_train = df_train.rename(columns = {0:"subject", 1:"category"})


df_train['subject'] = df_train.[Link]('subject' , '')
df_train.apply(pd.to_numeric, errors='coerce').dropna()
df_train['subject'] = pd.to_numeric(df_train["subject"])
df_train

subject category 2 filename

0 15 happy gif [Link]

1 13 noglasses gif [Link]

2 1 normal gif [Link]

3 15 surprised gif [Link]

4 14 wink gif [Link]

... ... ... ... ...

130 5 sad gif [Link]

131 3 surprised gif [Link]

132 4 sad gif [Link]

133 7 normal gif [Link]

134 2 surprised gif [Link]

135 rows × 4 columns

filenames2 = [Link](list_files(DIRECTORY_test, "subject"))

df_test = filenames2[0].[Link](".", expand=True)


df_test["filename"] = filenames2

df_test = df_test.rename(columns = {0:"subject", 1:"category"})


df_test['subject'] = df_test.[Link]('subject' , '')
df_test.apply(pd.to_numeric, errors='coerce').dropna()
df_test['subject'] = pd.to_numeric(df_test["subject"])
df_test
subject category 2 filename

0 3 glasses gif [Link]

1 12 normal gif [Link]

2 2 leftlight gif [Link]

3 13 sad gif [Link]

4 6 leftlight gif [Link]

5 11 glasses gif [Link]

6 2 centerlight gif [Link]

7 14 sad gif [Link]

8 14 normal gif [Link]

9 4 surprised gif [Link]

10 1 happy gif [Link]

11 9 rightlight gif [Link]

12 15 rightlight gif [Link]

13 9 sad gif [Link]

14 15 sad gif [Link]

15 7 happy gif [Link]

16 12 rightlight gif [Link]

17 5 surprised gif [Link]

18 4 leftlight gif [Link]

19 10 sad gif [Link]

20 6 happy gif [Link]

21 8 rightlight gif [Link]

22 13 sleepy gif [Link]

23 3 leftlight gif [Link]

24 5 sleepy gif [Link]

25 10 centerlight gif [Link]

26 8 normal gif [Link]

27 1 gif None [Link]

28 7 leftlight gif [Link]

29 11 happy gif [Link]

x_train=df_train.loc[:,['category','filename']]
x_test=df_test.loc[:,['category','filename']]
y_train=df_train.loc[:,['subject']]
y_test=df_test.loc[:,['subject']]

y_train=y_train.to_numpy()
y_test=y_test.to_numpy()

y_train = y_train.tolist()
y_test = y_test.tolist()

detector = MTCNN()

def load_dataset1(dataset):
faces = list()
for filename in tqdm(dataset["filename"]):
path = DIRECTORY_train + filename
# get face
face = extract_face_from_file(path)
[Link](face)
return [Link](faces)
detector = MTCNN()

def load_dataset2(dataset):
faces = list()
for filename in tqdm(dataset["filename"]):
path = DIRECTORY_test + filename
# get face
x_test =face
load_dataset2(x_test)
= extract_face_from_file(path)
x_train = load_dataset1(x_train)
[Link](face)
return [Link](faces)
print(x_test.shape)
print(x_train.shape)

100%|██████████| 30/30 [00:25<00:00, 1.19it/s]


91%|█████████ | 123/135 [01:40<00:09, 1.25it/s]

keyboard_arrow_down 3. Convolutional Neural Network Model


TRAINING_DATA_DIRECTORY = "data/train"
TESTING_DATA_DIRECTORY = "data/test"
NUM_CLASSES = 15
EPOCHS = 25
BATCH_SIZE = 20
NUMBER_OF_TRAINING_IMAGES = 135
NUMBER_OF_TESTING_IMAGES = 30
IMAGE_HEIGHT = 160
IMAGE_WIDTH = 160

import os

def save_keras_dataset(setname, dataset, labels, per_class):


data = sorted(list(zip(labels, dataset)), key=lambda x: x[0])

j = 0
for label, gray_img in tqdm(data):
j = (j% per_class) + 1

directory = f"data/{setname}/class_{label}/"
if not [Link](directory):
[Link](directory)
[Link](f"{directory}class_{label}_{j}.png",gray_img)

import shutil
[Link](r'data', ignore_errors=True)

# Save datasets
save_keras_dataset("test", x_test, y_test, 3)
save_keras_dataset("train", x_train, y_train, 8)

from [Link] import ImageDataGenerator


def data_generator():
return ImageDataGenerator(
keyboard_arrow_down 8. Text processing, Language Modeling using RNN
import numpy as np
import tensorflow as tf
from [Link] import Tokenizer
from [Link] import pad_sequences
from [Link] import Sequential
from [Link] import Embedding, SimpleRNN, Dense

# Sample text data


text_data = [
"The quick brown fox jumps over the lazy dog",
"A quick brown dog jumps over the lazy fox",
"The lazy fox jumps over the quick brown dog"
]

# Define parameters for text processing


num_words = 50 # Number of most frequent words to keep
max_len = 10 # Maximum sequence length

# Tokenize the text data


tokenizer = Tokenizer(num_words=num_words)
tokenizer.fit_on_texts(text_data)
sequences = tokenizer.texts_to_sequences(text_data)

# Pad sequences to ensure uniform length


padded_sequences = pad_sequences(sequences, maxlen=max_len, padding='pre')

# Prepare input-output pairs for language modeling


X = padded_sequences[:, :-1] # Input sequence (remove last token)
y = padded_sequences[:, 1:] # Output sequence (shifted by one)

# Define the RNN model


model = Sequential([
Embedding(input_dim=num_words, output_dim=10, input_length=max_len-1),
SimpleRNN(units=32, return_sequences=True),
Dense(num_words, activation='softmax')
])

# Compile the model


[Link](optimizer='adam', loss='sparse_categorical_crossentropy')

# Train the model


[Link](X, y, epochs=50, verbose=1)

# Generate text using the trained model


# Generate text using the trained model
def generate_text(seed_text, max_length=10):
for _ in range(max_length):
# Tokenize the seed text
tokenized_text = tokenizer.texts_to_sequences([seed_text])[0]
# Pad the sequence
padded_sequence = pad_sequences([tokenized_text], maxlen=max_len-1, padding='pre')
# Predict the next word
predicted_index = [Link]([Link](padded_sequence), axis=-1)[0][0]
# Convert the index to word
predicted_word = tokenizer.index_word.get(predicted_index, '')
if predicted_word:
seed_text += ' ' + predicted_word
else:
break
return seed_text

# Generate text starting from a seed


seed_text = "The quick brown"
generated_text = generate_text(seed_text)
print("Generated Text:", generated_text)
Epoch 32/50
1/1 [==============================] - 0s 14ms/step - loss: 3.1028
Epoch 33/50
1/1 [==============================] - 0s 14ms/step - loss: 3.0771
Epoch 34/50
1/1 [==============================] - 0s 14ms/step - loss: 3.0519
Epoch 35/50
1/1 [==============================] - 0s 14ms/step - loss: 3.0271
Epoch 36/50
1/1 [==============================] - 0s 12ms/step - loss: 3.0026
Epoch 37/50
1/1 [==============================] - 0s 12ms/step - loss: 2.9783
Epoch 38/50
1/1 [==============================] - 0s 12ms/step - loss: 2.9541
Epoch 39/50
1/1 [==============================] - 0s 13ms/step - loss: 2.9301
Epoch 40/50
1/1 [==============================] - 0s 16ms/step - loss: 2.9061
Epoch 41/50
1/1 [==============================] - 0s 11ms/step - loss: 2.8822
Epoch 42/50
1/1 [==============================] - 0s 13ms/step - loss: 2.8583
Epoch 43/50
1/1 [==============================] - 0s 15ms/step - loss: 2.8346
Epoch 44/50
1/1 [==============================] - 0s 12ms/step - loss: 2.8111
Epoch 45/50
1/1 [==============================] - 0s 13ms/step - loss: 2.7878
Epoch 46/50
1/1 [==============================] - 0s 13ms/step - loss: 2.7647
Epoch 47/50
1/1 [==============================] - 0s 14ms/step - loss: 2.7419
Epoch 48/50
1/1 [==============================] - 0s 13ms/step - loss: 2.7196
Epoch 49/50
1/1 [==============================] - 0s 12ms/step - loss: 2.6977
Epoch 50/50
1/1 [==============================] - 0s 14ms/step - loss: 2.6763
1/1 [==============================] - 0s 192ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 26ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 25ms/step
Generated Text: The quick brown the the the the the the fox brown fox fox
keyboard_arrow_down 9. Time Series Prediction using RNN
import numpy as np
import tensorflow as tf
from tensorflow import keras
from [Link] import Sequential
from [Link] import SimpleRNN, Dense
import [Link] as plt

# Function to generate time series data


def generate_time_series(n_steps):
freq1, freq2, offsets1, offsets2 = [Link](4, 1)
time = [Link](0, 1, n_steps)
series = 0.5 * [Link]((time - offsets1) * (freq1 * 10 + 10)) # First sine wave
series += 0.2 * [Link]((time - offsets2) * (freq2 * 20 + 20)) # Second sine wave
series += 0.1 * ([Link](n_steps) - 0.5) # Noise
return series[..., [Link]].astype(np.float32)

# Generate training and validation data


n_steps = 50
series = generate_time_series(10000)
X_train, y_train = series[:7000, :n_steps], series[:7000, -1]
X_valid, y_valid = series[7000:9000, :n_steps], series[7000:9000, -1]
X_test, y_test = series[9000:, :n_steps], series[9000:, -1]
# Plot the time series data
[Link](series)
[Link]('Time Series Data')
[Link]('Time')
[Link]('Value')
[Link]()
# Define the RNN model
model = Sequential([
SimpleRNN(20, input_shape=[None, 1]),
Dense(1)
])

# Compile the model


[Link](optimizer='adam', loss='mse')

# Train the model


history = [Link](X_train, y_train, epochs=20, validation_data=(X_valid, y_valid))

# Evaluate the model


mse_test = [Link](X_test, y_test)
print("Test MSE:", mse_test)

# Plot training and validation loss


[Link]([Link]['loss'], label='Training Loss')
[Link]([Link]['val_loss'], label='Validation Loss')
[Link]('Training and Validation Loss')
[Link]('Epoch')
[Link]('Loss')
[Link]()
[Link]()
output

Epoch 1/20
219/219 [==============================] - 3s 6ms/step - loss: 0.1242 - val_loss: 0.0010
Epoch 2/20
219/219 [==============================] - 1s 6ms/step - loss: 2.5556e-04 - val_loss: 1.0640e-05
Epoch 3/20
219/219 [==============================] - 1s 4ms/step - loss: 1.4771e-05 - val_loss: 1.0516e-05
Epoch 4/20
219/219 [==============================] - 1s 3ms/step - loss: 1.4698e-05 - val_loss: 1.0569e-05
Epoch 5/20
219/219 [==============================] - 1s 3ms/step - loss: 1.4588e-05 - val_loss: 1.1668e-05
Epoch 6/20
219/219 [==============================] - 1s 3ms/step - loss: 1.4546e-05 - val_loss: 1.0685e-05
Epoch 7/20
219/219 [==============================] - 1s 3ms/step - loss: 1.4416e-05 - val_loss: 1.0099e-05
Epoch 8/20
219/219 [==============================] - 1s 3ms/step - loss: 1.4256e-05 - val_loss: 1.0089e-05
Epoch 9/20
219/219 [==============================] - 1s 3ms/step - loss: 1.4095e-05 - val_loss: 1.0385e-05
Epoch 10/20
219/219 [==============================] - 1s 3ms/step - loss: 1.4024e-05 - val_loss: 1.0446e-05
Epoch 11/20
219/219 [==============================] - 1s 3ms/step - loss: 1.3683e-05 - val_loss: 9.8510e-06
Epoch 12/20
219/219 [==============================] - 1s 3ms/step - loss: 1.3568e-05 - val_loss: 9.1702e-06
Epoch 13/20
219/219 [==============================] - 1s 3ms/step - loss: 1.3355e-05 - val_loss: 9.5895e-06
Epoch 14/20
219/219 [==============================] - 1s 3ms/step - loss: 1.3283e-05 - val_loss: 9.6184e-06
Epoch 15/20
219/219 [==============================] - 1s 3ms/step - loss: 1.3050e-05 - val_loss: 1.0971e-05
Epoch 16/20
219/219 [==============================] - 1s 3ms/step - loss: 1.2653e-05 - val_loss: 9.2719e-06
Epoch 17/20
219/219 [==============================] - 1s 3ms/step - loss: 1.2574e-05 - val_loss: 8.5916e-06
Epoch 18/20
219/219 [==============================] - 1s 4ms/step - loss: 1.2296e-05 - val_loss: 1.0373e-05
Epoch 19/20
219/219 [==============================] - 1s 5ms/step - loss: 1.2138e-05 - val_loss: 8.4920e-06
Epoch 20/20
219/219 [==============================] - 1s 5ms/step - loss: 1.1929e-05 - val_loss: 8.3111e-06
32/32 [==============================] - 0s 2ms/step - loss: 1.1224e-05
Test MSE: 1.1224380614294205e-05
keyboard_arrow_down 10. Sentiment Analysis using LSTM
import pandas as pd
from sklearn.model_selection import train_test_split
from [Link] import LabelEncoder
from [Link] import imdb
from [Link] import Sequential
from [Link] import Embedding, LSTM, Dense, Dropout
from [Link] import pad_sequences
from [Link] import Tokenizer

# Load the dataset


file_path = '/content/drive/MyDrive/IMDB [Link]'
df = pd.read_csv(file_path, nrows=30000)

# Define parameters for preprocessing


num_words = 10000 # Number of most frequent words to keep
max_len = 100 # Maximum sequence length

# Perform train-test split


X_train_text, X_test_text, y_train, y_test = train_test_split(df['review'], df['sentiment'], test_size=0.2, random_state=42)

# Preprocess the text data


tokenizer = Tokenizer(num_words=num_words)
tokenizer.fit_on_texts(X_train_text)
X_train = tokenizer.texts_to_sequences(X_train_text)
X_test = tokenizer.texts_to_sequences(X_test_text)
X_train = pad_sequences(X_train, maxlen=max_len)
X_test = pad_sequences(X_test, maxlen=max_len)

# Encode the target labels


label_encoder = LabelEncoder()
y_train = label_encoder.fit_transform(y_train)
y_test = label_encoder.transform(y_test)

# Define the LSTM model


model = Sequential([
Embedding(input_dim=num_words, output_dim=128, input_length=max_len),
LSTM(units=64, dropout=0.2, recurrent_dropout=0.2),
Dense(units=1, activation='sigmoid')
])

# Compile the model


[Link](optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

# Train the model


[Link](X_train, y_train, batch_size=64, epochs=3, validation_data=(X_test, y_test))

# Evaluate the model on test data


loss, accuracy = [Link](X_test, y_test)
print(f'Test Loss: {loss:.4f}, Test Accuracy: {accuracy:.4f}')

output Epoch 1/3


375/375 [==============================] - 75s 195ms/step - loss: 0.4085 - accuracy: 0.8100 - val_loss: 0.3385 - val_accuracy: 0.85
Epoch 2/3
375/375 [==============================] - 71s 190ms/step - loss: 0.2624 - accuracy: 0.8944 - val_loss: 0.3361 - val_accuracy: 0.85
Epoch 3/3
375/375 [==============================] - 69s 184ms/step - loss: 0.1891 - accuracy: 0.9295 - val_loss: 0.4028 - val_accuracy: 0.85
188/188 [==============================] - 4s 20ms/step - loss: 0.4028 - accuracy: 0.8500
Test Loss: 0.4028, Test Accuracy: 0.8500

keyboard_arrow_down Testing on custom data.


# Preprocess custom inputs
def preprocess_input(text):
# Tokenize the text
tokenized_text = tokenizer.texts_to_sequences([text])
# Pad sequences
padded_text = pad_sequences(tokenized_text, maxlen=max_len)
return padded_text

# Example custom input


custom_input = "This project is great, It feels nice to work on it."
# Preprocess the custom input
preprocessed_input = preprocess_input(custom_input)

# Predict sentiment
prediction = [Link](preprocessed_input)

# Convert prediction to sentiment label


sentiment_label = "Positive" if prediction[0][0] > 0.5 else "Negative"

print("Custom Input:", custom_input)


print("Predicted Sentiment:", sentiment_label)

1/1 [==============================] - 0s 311ms/step


Custom Input: This project is great, It feels nice to work on it.
Predicted Sentiment: Positive
keyboard_arrow_down 11. Image geretation using GAN
import numpy as np
import [Link] as plt
from [Link] import mnist
from [Link] import Sequential
from [Link] import Dense, Flatten, Reshape
from [Link] import LeakyReLU
from [Link] import Adam
# Load MNIST dataset
(X_train, _), (_, _) = mnist.load_data()

# Normalize and reshape data


X_train = (X_train.astype(np.float32) - 127.5) / 127.5
X_train = np.expand_dims(X_train, axis=-1)

# Define generator model


generator = Sequential()
[Link](Dense(256, input_shape=(100,), activation='relu'))
[Link](Dense(512, activation='relu'))
[Link](Dense(28*28*1, activation='tanh'))
[Link](Reshape((28, 28, 1)))

# Define discriminator model


discriminator = Sequential()
[Link](Flatten(input_shape=(28, 28, 1)))
[Link](Dense(512, activation='relu'))
[Link](Dense(256, activation='relu'))
[Link](Dense(1, activation='sigmoid'))

# Compile discriminator
[Link](loss='binary_crossentropy', optimizer=Adam(learning_rate=0.0002, beta_1=0.5), metrics=['accuracy'])

# Combine generator and discriminator into a GAN model


[Link] = False
gan = Sequential([generator, discriminator])
[Link](loss='binary_crossentropy', optimizer=Adam(learning_rate=0.0002, beta_1=0.5))

# Training loop
batch_size = 64
epochs = 30000
sample_interval = 200
half_batch = batch_size // 2

for epoch in range(epochs):


# Train discriminator
idx = [Link](0, X_train.shape[0], half_batch)
real_images = X_train[idx]

noise = [Link](0, 1, (half_batch, 100))


fake_images = [Link](noise)

d_loss_real = discriminator.train_on_batch(real_images, [Link]((half_batch, 1)))


d_loss_fake = discriminator.train_on_batch(fake_images, [Link]((half_batch, 1)))
d_loss = 0.5 * [Link](d_loss_real, d_loss_fake)

# Train generator
noise = [Link](0, 1, (batch_size, 100))
valid_y = [Link]([1] * batch_size)
g_loss = gan.train_on_batch(noise, valid_y)

# Print progress
if epoch % sample_interval == 0:
print(f"Epoch {epoch}, [D loss: {d_loss[0]}, acc.: {100 * d_loss[1]}%], [G loss: {g_loss}]")

# Save generated images


r, c = 5, 5
noise = [Link](0, 1, (r * c, 100))
gen_imgs = [Link](noise)
gen_imgs = 0.5 * gen_imgs + 0.5 # Rescale images 0 - 1
fig, axs = [Link](r, c)
cnt = 0
for i in range(r):
for j in range(c):
axs[i,j].imshow(gen_imgs[cnt, :, :, 0], cmap='gray')
axs[i,j].axis('off')
cnt += 1
[Link]()
output Downloading data from [Link]
11490434/11490434 [==============================] - 0s 0us/step
1/1 [==============================] - 1s 771ms/step
Epoch 0, [D loss: 0.6993211209774017, acc.: 45.3125%], [G loss: 0.7240753173828125]
1/1 [==============================] - 0s 55ms/step

1/1 [==============================] - 0s 16ms/step


1/1 [==============================] - 0s 14ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 18ms/step
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 14ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 14ms/step
1/1 [==============================] - 0s 14ms/step
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 14ms/step
1/1 [==============================] - 0s 14ms/step
1/1 [==============================] - 0s 14ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 17ms/step
1/1 [==============================] - 0s 14ms/step
1/1 [==============================] - 0s 14ms/step
1/1 [==============================] - 0s 18ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 14ms/step
1/1 [==============================] - 0s 14ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 18ms/step
1/1 [==============================] - 0s 14ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 17ms/step
1/1 [==============================] - 0s 18ms/step
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 16ms/step
1/1 [==============================] - 0s 15ms/step
1/1 [==============================] - 0s 28ms/step
1/1 [==============================] - 0s 32ms/step
1/1 [==============================] - 0s 26ms/step
1/1 [==============================] - 0s 27ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 27ms/step

Common questions

Powered by AI

Custom activation functions can provide benefits such as better model performance and convergence properties. They allow the model to learn more complex relationships and behaviors specific to the given data. For example, in the discussed sources, custom operations using the recursive chain rule for backpropagation are implemented, facilitating smoother gradient flow and potentially avoiding issues like vanishing or exploding gradients associated with traditional functions like ReLU or Sigmoid . Moreover, custom functions can be tailored to enhance the modeling of specific patterns, leading to faster learning and improved accuracy .

When applying the softmax activation function in output layers, several considerations are critical: it transforms the output layer logits into probabilities that sum to one, which is ideal for multi-class classification problems. The primary consideration is ensuring numerical stability; softmax can cause issues if logits are large, leading to computational overflow. Implementing a stable version of softmax, often using techniques like subtracting the max logit from all logits before exponentiation, is necessary. Additionally, softmax works best when classes are mutually exclusive. The design of loss functions, such as cross-entropy loss, must align with softmax outputs to produce meaningful gradients for optimization .

ReLU (Rectified Linear Unit) and Tanh (hyperbolic tangent) are popular activation functions used in neural networks. ReLU outputs zero for negative inputs and linearly increases for positive inputs, which can lead to faster convergence but also dead neurons (units that never activate). Tanh, on the other hand, outputs values between -1 and 1 and is symmetric around the origin, capturing more varied relationships but potentially suffering from the vanishing gradient problem, as it compresses its gradient to small values. The choice between these functions often depends on the specific requirements of the learning task and the architecture of the network .

A simple CNN model in both PyTorch and TensorFlow processes the CIFAR-10 dataset through convolutional layers followed by activation and pooling operations. Both implementations involve similar layer configurations with convolutional, ReLU, and max-pooling operations. However, PyTorch provides more flexibility in model definition and dynamic computation graph capabilities, which can ease debugging and allow for more customized layer operations. TensorFlow, on the other hand, is recognized for scalability and often better integration into large-scale deployments. The choice between these frameworks might depend on specific project requirements, like the need for rapid prototyping (PyTorch) or production deployment (TensorFlow).

Integrating visualizations of layer outputs can significantly enhance CNN model architecture by providing insights into how features are learned and processed at each stage of the network. Visualization can reveal which layers contribute most effectively to feature extraction and those where learning might be impaired due to saturation or dead neurons. By studying convolutions, max-pooling, and ReLU activations, researchers can design targeted modifications, such as adjusting kernel sizes or strides, to improve information retention or propagation. It also helps in understanding the transformation from input to output across layers, facilitating optimization of layer depth, width, and connectivity for improved accuracy and efficiency .

Dropout is a regularization technique used to reduce overfitting by randomly dropping units and their connections in a neural network during training. This prevents units from becoming co-dependent, thereby promoting the learning of robust features that generalize better to unseen data. However, the trade-off involves increased training time and computational cost due to the need for longer training to achieve convergence. Additionally, it can result in model underfitting if the dropout rate is too high, reducing the model's capacity to learn complex features. Thus, selecting an appropriate dropout rate is crucial for balancing model complexity and generalization .

The bias-variance tradeoff is a fundamental concept that influences the design and performance of neural networks. High bias, indicative of an overly simplistic model, can lead to underfitting, where the model fails to capture the underlying trend of the data. Conversely, high variance, indicative of a model that is too complex, can result in overfitting, where the model captures noise along with the data's actual trend. Achieving an optimal balance involves designing a model with enough capacity to generalize well to unseen data while avoiding capturing unnecessary noise. Techniques such as regularization (L1, L2, dropout), cross-validation, and selecting the appropriate complexity level (like the number of layers or units) are critical in managing this tradeoff .

Enhanced neural network models with additional layers, such as deeper architectures, can capture more complex representations of the input data. By adding layers, models can learn hierarchical features; initially capturing low-level features and gradually building up to more abstract patterns. For instance, the EnhancedNet model discussed in the sources adds more hidden layers, allowing it to learn and represent intricate structures within the data. This is beneficial in domains like image recognition, where local and global patterns need to be identified . Moreover, such enhancements help in greater model expressiveness and potentially better generalization when appropriately regularized .

Backpropagation is a key component of training neural networks, primarily used to compute gradients of the loss function with respect to its parameters efficiently. The gradients are then used to update the network's weights to minimize the loss. In custom operations, as discussed in the document, the recursive chain rule is applied to compute these gradients. For instance, a custom PyTorch function implements backpropagation with the Tanh activation by calculating the derivative of the activation during the backward pass. This allows for more control over the training dynamics and can optimize performance on specific tasks .

Visualizing intermediate results in CNNs helps in understanding how the network processes inputs at various stages. By inspecting activations after different layers, one can gain insights into what features the model is learning and whether they are meaningful for the task. This can be particularly useful for debugging; anomalies in activations may indicate issues like improper weight initialization or suboptimal layer configurations. For instance, visualizing pooled channels can show how the model condenses information, highlighting areas of potential loss or distortion of critical features. Such analyses can guide model adjustments to improve performance and robustness .

You might also like