0% found this document useful (0 votes)
59 views

Assignment 2

The document discusses the differences between deep and shallow neural networks. Deep neural networks have multiple hidden layers which allows them to learn more complex patterns from data compared to shallow networks with fewer layers. Deep networks may require more advanced training techniques but can better represent hierarchical features in data and may be less prone to overfitting.

Uploaded by

Manish Raj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Assignment 2

The document discusses the differences between deep and shallow neural networks. Deep neural networks have multiple hidden layers which allows them to learn more complex patterns from data compared to shallow networks with fewer layers. Deep networks may require more advanced training techniques but can better represent hierarchical features in data and may be less prone to overfitting.

Uploaded by

Manish Raj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Q.1 Differentiate between Deep and Shallow Neural Network.

Ans:- Deep neural networks and shallow neural networks differ primarily in their
architecture and complexity. Here's a breakdown of their differences:

1. Depth:
 Shallow Neural Network: Also known as a single-layer neural network,
it typically consists of an input layer, one hidden layer, and an output
layer. The number of layers is limited, usually to one or two.
 Deep Neural Network: These networks have multiple hidden layers
between the input and output layers. They are characterized by having
a "deep" architecture with a large number of layers, often ranging from
several to hundreds or even thousands in some cases.
2. Representation Power:
 Shallow Neural Network: Due to their limited number of layers,
shallow networks are less capable of learning complex representations
from raw data. They might struggle with tasks that require capturing
intricate patterns or relationships in the data.
 Deep Neural Network: The deeper architecture allows deep neural
networks to learn hierarchical representations of data. This enables
them to handle more complex tasks, such as image and speech
recognition, natural language processing, and playing strategy games
at a high level.
3. Feature Hierarchies:
 Shallow Neural Network: With fewer layers, shallow networks can only
learn simple features directly from the input data. They might require
manual feature engineering to perform well on certain tasks.
 Deep Neural Network: Deep networks can automatically learn
hierarchical representations of features at different levels of abstraction.
Lower layers might learn simple features like edges or corners, while
higher layers combine these features to learn more complex patterns
relevant to the task.
4. Training Complexity:
 Shallow Neural Network: Training shallow networks is generally
simpler and faster compared to deep networks because of their smaller
size and simpler architecture.
 Deep Neural Network: Deep networks often require more
sophisticated training techniques, such as layer-wise pre-training, batch
normalization, and specialized optimization algorithms like Adam or
RMSProp. Training deep networks can be computationally intensive
and may require significant amounts of data and computational
resources.
5. Overfitting:
 Shallow Neural Network: Shallow networks are more prone to
overfitting, especially when dealing with complex datasets, due to their
limited capacity to learn abstract representations.
 Deep Neural Network: Deep networks can potentially mitigate
overfitting through techniques like dropout regularization, batch
normalization, and early stopping. However, they might still suffer from
overfitting if not properly regularized or if the training data is
insufficient.
Q.2What is deep learning , Explain its uses and application and history.

Ans:-
Deep learning is a subset of machine learning that involves artificial neural networks
with multiple layers (hence the term "deep"). These networks are designed to
automatically learn hierarchical representations of data by progressively extracting
higher-level features from raw input. Deep learning has gained significant attention
and popularity due to its remarkable performance in various domains, including
computer vision, natural language processing, speech recognition, and reinforcement
learning.

History:

 1940s - 1950s: The foundational concepts of neural networks were laid down
during this period, with the introduction of McCulloch-Pitts neurons and the
perceptron model.
 1960s - 1980s: Interest in neural networks grew, leading to the development
of more sophisticated models like the backpropagation algorithm. However,
the limitations of computing power and available data hindered progress.
 1990s - 2000s: Deep learning experienced a period of stagnation, often
referred to as the "AI winter," due to challenges in training deep neural
networks and the prevalence of other machine learning approaches.
 2010s: Deep learning experienced a resurgence, fueled by advancements in
computational power, the availability of large-scale datasets, and novel
techniques for training deep neural networks, such as convolutional neural
networks (CNNs) and recurrent neural networks (RNNs).

Uses and Applications:

1. Computer Vision:
 Object detection and recognition
 Image classification
 Image segmentation
 Facial recognition
 Autonomous vehicles
2. Natural Language Processing (NLP):
 Sentiment analysis
 Machine translation
 Named entity recognition
 Text generation
 Question answering
3. Speech Recognition:
 Speech-to-text conversion
 Voice assistants
 Speaker identification
4. Healthcare:
 Medical image analysis (e.g., X-ray interpretation, MRI analysis)
 Disease diagnosis and prognosis
 Drug discovery and development
 Personalized medicine
5. Finance:
 Fraud detection
 Algorithmic trading
 Credit scoring
 Risk assessment
