Program 4
Program 4
Importing Libraries
import
importnumpy
numpyasasnp
np
NumPy is imported, which is a powerful library for numerical computations, especially for
working with arrays and performing matrix operations.
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(x):
return x * (1 - x)
Sigmoid Activation Function: This is a common activation function used in neural networks. It
maps input values to a range between 0 and 1, which helps in squashing the output to be non-linear.
Sigmoid Derivative: The derivative of the sigmoid function is used in backpropagation to compute
how much the weights should be adjusted based on the error.
Output Data (y): The desired output for each corresponding input. For XOR, the output is 1 if the
inputs are different, otherwise 0.
Random Seed: This ensures the random number generator produces the same sequence every time
the program runs, which helps in reproducibility of results.
Hidden Layer: 2 neurons for this example. This layer processes input and learns useful
features.
Output Layer: 1 neuron because the output is a single binary value (0 or 1).
5. Initializing Weights
Weight Matrices:
weights_input_hidden: A 2x2 matrix representing weights between the input and hidden
layer, initialized with random values.
weights_hidden_output: A 2x1 matrix representing weights between the hidden and output
layer.
learning_rate = 0.5
epochs = 10000
Learning Rate: A hyperparameter that controls the step size during the update of weights. A value
of 0.5 is used here.
Epochs: The number of iterations over the entire training dataset, set to 10,000.
Forward Pass
hidden_layer_output = sigmoid(hidden_layer_input)
predicted_output = sigmoid(output_layer_input)
Hidden Layer Output: The activation function (sigmoid) is applied to the input to get the
output of the hidden layer.
Input to Output Layer: The same process is repeated for the output layer, where the hidden
layer’s output is multiplied by the weights between the hidden and output layers.
Predicted Output: The sigmoid activation is applied to get the final output (between 0 and
1).
Calculating Error
error = y - predicted_output
Error: The difference between the actual output (y) and the predicted output (predicted_output).
error_hidden_layer = d_predicted_output.dot(weights_hidden_output.T)
Error at Hidden Layer: The error is propagated backward from the output layer to the hidden layer
using the dot product of the output layer’s error with the transpose of the weights.
Gradient at Hidden Layer: The gradient (d_hidden_layer) is calculated by multiplying the error at
the hidden layer with the derivative of the sigmoid function applied to the hidden layer's output.
Updating Weights
Weight Update: The weights are updated by moving in the direction of the negative gradient
to reduce the error. The learning rate controls the size of the weight updates.
Every 1000 epochs, the mean absolute error is printed to monitor how well the model is learning.
9. Final Output
print(predicted_output)
This means the network has learned to solve the XOR problem.
[[0]
[1]
[1]
[0]]