ANN Research
ANN Research
Neurons or nerve cells that are the primary units of both the brain and the
nervous system. These neurons receive sensory input from the outside world
which they process and then provide the output which might act as the input
to the next neuron. Each of these neurons is connected to other neurons in
complex arrangements at synapses.
Artificial Neural Networks contain artificial neurons which are called units .
These units are arranged in a series of layers that together constitute the
whole Artificial Neural Network in a system. A layer can have only a dozen
units or millions of units as this depends on how the complex neural
networks will be required to learn the hidden patterns in the dataset.
Then this data passes through one or multiple hidden layers that transform
the input into data that is valuable for the output layer. Finally, the output
layer provides an output in the form of a response of the Artificial Neural
Networks to input data provided.
In the majority of neural networks, units are interconnected from one layer to
another. Each of these connections has weights that determine the influence
of one unit on another unit. As the data transfers from one unit to another,
the neural network learns more and more about the data which eventually
results in an output from the output layer.
The input layer of an artificial neural network is the first layer, and it
receives input from external sources and releases it to the hidden layer,
which is the second layer.
In the hidden layer, each neuron receives input from the previous layer
neurons, computes the weighted sum, and sends it to the neurons in the
next layer.
Connections between layers are "weighted," meaning each input from the
previous layer has a different level of importance. These weights are like
multipliers that adjust the impact of each input. During training, the network
learns how much each weight should be to make better predictions. In other
words, the network adjusts these weights over time to improve its
performance and make more accurate predictions.
What is Backpropagation?
Backpropagation is a technique used in deep learning to train
artificial neural networks particularly feed-forward networks. It
works iteratively to adjust weights and bias to minimize the cost
function.
In each epoch the model adapts these parameters reducing loss
by following the error gradient. Backpropagation often uses
optimization algorithms like gradient descent or stochastic
gradient descent. The algorithm computes the gradient using
the chain rule from calculus allowing it to effectively navigate
complex layers in the neural network to minimize the cost
function.
Where,
aj is the weighted sum of all the inputs and weights at
each node
wi,jwi,j represents the weights associated with the jth input
to the ith neuron
xi represents the value of the jth1 input
2. Sigmoid Function
The sigmoid function returns a value between 0 and 1,
introducing non-linearity into the model.
3. Computing Outputs
At h1 node
Once we calculated the a1 value, we can now proceed to find the
y3 value:
Similarly find the values of y4 at h2 and y5 at O3
4. Error Calculation
Where:
δj is the error term for each unit,
η is the learning rate.
2. Output Unit Error
For O3:
For h2:
4. Weight Updates
For the weights from hidden to output layer:
New weight:
New weight:
class NeuralNetwork:
def __init__(self, input_size, hidden_size, output_size):
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
self.weights_input_hidden = np.random.randn(self.input_size,
self.hidden_size)
self.weights_hidden_output = np.random.randn(self.hidden_size,
self.output_size)
self.output_activation = np.dot(self.hidden_output,
self.weights_hidden_output) + self.bias_output
self.predicted_output = self.sigmoid(self.output_activation)
return self.predicted_output
hidden_error = np.dot(output_delta,
self.weights_hidden_output.T)
hidden_delta = hidden_error *
self.sigmoid_derivative(self.hidden_output)
self.weights_hidden_output += np.dot(self.hidden_output.T,
output_delta) * learning_rate
self.bias_output += np.sum(output_delta, axis=0, keepdims=True)
* learning_rate
self.weights_input_hidden += np.dot(X.T, hidden_delta) *
learning_rate
self.bias_hidden += np.sum(hidden_delta, axis=0, keepdims=True)
* learning_rate
output_error = y - self.predicted_output: calculates the error
at the output layer
output_delta = output_error *
self.sigmoid_derivative(self.predicted_output): calculates the
delta for the output layer
hidden_error = np.dot(output_delta,
self.weights_hidden_output.T): calculates the error at the
hidden layer
hidden_delta = hidden_error *
self.sigmoid_derivative(self.hidden_output): calculates the
delta for the hidden layer
self.weights_hidden_output += np.dot(self.hidden_output.T,
output_delta) * learning_rate: updates weights between
hidden and output layers
self.weights_input_hidden += np.dot(X.T, hidden_delta) *
learning_rate: updates weights between input and hidden
layers
4. Training Network
The network is trained over 10,000 epochs using the
backpropagation algorithm with a learning rate of 0.1
progressively reducing the error.
def train(self, X, y, epochs, learning_rate):
for epoch in range(epochs):
output = self.feedforward(X)
self.backward(X, y, learning_rate)
if epoch % 4000 == 0:
loss = np.mean(np.square(y - output))
print(f"Epoch {epoch}, Loss:{loss}")
output = self.feedforward(X): computes the output for the
current inputs
self.backward(X, y, learning_rate): updates weights and
biases using backpropagation
loss = np.mean(np.square(y - output)): calculates the mean
squared error (MSE) loss
5. Testing Neural Network
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([[0], [1], [1], [0]])
output = nn.feedforward(X)
print("Predictions after training:")
print(output)
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]): defines the
input data
y = np.array([[0], [1], [1], [0]]): defines the target values
nn = NeuralNetwork(input_size=2, hidden_size=4, output_size=1):
initializes the neural network
nn.train(X, y, epochs=10000, learning_rate=0.1): trains the
network
output = nn.feedforward(X): gets the final predictions after
training
Output:
Trained Model
The output shows the training progress of a neural network over
10,000 epochs. Initially the loss was high (0.2713) but it gradually
decreased as the network learned reaching a low value of 0.0066
by epoch 8000. The final predictions are close to the expected
XOR outputs: approximately 0 for [0, 0] and [1, 1] and
approximately 1 for [0, 1] and [1, 0] indicating that the network
successfully learned to approximate the XOR function.
Advantages of Backpropagation for Neural
Network Training
The key benefits of using the backpropagation algorithm are:
Ease of Implementation: Backpropagation is beginner-
friendly requiring no prior neural network knowledge and
simplifies programming by adjusting weights with error
derivatives.
Simplicity and Flexibility: Its straightforward design
suits a range of tasks from basic feedforward to complex
convolutional or recurrent networks.
Efficiency: Backpropagation accelerates learning by
directly updating weights based on error especially in
deep networks.
Generalization: It helps models generalize well to new
data improving prediction accuracy on unseen examples.
Scalability: The algorithm scales efficiently with larger
datasets and more complex networks making it ideal for
large-scale tasks.
Challenges with Backpropagation
While backpropagation is powerful it does face some challenges:
1. Vanishing Gradient Problem: In deep networks the
gradients can become very small during backpropagation
making it difficult for the network to learn. This is common
when using activation functions like sigmoid or tanh.
2. Exploding Gradients: The gradients can also become
excessively large causing the network to diverge during
training.
3. Overfitting: If the network is too complex it might
memorize the training data instead of learning general
patterns.
Backpropagation is a technique that makes neural network learn.
By propagating errors backward and adjusting the weights and
biases neural networks can gradually improve their predictions.
Though it has some limitations like vanishing gradients many
techniques like ReLU activation or optimizing learning rates have
been developed to address these issues.
Artificial neural networks are trained using a training set. For example,
suppose you want to teach an ANN to recognize a cat. Then it is shown
thousands of different images of cats so that the network can learn to
identify a cat. Once the neural network has been trained enough using
images of cats, then you need to check if it can identify cat images correctly.
This is done by making the ANN classify the images it is provided by deciding
whether they are cat images or not. The output obtained by the ANN is
corroborated by a human-provided description of whether the image is a cat
image or not. If the ANN identifies incorrectly then back-propagation is used
to adjust whatever it has learned during training. Backpropagation is done by
fine-tuning the weights of the connections in ANN units based on the error
rate obtained. This process continues until the artificial neural network can
correctly recognize a cat in an image with minimal possible error rates.
2. Marketing and Sales: When you log onto E-commerce sites like
Amazon and Flipkart, they will recommend your products to buy based
on your previous browsing history. Similarly, suppose you love Pasta,
then Zomato, Swiggy, etc. will show you restaurant recommendations
based on your tastes and previous order history. This is true across all
new-age marketing segments like Book sites, Movie services,
Hospitality sites, etc. and it is done by implementing personalized
marketing . This uses Artificial Neural Networks to identify the
customer likes, dislikes, previous shopping history, etc., and then tailor
the marketing campaigns accordingly.