6. Robotics:
 Object manipulation
 Autonomous navigation
 Human-robot interaction
7. Gaming:
 Game playing agents (e.g., AlphaGo)
 Character animation
 Procedural content generation
8. Recommendation Systems:
 Product recommendations
 Content recommendations (e.g., movies, music)
 Personalized marketing
9. Climate Science:
 Weather forecasting
 Climate modeling
 Environmental monitoring
Q.3 Create a simulation of the forward pass in a simple neural network with three layers,
demonstrating how input data is transformed to produce an output.

Ans:- Sure, let's create a simple simulation of the forward pass in a neural network
with three layers: an input layer, a hidden layer, and an output layer. We'll assume a
feedforward neural network where each neuron in a layer is connected to every
neuron in the subsequent layer. For simplicity, we'll use a random set of weights and
biases.

import numpy as np

# Define the neural network architecture

input_size = 4

hidden_size = 3

output_size = 2

# Randomly initialize weights and biases for each layer

np.random.seed(42) # For reproducibility

weights_input_hidden = np.random.randn(input_size, hidden_size)

biases_hidden = np.random.randn(hidden_size)

weights_hidden_output = np.random.randn(hidden_size, output_size)

biases_output = np.random.randn(output_size)

# Define the input data

input_data = np.array([0.1, 0.2, 0.3, 0.4])

# Forward pass through the network

hidden_layer_input = np.dot(input_data, weights_input_hidden) + biases_hidden

hidden_layer_output = np.maximum(0, hidden_layer_input) # ReLU activation


function
output_layer_input = np.dot(hidden_layer_output, weights_hidden_output) +
biases_output

output = output_layer_input # No activation function for output layer (assuming


regression)

# Print the output

print("Input Data:")

print(input_data)

print("\nWeights (Input to Hidden):")

print(weights_input_hidden)

print("\nBiases (Hidden):")

print(biases_hidden)

print("\nWeights (Hidden to Output):")

print(weights_hidden_output)

print("\nBiases (Output):")

print(biases_output)

print("\nOutput:")

print(output)

This code simulates the forward pass of a neural network with three layers. Here's a
brief explanation of each step:

1. We define the architecture of the neural network, specifying the number of


neurons in the input, hidden, and output layers.
2. We randomly initialize the weights and biases for each layer.
3. We define an input data array to feed into the network.
4. We perform the forward pass through the network:
 Calculate the input to the hidden layer by multiplying the input data by
the weights and adding the biases.
Apply the Rectified Linear Unit (ReLU) activation function to the hidden
layer's input to get the hidden layer's output.
 Calculate the input to the output layer by multiplying the hidden layer's
output by the weights and adding the biases.
 The output of the network is the input to the output layer.
5. We print out the input data, weights, biases, and the output of the network

Q.4. Explain Back propagation with its algorithm.

Ans:-
Backpropagation is a fundamental algorithm used to train artificial neural networks.
It's a supervised learning algorithm that adjusts the weights of the connections
between neurons in the network, enabling it to learn from labeled training data. The
goal of backpropagation is to minimize the difference between the predicted output
of the network and the actual output (i.e., the error).

Here's an explanation of backpropagation along with its algorithm:

Explanation:

1. Forward Pass:
 The input data is fed forward through the network layer by layer,
activating each neuron and producing an output.
 The output is compared to the actual target output, and the error for
each neuron in the output layer is calculated.
2. Backward Pass:
 The error is propagated backward through the network, starting from
the output layer to the input layer.
 For each neuron, the error is used to adjust its weights, aiming to
reduce the error in subsequent iterations.
3. Gradient Descent:
 Backpropagation utilizes gradient descent to update the weights. It
calculates the gradient of the error function with respect to each
weight.
 The weights are adjusted in the opposite direction of the gradient,
moving towards the minimum of the error function.
4. Iteration:
 The forward pass and backward pass are iteratively performed on
batches of training data until convergence, where the error is
minimized to an acceptable level or a predefined number of iterations
is reached.

Algorithm:
Here's a step-by-step algorithm for backpropagation:

1. Initialize Weights: Randomly initialize the weights of the connections


between neurons in the network.
2. Forward Pass:
 Input the training data into the network and perform the forward pass
to calculate the predicted output.
 Calculate the error between the predicted output and the actual output
using a suitable error metric (e.g., mean squared error).
3. Backward Pass:
 Compute the gradient of the error with respect to the weights of the
output layer using the chain rule.
 Propagate the error backward through the network, layer by layer,
computing the gradients of the error with respect to the weights of
each layer.
4. Weight Update:
 Update the weights of the connections using the gradients computed
in the backward pass and the learning rate hyperparameter.
 Adjust the weights in the direction that minimizes the error.
5. Repeat:
 Repeat steps 2-4 for a specified number of iterations or until the error
