Professional Documents
Culture Documents
BPN was discovered by Rumelhart, Williams & Honton in 1986. The core concept
of BPN is to backpropagate or spread the error from units of output layer to
internal hidden layers in order to tune the weights to ensure lower error rates. It is
considered a practice of fine-tuning the weights of neural networks in each
iteration. Proper tuning of the weights will make a sure minimum loss and this will
make a more robust, and generalizable trained neural network.
y[:3]
Output:
array([[1, 0, 0],
[1, 0, 0],
results = pd.DataFrame
(columns=["mse", "accuracy"])
Lets initialize the weights for hidden and output layers with random values.
# Initialize weights
np.random.seed(10)
# feedforward propagation
# on hidden layer
Z1 = np.dot(x_train, W1)
A1 = sigmoid(Z1)
# on output layer
Z2 = np.dot(A1, W2)
A2 = sigmoid(Z2)
# Calculating error
mse = mean_squared_error(A2, y_train)
acc = accuracy(A2, y_train)
results=results.append({"mse":mse, "accuracy":acc},ignore_index=True )
# backpropagation
E1 = A2 - y_train
dW1 = E1 * A2 * (1 - A2)
E2 = np.dot(dW1, W2.T)
dW2 = E2 * A1 * (1 - A1)
# weight updates
W2_update = np.dot(A1.T, dW1) / N
W1_update = np.dot(x_train.T, dW2) / N
W2 = W2 - learning_rate * W2_update
W1 = W1 - learning_rate * W1_update
Plot MSE and Accuracy
Lets plot mean squared error in each iteration using pandas plot() funciton.
results.mse.plot(title="Mean Squared Error")
Output:
Z2 = np.dot(A1, W2)
A2 = sigmoid(Z2)
Accuracy: 0.8
you can see in the above output, we are getting 80% accuracy on test dataset.
Conclusion
Backpropagation neural network is a method to optimize neural networks by
propagating the error or loss into a backward direction. It finds loss for each node
and updates its weights accordingly in order to minimize the loss using gradient
descent.
Explanation of back-propagation algorithm
Backpropagation, short for "backward propagation of errors," is a fundamental algorithm used in
the training of artificial neural networks, including deep learning models. It is a supervised
learning technique that helps the network adjust its internal parameters (weights and biases) to
minimize the difference between the predicted output and the actual target output for a given set
of input data.
1. Forward Pass: During the forward pass, input data is fed through the neural network,
and the network's weights and biases are used to make predictions. The input data
propagates through the network layer by layer, producing an output.
2. Calculation of Error: The output of the network is compared to the actual target output
(ground truth). The difference between the predicted output and the actual output is
quantified using a loss function, which measures the error or cost.
4. Weight Update: With the gradients of the loss function with respect to the network's
parameters (weights and biases), the network updates its parameters in the opposite
direction of the gradient to minimize the loss. This is typically done using optimization
algorithms like gradient descent.
The backpropagation process is repeated iteratively for a large number of training examples to
improve the network's ability to make accurate predictions. The network continues to adjust its
parameters until the loss converges to a minimum, at which point the network has learned to map
inputs to outputs effectively.
The sigmoid activation function is like a switch that determines how much a neuron should
activate. The gradient of the sigmoid function helps us figure out how sensitive the neuron's
output is to changes in its input.
In easy terms, it tells us how much a small change in the input affects the output.
This is like a ruler to measure how wrong our prediction is. The gradient of the mean squared
error loss helps us find out the direction and magnitude we should adjust our prediction to make
it better.
In simple words, it tells us how much and in which direction we should nudge our prediction to
make it closer to the actual result.
When we update the "strength" of connections between neurons (weights) or how active a
neuron is (bias), we need to know how these changes will affect the overall error. These
gradients tell us how much we should adjust weights and biases to reduce the error.
X: A NumPy array representing the input data. It has three data points, each with two
features (hours of sleep and hours of studying).
y: A NumPy array representing the corresponding target values (test scores). The target
values are scaled to be between 0 and 1.
2. NeuralNetwork Class:
A class named NeuralNetwork is defined to encapsulate the neural network model and
its operations.
self.W1: Weight matrix for the connections between the input and hidden layer. It's
initialized with random values and has dimensions (2x3).
self.W2: Weight matrix for the connections between the hidden and output layer. Also
initialized with random values and has dimensions (3x1).
4. Feedforward Method:
feedForward(self, X): This method performs forward propagation through the neural
network.
self.z: The weighted sum of the input and weights for the first layer.
output: The final output after applying the sigmoid activation function to self.z3.
6. Backpropagation Method:
self.z2_error: The error in the hidden layer, which measures how much the hidden layer
contributes to the output error.
self.z2_delta: The delta for the hidden layer after applying the derivative of the sigmoid
function.
7. Training Method:
It calls feedForward to obtain the output and then backward to adjust the weights
based on the errors and deltas.
8. Training Loop:
A for loop runs for 1000 iterations to train the neural network.
After every 100 iterations, it prints the mean squared error (loss) to monitor training
progress.
Inside the loop, the train method is called to update the weights based on the input
data and target.
9. Printing Results:
After training, the input data (X), actual output (y), and the final loss are printed.
The predicted output is displayed, representing the neural network's predictions for the
input data.
OUTPUT