converges to a satisfactory level.
6. Evaluate:
 Once training is complete, evaluate the performance of the trained
network on unseen data to assess its generalization ability.

Q.5 Simulate a neural network training process with different activation functions. Analyze and
compare the convergence speed and accuracy.

Ans:-
To simulate a neural network training process with different activation functions, we'll
create a simple feedforward neural network and train it on a sample dataset using
different activation functions. We'll then analyze and compare the convergence
speed and accuracy for each activation function.

First, let's outline the steps:

1. Define a simple dataset for binary classification.


2. Implement a feedforward neural network with one hidden layer.
3. Train the network using different activation functions (e.g., ReLU, Sigmoid, and
Tanh).
4. Compare the convergence speed and accuracy for each activation function.
Let's start by implementing these steps in Python:

import numpy as np

import matplotlib.pyplot as plt

# Step 1: Define a simple dataset

np.random.seed(0)

X = np.random.randn(100, 2)

y = np.logical_xor(X[:, 0] > 0, X[:, 1] > 0) # XOR function

y = np.where(y, 1, 0) # Convert to 0/1 labels

# Step 2: Implement a feedforward neural network

class NeuralNetwork:

def __init__(self, input_size, hidden_size, output_size, activation):

self.W1 = np.random.randn(input_size, hidden_size)

self.b1 = np.zeros((1, hidden_size))

self.W2 = np.random.randn(hidden_size, output_size)

self.b2 = np.zeros((1, output_size))

self.activation = activation

def forward(self, X):

self.z1 = np.dot(X, self.W1) + self.b1

self.a1 = self.activation(self.z1)

self.z2 = np.dot(self.a1, self.W2) + self.b2

self.probs = self.sigmoid(self.z2)

return self.probs

def sigmoid(self, x):


return 1 / (1 + np.exp(-x))

def relu(self, x):

return np.maximum(0, x)

def tanh(self, x):

return np.tanh(x)

def sigmoid_derivative(self, x):

return x * (1 - x)

def relu_derivative(self, x):

return np.where(x > 0, 1, 0)

def tanh_derivative(self, x):

return 1 - np.square(x)

def backward(self, X, y, lr):

m = X.shape[0]

delta2 = self.probs - y.reshape(-1, 1)

dW2 = np.dot(self.a1.T, delta2)

db2 = np.sum(delta2, axis=0, keepdims=True)

if self.activation == self.relu:

delta1 = np.dot(delta2, self.W2.T) * self.relu_derivative(self.a1)

elif self.activation == self.sigmoid:

delta1 = np.dot(delta2, self.W2.T) * self.sigmoid_derivative(self.a1)

elif self.activation == self.tanh:

delta1 = np.dot(delta2, self.W2.T) * self.tanh_derivative(self.a1)


dW1 = np.dot(X.T, delta1)

db1 = np.sum(delta1, axis=0, keepdims=True)

# Update weights and biases

self.W1 -= lr * dW1

self.b1 -= lr * db1

self.W2 -= lr * dW2

self.b2 -= lr * db2

def train(self, X, y, lr, epochs):

for i in range(epochs):

self.forward(X)

self.backward(X, y, lr)

def predict(self, X):

return np.round(self.forward(X))

# Step 3: Train the network with different activation functions

activations = ['sigmoid', 'relu', 'tanh']

results = {}

for activation in activations:

model = NeuralNetwork(input_size=2, hidden_size=5, output_size=1,


activation=getattr(NeuralNetwork, activation))

model.train(X, y, lr=0.1, epochs=1000)

preds = model.predict(X)

accuracy = np.mean(preds == y.reshape(-1, 1))

results[activation] = {'accuracy': accuracy}


# Step 4: Compare convergence speed and accuracy

for activation, result in results.items():

print(f'Activation Function: {activation}, Accuracy: {result["accuracy"]:.4f}')

plt.bar(results.keys(), [result['accuracy'] for result in results.values()])

plt.xlabel('Activation Function')

plt.ylabel('Accuracy')

plt.title('Accuracy Comparison with Different Activation Functions')

plt.ylim(0, 1)

plt.show()

Q.6. Using a programming environment, simulate the implementation of the Sigmoid activation
function. Include a sample input-output scenario.

Ans:- import numpy as np

def sigmoid(x):

return 1 / (1 + np.exp(-x))

# Sample input

sample_input = np.array([-1, 0, 1, 2, 3])

# Apply sigmoid activation function

output = sigmoid(sample_input)

# Print the result

print("Input:", sample_input)

print("Output (Sigmoid):", output)

In this code:
 We define the sigmoid activation function, which takes an input array x and
returns the sigmoid of each element.
 We create a sample input array sample_input.
 We apply the sigmoid activation function to the sample input using the sigmoid
function.
 Finally, we print both the input and the output.

You might also like